Guides / Managing results / Rules / Detecting intent

Enhance search results with query-based rules

You can use rules to enhance the user experience based on what they search for.

For example, if you’re building an app to search for airports, Algolia’s geolocation feature helpfully ranks results by proximity. However, the nearest airport isn’t necessarily the best choice. In Paris, a search based on proximity might rank Le Bourget, a small airport for private jets, over large airports like Charles de Gaulle or Orly.

As a solution, you could prioritize other factors over proximity. For example, you could rank airports by their number of airline connections. However, if you also set a geolocation parameter like aroundLatLngViaIP, proximity remains the primary factor in determining the order of search results.

A better solution is to create a rule so that when users search for a specific city or country, it turns off the aroundLatLngViaIP parameter for that query.

Before you begin

Before creating the rule, ensure that:

Create the rule in the dashboard

This example assumes you’re using the sample dataset.

Custom ranking and attributes for faceting

  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Algolia Search Search.
  3. Select your Algolia index:

    Select your Algolia application and index

  4. On the Configuration tab, click Ranking and Sorting.
  5. Click Add custom ranking attribute and select the nb_airline_connections attribute from the drop-down menu.
  6. Click Facets, under Filtering and faceting, then click Add an Attribute and select country and city from the drop-down menu.
  7. Save your changes.

Rule

Use the dashboard to create a rule that detects matches in facets city and country, and changes the search parameters accordingly.

  1. Click Rules in the Algolia dashboard’s left sidebar menu.
  2. Select Create your first rule or New rule.
  3. In the drop-down menu, click Manual Editor.
  4. In the Condition(s) section, keep Query toggled on, click Contains in the drop-down menu, and select country. {facet:country} should appear in the input.
  5. In the Consequence(s) section:

    • Click Add consequence and select Add Query Parameter.
    • In the input field that appears, add the JSON parameters to apply when a user query matches the rule: { "aroundLatLngViaIP": false }
  6. Save your changes.
  7. Click New Rule and then Manual Editor again.
  8. In the Condition(s) section, keep Query toggled on, click Contains in the drop-down menu, and select city. {facet:city} should appear in the input.
  9. In the Consequence(s) section:

    • Click Add consequence and select Add Query Parameter.
    • In the input field that appears, add the JSON parameters to apply when a user query matches the rule: { "aroundLatLngViaIP": false }
  10. Save your changes.

Create the rule with the API

Before creating the rule, set custom ranking and the attributes for faceting.

This example assumes you’re using the sample dataset.

Custom ranking and attributes for faceting

1
2
3
4
5
6
7
$index->setSettings([
  'customRanking' => [
    'desc(nb_airline_liaisons)'
  ],
  'attributesForFaceting' => [
    'city', 'country'
]);

Create the rule

Use the batchRules method to create a rule that detects matches in facets city and country, and changes the search parameters accordingly.

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
$index->saveRules([
  [
    'objectID' => 'country',
    'conditions' => array(array(
      'pattern' => '{facet:country}',
      'anchoring' => 'contains'
    )),
    'consequence' => [
      'params' => [
        'aroundLatLngViaIP' => false
      ]
    ]
  ],
  [
    'objectID' => 'city',
    'conditions' => array(array(
      'pattern' => '{facet:city}',
      'anchoring' => 'contains'
    )),
    'consequence' => [
      'params' => [
        'aroundLatLngViaIP' => false
      ]
    ]
  ]
]);

Query time

If a user query exactly matches a city or a country in your dataset, the rule sets aroundLatLngViaIP to false overriding any other parameters in the query.

For example, if a user searches for “Paris” or “China”, matching airports are ranked by nb_airline_liaisons. When not searching for either a city or a country, airports are ranked by distance from the user’s location.

1
2
3
$results = $index->search('query', [
  'aroundLatLngViaIP' => true
]);

Example dataset

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[
  {
    "name": "Orly",
    "city": "Paris",
    "country": "France",
    "_geoloc": { "lat": 48.725278, "lng": 2.359444 },
    "nb_airline_connections": 404
  },
  {
    "name": "Charles De Gaulle",
    "city": "Paris",
    "country": "France",
    "_geoloc": { "lat": 49.012779, "lng": 2.55 },
    "nb_airline_connections": 1041
  },
  {
    "name": "Le Bourget",
    "city": "Paris",
    "country": "France",
    "_geoloc": { "lat": 48.961487, "lng": 2.436966 },
    "nb_airline_connections": 0
  },
  {
    "name": "Ben Gurion",
    "city": "Tel-Aviv",
    "country": "Israel",
    "_geoloc": { "lat": 32.011389, "lng": 34.886667 },
    "nb_airline_connections": 271
  },
  {
    "name": "Haifa",
    "city": "Haifa",
    "country": "Israel",
    "_geoloc": { "lat": 32.809444, "lng": 35.043056 },
    "nb_airline_connections": 4
  },
  {
    "name": "Pudong",
    "city": "Shanghai",
    "country": "China",
    "_geoloc": { "lat": 31.143378, "lng": 121.805214 },
    "nb_airline_connections": 825
  },
  {
    "name": "Hongqiao Intl",
    "city": "Shanghai",
    "country": "China",
    "_geoloc": { "lat": 31.197875, "lng": 121.336319 },
    "nb_airline_connections": 411
  }
]

Further reading

To learn how to import data into Algolia, see Send and update your data.

Did you find this page helpful?