Algolia lets you create categories based on specific attributes so you can filter search results on them. For example, if you have an index of books, you may want to categorize them by author and genre. This way, your end users can filter on their favorite author or discover a new genre.
To do so, you first need to declare attributes genre
and author
as attributesForFaceting
.
Using the API
To make genre
and author
attributes available for both faceting and filtering, you need to apply the following setting:
1
2
3
4
5
6
| $index->setSettings([
'attributesForFaceting' => [
"category",
"author"
]
]);
|
1
2
3
4
5
6
| index.set_settings({
attributesForFaceting: [
'category',
'author'
]
})
|
1
2
3
4
5
6
7
8
| index.setSettings({
attributesForFaceting: [
'category',
'author'
]
}).then(() => {
// done
});
|
1
2
3
4
5
6
| index.set_settings({
'attributesForFaceting': [
'category',
'author'
]
})
|
1
2
3
4
5
6
7
8
9
10
11
| let settings = Settings()
.set(\.attributesForFaceting, to: [
"category",
"author",
])
index.setSettings(settings) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| IndexSettings settings = new IndexSettings
{
AttributesForFaceting = new List<string>
{
"category",
"author"
}
};
index.SetSettings(settings);
// Asynchronous
await index.SetSettingsAsync(settings);
|
1
2
3
4
5
6
| index.setSettings(
new IndexSettings().setAttributesForFaceting(Arrays.asList(
"category",
"author"
))
);
|
1
2
3
4
5
6
| res, err := index.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting(
"category",
"author"
),
})
|
1
2
3
4
5
6
7
8
| client.execute {
changeSettings of "myIndex" `with` IndexSettings(
attributesForFaceting = Some(Seq(
"author",
"category",
))
)
}
|
1
2
3
4
5
6
7
8
| val settings = settings {
attributesForFaceting {
+"category"
+"author"
}
}
index.setSettings(settings)
|
In some cases, you may have many facet values. In our example, if you have many books in your index, you may also have plenty of different authors. The engine can’t return more than 1000 values per facet, so if you have more, you may want to let your end users search into them. You can achieve this by leveraging the searchable
modifier.
1
2
3
4
5
6
| $index->setSettings([
'attributesForFaceting' => [
"category",
"searchable(author)"
]
]);
|
1
2
3
4
5
6
| index.set_settings({
attributesForFaceting: [
'category',
'searchable(author)'
]
})
|
1
2
3
4
5
6
7
8
| index.setSettings({
attributesForFaceting: [
'category',
'searchable(author)'
]
}).then(() => {
// done
});
|
1
2
3
4
5
6
| index.set_settings({
'attributesForFaceting': [
'category',
'searchable(author)'
]
})
|
1
2
3
4
5
6
7
8
9
10
11
| let settings = Settings()
.set(\.attributesForFaceting, to: [
"category",
.searchable("author"),
])
index.setSettings(settings) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| IndexSettings settings = new IndexSettings
{
AttributesForFaceting = new List<string>
{
"category",
"searchable(author)"
}
};
index.SetSettings(settings);
// Asynchronous
await index.SetSettingsAsync(settings);
|
1
2
3
4
5
6
| index.setSettings(
new IndexSettings().setAttributesForFaceting(Arrays.asList(
"category",
"searchable(author)"
))
);
|
1
2
3
4
5
6
| res, err := index.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting(
"category",
"searchable(author)"
),
})
|
1
2
3
4
5
6
7
8
| client.execute {
changeSettings of "myIndex" `with` IndexSettings(
attributesForFaceting = Some(Seq(
"category",
"searchable(author)",
))
)
}
|
1
2
3
4
5
6
7
8
| val settings = settings {
attributesForFaceting {
+"category"
+Searchable("author")
}
}
index.setSettings(settings)
|
If you only need the filtering feature, you can take advantage of filterOnly
which reduces the index size and improves the speed of the search. Imagine you want to automatically filter what genre the end users can search into based on what section of your website they’re on, without displaying genres as clickable filters. In this case, you only need filtering capabilities.
1
2
3
4
5
6
| $index->setSettings([
'attributesForFaceting' => [
"filterOnly(category)",
"author"
]
]);
|
1
2
3
4
5
6
| index.set_settings({
attributesForFaceting: [
'filterOnly(category)',
'author'
]
})
|
1
2
3
4
5
6
7
8
| index.setSettings({
attributesForFaceting: [
'filterOnly(category)',
'author'
]
}).then(() => {
// done
});
|
1
2
3
4
5
6
| index.set_settings({
'attributesForFaceting': [
'filterOnly(category)',
'author'
]
})
|
1
2
3
4
5
6
7
8
9
10
11
| let settings = Settings()
.set(\.attributesForFaceting, to: [
.filterOnly("category"),
"author",
])
index.setSettings(settings) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| IndexSettings settings = new IndexSettings
{
AttributesForFaceting = new List<string>
{
"filterOnly(category)",
"author"
}
};
index.SetSettings(settings);
// Asynchronous
await index.SetSettingsAsync(settings);
|
1
2
3
4
5
6
| index.setSettings(
new IndexSettings().setAttributesForFaceting(Arrays.asList(
"filterOnly(category)",
"author"
))
);
|
1
2
3
4
5
6
| res, err := index.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting(
"filterOnly(category)",
"author"
),
})
|
1
2
3
4
5
6
7
8
| client.execute {
changeSettings of "myIndex" `with` IndexSettings(
attributesForFaceting = Some(Seq(
"filterOnly(category)",
"author",
))
)
}
|
1
2
3
4
5
6
7
8
| val settings = settings {
attributesForFaceting {
+FilterOnly("category")
+"author"
}
}
index.setSettings(settings)
|
Make sure not to include colons (:
) in attribute names that you want to use for faceting, because the filters
syntax relies on that character as a delimiter.
Using the dashboard
- Select the Search product icon on your dashboard and then select your index.
- Click the Configuration tab.
- Under the Filtering and Faceting category, click on Facets.
- In the Attributes for faceting section, click on Add an Attribute and select the attribute you wish to declare for faceting.
- For each attribute, click the dropdown on the right and set them as “searchable”, “filter only” or “not searchable”.
- Don’t forget to save your changes.