Guides / Sending and managing data / Format and structure your data

Setting Searchable Attributes

When you create an index, all attributes from all your records are searchable by default. Having all attributes searchable by default 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 recipe website, you might include data such as image URLs which aren’t relevant for textual search.

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

Dataset

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"
    ],
    "images": [
      "https://yourdomain.com/bread-and-cakes/glutenfreesponge1.jpg",
      "https://yourdomain.com/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"
    ],
    "images": [
      "https://yourdomain.com/bread-and-cakes/glutenfreeoatmealraisins1.jpg",
      "https://yourdomain.com/bread-and-cakes/glutenfreeoatmealraisins2.jpg"
    ],
    "comments": [
      "Amazing, this almost tastes like cake."
    ]
  }
]

If you index this dataset without adding any setting, all attributes are searchable. In our case, this means that when users search, the engine also searches into attributes like images, 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 leave images out. Note that you can can still display or filter attributes 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 when you set searchable attributes at the same level, they all 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?