Dynamic Filtering with Query Rules

You can use Query Rules to add filters dynamically depending on what the user types.

Filter by color

Use Case

Consider the query “red t-shirt”. A customer wants Algolia to recognize “red” as a color and therefore restrict the search only to records containing “red” in their color attribute whenever the keyword “red” is typed in.

Rule

If query contains ‘red’ then filter by color=red

Option 1 - Using filters

API

One approach is to use the filters parameter. With this approach, you will need to have 1 rule per filter value. So if you have 10 color options, you will need to create 10 rules, 1 for each color.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$rule = array(
  'objectID' => 'red-color',
  'condition' => array(
    'pattern' => 'red',
    'anchoring' => 'contains'
  ),
  'consequence' => array(
    'params' => array(
      'query' => array(
        'remove' => 'red'
      ),
      'filters' => 'color:red'
    )
  )
);

// push rule to index
$index->saveRule($rule['objectID'], $rule);

Dashboard

Qr red filters


Option 2: Using facets

This alternative method is only available through our Enterprise plans.

API

This approach uses facets. With facets, instead of creating 1 rule for each color, you create 1 rule per facet. So if you have the facet ‘color’, then 1 rule will handle every color value. For example, if the query includes red, and if red matches a value in the color facet, the rule will be triggered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// turn json into an array
$rule = array(
  'objectID' => 'color-facets',
  'condition' => array(
    'objectID' => 'color-facets',
    'pattern' => '{facet:color}'
  ),
  'consequence' => array(
    'params' => array(
      'automaticFacetFilters' => ['color']
    )
  )
);

// push rule to index
$index->saveRule($rule['objectID'], $rule);

note: You can replace automaticFacetFilters with automaticOptionalFacetFilters. Same syntax. This will include non-red t-shirts below the red ones. Learn more about optional filters.

Dashboard (facets)

This alternative method is only available through our Enterprise plans.

Qr red facets

Filter by type

Use Case

Show only “shirts” whenever the engine detected a kind of shirt like “t-shirt”.

Rule

If query contains ‘t-shirt’ then filter by type=shirt

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// turn json into an array
$rule = array(
  'objectID' => 't-shirt',
  'condition' => array(
    'pattern' => 't-shirt',
    'anchoring' => 'contains'
  ),
  'consequence' => array(
    'params' => array(
      'query' => array(
        'remove' => 't-shirt'
      ),
      'filters' => 'clothing_type:shirt'
    )
  )
);

// push rule to index
$index->saveRule($rule['objectID'], $rule);

Dashboard

Qr t shirt

Numerical filtering

Use Case

Imagine the query “cheap toaster 800w”. Query Rules can be used to filter the results by “toaster” and “prices between 0 and 25”, so that the only textual search is the remaining term, “800w”, which could further be used to limit the results with that wattage.

Rule

If query = “cheap toaster” then price < 10 and type=toaster

Note: This requires 2 rules.

API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// turn json into an array
$rules = array(
  array(
    'objectID' => 'toaster',
    'condition' => array(
      'pattern' => 'toaster',
      'anchoring' => 'contains'
    ),
    'consequence' => array(
      'params' => array(
        'query' => array(
          'remove' => 'toaster'
        ),
        'filters' => 'product_type:toaster'
      )
    )
  ),
  array(
    'objectID' => 'cheap',
    'condition' => array(
      'pattern' => 'cheap',
      'anchoring' => 'contains'
    ),
    'consequence' => array(
      'params' => array(
        'query' => array(
          'remove' => 'cheap'
        ),
        'filters' => 'price < 10'
      )
    )
  )
);

// push rule to index
$index->batchRules($rules);

Dashboard

Qr toaster

Qr cheap

Did you find this page helpful?