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 remove the noise, you just 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 rank your searchable attributes, making some more relevant than others.

Dataset

Imagine you’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. Here, this means that when users search, the engine also searches into attributes like images, which you just need 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 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 the example, 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 Algolia dashboard

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

  1. Go to your dashboard and select your index.
  2. Click the Configuration tab.
  3. In the Searchable Attributes section, click the Add a Searchable Attribute button.
  4. You can change the level of importance of attributes by dragging them up or down on the page. To assign the same level of importance, when you create a new attribute, type them directly in the input field as a comma-separated list (for example, “title, comments”).
  5. Don’t forget to save your changes.
Did you find this page helpful?