Querying in the middle of a word
How to match queries with records that contain the query text in the middle or the end of a word.
By design, Algolia doesn’t support infix/suffix matching; therefore it won’t find substrings in the middle or the end of a string. Only prefix matching is natively supported. This means that if a user searches for “note”, this would find records with attributes containing “notepad” or “notebook”, but wouldn’t match “keynote”.
However, there are occasional use cases where this behavior can be useful. Let’s say you have prefixed references in your system (e.g., “XXXABCDEF”) but not on printed catalogs. Therefore, if a user searched for “ABCDEF”, it wouldn’t return the product with reference “XXXABCDEF”. In this case, it would make sense to enable search in the middle of the reference.
To do this with Algolia, you need to generate alternatives.
Partial matching can be dangerous for relevance because they create noise. Make sure you do it sparingly and limit the number of alternatives to what makes sense.
Modifying the data: an example
Before
Let’s say you have the following dataset for products:
The product_reference
attribute has a numeric prefix, which may exist for internal purposes. The problem is, if a user searches for “ABCDEF” or “GHIJKL”, they won’t find anything.
Since Algolia only supports prefix search, what you need to do to is add an attribute that contains all the possible suffixes for product_reference
, as an array.
After
Once processed, the records should look like this:
We’ve created a new attribute called product_reference_suffixes
where we’ve stored all suffixes for the product_reference
attribute.
Note that you might not need to generate all possible suffixes, and stop when the reference is of a certain length. As said above, partial matching generates noise, so the more short alternatives you have, the more your search may return irrelevant results.
Updating searchableAttributes
You also need to add product_reference_suffixes
to the list of searchable attributes, to ensure the suffixes are searchable along with the original product reference.
It’s recommended to set product_reference_suffixes
after product_reference
to give it a lower priority. This way, a full match would be considered better than a partial one.
Was this page helpful?