Setting Searchable Attributes

When you create an index, all attributes from all your records are searchable by default. This lets you perform searches right from the start without having to configure anything. Yet, if you want to make your search more relevant and eliminate the noise, you only want to set meaningful attributes as searchable. For example, if you’re building a cooking recipes website, you might include data such as image URLs which aren’t relevant for textual search.

You can do this by using Algolia’s searchableAttributes setting, by specifying what attributes should be searchable. You can even go a step further and prioritize your searchable attributes, making some more relevant than others.

Dataset Example

Let’s go back to our example. We’re developing a website to find recipes. Here’s what the dataset could look like:

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
[
  {
    "title": "Gluten free sponge cake",
    "ingredients": [
      "gluten free self raising flour",
      "caster sugar",
      "eggs"
    ],
    "image_urls": [
      "http://myrecipewebsite/images/bread-and-cakes/glutenfreesponge1.jpg",
      "http://myrecipewebsite/images/bread-and-cakes/glutenfreesponge2.jpg"
    ],
    "comments": [
      "This is incredible! I added raisins and it was even better."
    ]
  },
  {
    "title": "Gluten Free Oatmeal Cinnamon Raisin Bread",
    "ingredients": [
      "brown rice flour",
      "potato starch",
      "raisins"
    ],
    "image_urls": [
      "http://myrecipewebsite/images/bread-and-cakes/glutenfreeoatmealraisins1.jpg",
      "http://myrecipewebsite/images/bread-and-cakes/glutenfreeoatmealraisins2.jpg"
    ],
    "comments": [
      "Amazing, this almost tastes like cake."
    ]
  }
]

If you index this dataset without adding any setting, all attributes will be searchable. In our case, this means that when users make a search, the engine also searches into attributes like image_urls, which you likely only want to use for display purposes.

For that reason, you want to explicitly set searchable attributes on what users would realistically search for. Here, it makes sense to make title, ingredients and comments searchable, and to leave image_urls out. Note that attributes can still be displayed or filtered without making them searchable.

Using the API

To make some attributes searchable, you need to use searchableAttributes during indexing time.

It’s important to note that if you set searchable attributes at the same level, they will have the same priority. In our case, this means that if you set title and comments at the same level and someone searches for cake, matches in the comments may come before matches in the recipe title.

Instead, you can order searchable attributes to define a priority.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// `title` and `comments` have the same priority
$index->setSettings([
  'searchableAttributes' => [
    "title,comments",
    "ingredients"
  ]
]);

// `title` has the highest priority, then `ingredients`, then `comments`
$index->setSettings([
  'searchableAttributes' => [
    "title",
    "comments",
    "ingredients"
  ]
]);

Using the Dashboard

You can also set your searchable attributes in your Algolia dashboard.

  • Go to your dashboard and select your index.
  • Click the Ranking tab.
  • In the Searchable Attributes section, click the Add a searchable attribute button.
  • To add attributes with the same level of importance, type them directly in the input field as a comma-separated list (e.g.: title,comments).
  • To add attributes with an order of importance, click on attributes in the dropdown one after the other.
  • Don’t forget to save your changes.

Did you find this page helpful?