Query Rules Overview
On this page
What is Query Rules solving?
As part of customer support, we take note of frequent requests, looking for patterns. When a particular problem persists, we take a step back and think about how we can best address the problem.
Query Rules comes directly out of this process, as it addresses a number of recurring and similarly-related customer requests.
For example, we’ve had many requests for word detection. Customers ask whether we have a mechanism that could weigh words differently depending on context, thereby creating relevance based on meaning, not only textual matching. Can Algolia dynamically adapt its behavior to what a user types?
Up to now, if a customer wanted this kind of targeted, dynamic behavior, we advised changing settings, defining different data attributes, and using features such as synonyms, query expansion, and optional filters. In other words, we relied on existing functionality that, when put together, more or less achieved the desired results. But these solutions required some extra implementation work, and they also applied to every search, thereby improving the relevance for some queries but degrading it for others.
For example: consider “red t-shirt”. If a customer wanted Algolia to recognize “red” as a color and therefore search only in the color attribute whenever “red” was typed in, or to show only “shirts” whenever the engine detected a kind of shirt like “t-shirt”, we would need a new mechanism.
This is what Query Rules has been designed to address.
Every Algolia customer can benefit from Query Rules
Query Rules can be used by any business, in any industry, to address the same kind of issues that customers typically face with search. Even if the details of the problem differ, the general solution is the same.
Whether promoting a book, or certain fruits, or red t-shirts, or filtering content based on themes or categories, or showing only low cost or newer items - you can manage all these situations (and a lot more) doing more or less the same thing with Query Rules. So as you read through our examples and use cases, try to see how you can adapt them to your own needs.
If This Then That
You can think of Query Rules as an If This Then That logic: If Apple is the query, then put iPhone at the top.
We expect that you will not need to use Query Rules in ninety-nine percent of your use-cases. With Custom Ranking and a well-configured index (here, here, here, …), Algolia should normally produce the kind of results you want.
But there are some situations that fall through the cracks. Query Rules has been built for this.
All attributes are searchable by default, which enables search to work right from the start. However, for better relevance, you want to be more selective by setting only some attributes as searchable. You can do this with the searchable attributes feature. You can also use this setting to prioritize your search attributes, making some more relevant than others.
Quick Overview of How Query Rules Work
Rules are If-Then configurations, or condition/consequence pairs.
The if involves parsing the user-entered query and seeing if any part of the query satisfies a condition (that we’ve set up with Query Rules).
If the if condition is satisfied, we process the then consequence.
With the If-Then approach, the search engine adapts its behavior using the then-consequences associated with a if-condition. If the text contains “red”, for example, the consequence is that the engine can filter on the facet “color”.
And that’s only one kind of consequence. As described in our overview, and presented in technical detail below - there are many kinds of consequences that adapt the search and alter the relevance.