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"
]
]);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # `title` and `comments` have the same priority
index.set_settings({
searchableAttributes: [
'title,comments',
'ingredients'
]
})
# `title` has the highest priority, then `ingredients`, then `comments`
index.set_settings({
searchableAttributes: [
'title',
'ingredients',
'comments'
]
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| // `title` and `comments` have the same priority
index.setSettings({
searchableAttributes: [
'title,comments',
'ingredients'
]
}).then(() => {
// done
});
// `title` has the highest priority, then `ingredients`, then `comments`
index.setSettings({
searchableAttributes: [
'title',
'ingredients',
'comments'
]
}).then(() => {
// done
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # `title` and `comments` have the same priority
index.set_settings({
'searchableAttributes': [
'title,comments',
'ingredients'
]
})
# `title` has the highest priority, then `ingredients`, then `comments`
index.set_settings({
'searchableAttributes': [
'title',
'ingredients',
'comments'
]
})
|
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"
]
])
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| // `title` and `comments` have the same priority
index.setSettings(
new JSONObject().put(
"searchableAttributes",
new JSONArray()
.put("title,comments")
.put("ingredients")
)
);
// `title` has the highest priority, then `ingredients`, then `comments`
index.setSettings(
new JSONObject().put(
"searchableAttributes",
new JSONArray()
.put("title")
.put("comments")
.put("ingredients")
)
);
|
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
| // `title` and `comments` have the same priority
IndexSettings settings = new IndexSettings
{
SearchableAttributes = new List<string>
{
"title,comments",
"ingredients"
}
};
// `title` has the highest priority, then `ingredients`, then `comments`
IndexSettings settings = new IndexSettings
{
SearchableAttributes = new List<string>
{
"title",
"comments",
"ingredients"
}
};
index.SetSettings(settings);
// Asynchronous
await index.SetSettingsAsync(settings);
|
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(
new IndexSettings().setSearchableAttributes(Arrays.asList(
"title,comments",
"ingredients"
))
);
// `title` has the highest priority, then `ingredients`, then `comments`
index.setSettings(
new IndexSettings().setSearchableAttributes(Arrays.asList(
"title",
"comments",
"ingredients"
))
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| // `title` and `comments` have the same priority
res, err := index.SetSettings(search.Settings{
SearchableAttributes: opt.SearchableAttributes(
"title,comments",
"ingredients",
),
})
// `title` has the highest priority, then `ingredients`, then `comments`
res, err := index.SetSettings(search.Settings{
SearchableAttributes: opt.SearchableAttributes(
"title",
"comments",
"ingredients",
),
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| // `title` and `comments` have the same priority
client.execute {
changeSettings of "myIndex" `with` IndexSettings(
searchableAttributes = Some(Seq(
SearchableAttributes.attributes("title", "comments")
SearchableAttributes.attribute("ingredients")
))
)
}
// `title` has the highest priority, then `ingredients`, then `comments`
client.execute {
changeSettings of "myIndex" `with` IndexSettings(
searchableAttributes = Some(Seq(
SearchableAttributes.attributes("title")
SearchableAttributes.attribute("ingredients")
SearchableAttributes.unordered("comments")
))
)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| // "title" and "comments" have the same priority
var settings = settings {
searchableAttributes {
+"title,comments"
+"ingredients"
}
}
// "title" has the highest priority, then "ingredients", then "comments"
settings = settings {
searchableAttributes {
+"title"
+"ingredients"
+"comments"
}
}
index.setSettings(settings)
|
Using the Algolia 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 (for example, “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.