Guides / Managing results / Rules / Merchandising and promoting items

Combining Multiple Consequences

When you’re setting a 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 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. Select the Rules section from the left sidebar menu in the Algolia dashboard.
  5. Under the heading Rules, select the index you are adding a Rule to.
  6. Select Create your first rule or New rule. In the dropdown, click on the Manual Editor option.
  7. In the Condition(s) section, keep Query toggled on, select Contains in the dropdown, and select the option Add Facet “director” from the input dropdown. {facet:director} should then be displayed in the input.
  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. Don’t forget to save your changes.

Did you find this page helpful?