🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Sending and managing data / Prepare your records for indexing

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

Consider a website to find recipes with the following 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
[
  {
    "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.

Set searchable attributes in the Algolia dashboard

  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, select Searchable attributes.
  5. Click Add a Searchable Attribute and type or select an attribute from the list.
  6. You can change the level of importance of attributes by dragging them up or down in the Searchable attributes list. To make two attributes equally important, when you add a new attribute, type the attributes directly as a comma-separated list—for example, title, comments.
  7. Save your changes.

Set searchable attributes with the API

To make some attributes searchable, you need to include them in the searchableAttributes setting, when you configure your index with the setSettings method.

Matches within attributes that are higher in this list rank first. If you want two attributes to have the same priority, include them on the same level, separated by a comma.

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",
    "ingredients",
    "comments"
  ]
]);

In the first example, title and comments have the same priority. In the second example, matches in the title attribute rank before matches in the comments attribute.

Did you find this page helpful?