Guides / Managing results / Rules / Detecting intent

You can use Rules to detect a specific word and modify a query setting because of this word.

Limiting the search to only a subset of attributes

Use Case - Title or ID (SKU) searches

A good 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 signalling to the system that they are looking for an article whose title or ID contains “ref21”.

Rule

If query = “article ref21” then remove article and search for an article whose title or ID contains ‘ref21’

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Turn JSON into an array
$rule = array(
  'objectID' => 'article-rule',
  'conditions' => array(array(
    'pattern' => 'article',
    'anchoring' => 'startsWith'
  )),
  'consequence' => array(
    'params' => array(
      'query' => array(
        'remove' => 'article'
      ),
      'restrictSearchableAttributes' => array(
        'title',
        'book_id'
      )
    )
  )
);

// Push Rule to index
$index->saveRule($rule);

Using the dashboard

You can also add your rules in your Algolia dashboard.

  1. Select the Rules section from the left sidebar menu in the Algolia dashboard.
  2. Under the heading Rules, select the index you are adding a Rule to.
  3. Click the New rule button.
  4. Select Create your first rule or New rule. In the dropdown, click on the Manual Editor option.
  5. In the Condition(s) section, keep Query toggled on, select Contains in the dropdown, and enter “article” in the input field.
  6. In the Consequence(s) section:
    • Click the Add consequence button and select Add Query Parameter.
    • In the input field that appears, enter the JSON search parameter you want to add. For example, { "restrictSearchableAttributes": ["title","id"] }.
    • Click the Add consequence button again and select Remove Word.
    • Type or select “article” in the input field.
  7. Don’t forget to save your changes.

Did you find this page helpful?