When you’re setting a Query Rule, it’s not uncommon that you need to set more than one consequence to achieve your goal. A good example is when you want to use a search term as a filter or a facet value, but not for textual search.

Imagine you have a movie database website, where users can search movies by title, actors, or director. Some prolific actors, like Clint Eastwood, also have a rich filmography as a director, and not everyone searching for “clint eastwood” might 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

Let’s say we have the following dataset. Some movies have Clint Eastwood as an actor, some as a director, and some have both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[
  {
    "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 front end.

Yet, an even better way of helping your end user 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.

Concretely, you’ll 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

First, you need to set director as attributesForFaceting. This happens at indexing time.

1
2
3
4
5
$index->setSettings([
  'attributesForFaceting' => [
    "director"
  ]
]);

Then, you can set a query rule that detects the term “director” in a query whenever it comes after a term that matches the name of a director, and applies a filter on facet value director:'Clint Eastwood'. For this, you need to use the saveRule method.

However, setting the rule isn’t enough. Even when you set a rule, the engine still tries to match on the terms of 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 our dataset, this would return no results at all. This is where combining consequences comes in handy.

You can add another consequence, to remove the word “director” from the query, when this rule applies. This way, it won’t be used as a search term, only for filtering purposes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$rule = [
  'objectID' => 'director-rule',
  'condition' => [
    'pattern'   => '{facet:director} director',
    'anchoring' => 'contains',
  ],
  'consequence' => [
    'params' => [
      'automaticFacetFilters' => ['director'],
      'query' => [
        'edits' => [
          [
            'type' => 'remove',
            'delete' => 'director'
          ]
        ]
      ]
    ]
  ]
];

$response = $index->saveRule($rule);

Using the Dashboard

You can also add your rules in your Algolia dashboard.

  1. Go to your dashboard and 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. Then, go to the Query Rules tab and click the “New Rule” button.
  5. In the Condition(s) section:
    • Set the anchoring of “if the query…” to Contains.
    • Type {facet:director} director in the input field and press enter.
  6. In the Consequences section:
    • Click the “Add consequence” button and select Filter Matching Attributes (or Filter/Boost Matching Attributes on Enterprise plans).
    • Type director in the Filter field and press enter.
    • Click the “Add consequence” button again and select Remove Word.
    • Type “director” in the input field and press Enter.
  7. Don’t forget to save your changes!

Did you find this page helpful?