When creating a Rule, it’s not uncommon to set more than one consequence. A good example is when you want to use a search term as a filter or a facet value but not for textual search.

Consider a website where users can search for movies by title, actor, or director. Some actors, like Clint Eastwood are also directors: people searching for “clint eastwood” might not be looking for the same thing. For example, people looking only for movies directed by Clint Eastwood might search for “clint eastwood director”, hoping to filter out every movie that Clint Eastwood plays in but didn’t direct.

In this case, you wouldn’t be interested in the word “director for textual search purposes, but as a cue to set a filter. You would therefore want to remove the word “director” from the search query so that the engine doesn’t textually look for records matching that word. You can achieve this by combining several consequences in your Rule.

Dataset example

In the following dataset, some movies have Clint Eastwood as an actor, some as a director, and some have both.

json
[
  {
    "title": "The Good, the Bad and the Ugly",
    "director": "Sergio Leone",
    "actors": ["Clint Eastwood", "Lee Van Cleef", "Elli Wallach"]
  },
  {
    "title": "Dirty Harry",
    "director": "Don Siegel",
    "actors": ["Clint Eastwood", "Andy Robinson", "Harry Guardino"]
  },
  {
    "title": "Million Dollar Baby",
    "director": "Clint Eastwood",
    "actors": ["Clint Eastwood", "Hilary Swank", "Morgan Freeman"]
  },
  {
    "title": "Invictus",
    "director": "Clint Eastwood",
    "actors": ["Morgan Freeman", "Matt Damon", "Tony Kgoroge"]
  }
]

If someone searches for “clint eastwood”, they would retrieve all movies by or with Clint Eastwood, even if they were only interested in movies with Clint Eastwood as the director. You could solve this problem with faceting, by setting director as an attribute for faceting and providing a refinement list with its own search input on your frontend.

Yet, an even better way of helping your users would be to make your search smart and look only for movies where director is equal to “Clint Eastwood” whenever they type “clint eastwood director”. To achieve this, you first need to set director in your list of attributes for faceting. Then, you can create a new Rule that filters on matching attributes.

You need to create two consequences whenever a user types in “clint eastwood director”:

  • Filter all results with “director”
  • Remove the word “director” from the search

Using the API

  1. Set director as attributesForFaceting. This happens at indexing time.

  2. Set a Rule that:

    • Detects the term “director” in a query whenever it comes after a term that matches the name of a director
    • Applies a filter on facet value director:'Clint Eastwood'.

To do this, use the saveRule method.

However, setting the Rule isn’t enough. Even when you set a Rule, the engine still tries to match the terms in the query. If a user searches for “clint eastwood director”, even if your Rule applies, the engine will look for the word “director” in the records. With the example dataset, this would return no results at all. This is where combining consequences comes in handy.

When this Rule applies, you can add another consequence to remove the word “director” from the query. This way, the engine will only use it for filtering, not search.

Using the dashboard

You can also add your Rules in Algolia’s dashboard.

  1. Select the Search product icon on your dashboard and then select your index.
  2. Click the Configuration tab.
  3. In the Facets subsection of Filtering and Faceting, click the “Add an attribute” button and select the director attribute from the dropdown.
  4. Select the Rules section from the left sidebar menu in the Algolia dashboard.
  5. Under the heading Rules, select the index to which you’re adding a Rule.
  6. Select Create your first rule or New rule. In the dro-down menu, click the Manual Editor option.
  7. In the Condition(s) section, keep Query toggled on, select Contains in the drop-down menu, and select the option Add Facet “director” from the input box. {facet:director} should then be displayed.
  8. In the Consequence(s) section:
    • Click the Add consequence button and select Filter Matching Attributes (or Filter/Boost Matching Attributes).
    • In the Filter input that appears, select the option Add Facet “director” in the dropdown.
    • Click the Add consequence button again and select Remove Word.
    • In the input that appears, select Remove {facet:director}.
  9. Save your changes.