On this page
You can set up rules (Query Rules) that parse your users’ queries and use certain query terms to perform certain actions. We see that in merchandising and promoting, where Query Rules can be used to promote or hide results. Now we’ll go further and use the contents of a query to discern the underlying intent of the user. With this intent, we can set up rules that generate filters, create new settings, personalize results, and/or change the entire query to better match the data in your index.
We’ll discuss the most popular applications of this feature. However, note that Query Rules is flexible enough to handle additional applications and intents.
- Dynamic filtering
- Removing or replacing words and phrases
- Replacing the entire query
- Creating keyword-based searches
- Adding additional settings based on the content of the query
Auto/Dynamic Filtering based on the query
Imagine a query “cheap toaster 800w”. Query Rules can be used to filter the results by “toaster” and “prices between 0 and 25”, so that the only textual search is the remaining term “800w”, which will further limit the results with that wattage.
Another example: “tomato” - A simple search, but one that may never return the fruit (or vegetable) because too many Tomato Soup brands are selling better than cherry tomatoes. With a dataset that contains both tomatoes and tomato soups, there’s currently no way for the engine to distinguish between those two items - even though we consider that when people type “tomato” they are looking for the fruit and not the soup. Query Rules can help contextualize a search - that is, provide hints about what to display - by adding a filter depending on the query.
So in this example when the query is “tomato” we can add a filter “category=fruit”.
Altering the query by recognizing keywords
An excellent example of altering a query is with an online document library that allows keyword searches inside documents. If a user types in the word “article ref21”, they are probably signaling to the system that they are looking for an article whose title or id contains “ref21”. Article, in this context, is a keyword; ref21 is an id.
Currently, Algolia wouldn’t return the expected results because:
- the engine would search using both the word “article” and the word “ref21”
- however, unfortunately, the records don’t contain the word “article”.
Query Rules can fix this: when we see the pattern “article ref”, we remove the word “article”. By doing that, “article ref21” would be replaced by “ref21”, moreover, we would therefore only find records containing “ref21” in the
Query Rules is designed for this, to easily create rules that recognize keywords like “article” or “financial report” and remove them from the search, focusing the search exclusively on the article’s title or reference id.
Replacing words (similar to synonyms)
This feature can be compared to synonyms. Our current synonym functionality expands a search by adding words with similar meanings. When you create a synonym, the engine searches both the typed word and its synonym. However, what if you want to search only the synonym? You can’t do this with the Synonyms functionality.
Query Rules offers an alternative. You can now replace words instead of adding new ones. For example, if you make “tv” a synonym for “television”, Query Rules will replace “tv” with “television” so that only “television” is used to search, as opposed to synonyms, which will search with both “tv” and “television”.
Query Rules, therefore, reduces the noise that comes from using both synonyms, because Synonyms will return results from both “tv” and “television”, whereas Query Rules will removes all results from “tv”.
Replacing the entire query
This is similar to replacing words, but it replaces the entire query with alternative words or phrases, and removes the query from the search process. The use case is slightly different: Imagine a query where users type in a common, informal phrase for a product, but you don’t use this phrase in your records. You can remove the common phrase from the query and replace it with your own vocabulary. An excellent example of this is “smartphone apple” which you can replace with “iphone”.
Applying search parameters
Saving the best for last. Perhaps the most flexible and powerful functionality that Query Rules offers is its ability to add or change query settings on the fly. You can use its query parsing capability to determine which settings best apply to the given search. Potentially, every search can have a different logic. Depending on the search, you can disable/enable typo tolerance, change geolocation settings, search only certain types of attributes, filter differently, and a host of other possibilities.