Algolia lets you create categories based on specific attributes so users can filter search results by those categories. For example, if you have an index of books, you could categorize them by author and genre. This allows users to filter search results by their favorite author or discover new genres.
To enable this categorization, first declare the attributes genre
and author
as attributesForFaceting
:
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
val settings = settings {
attributesForFaceting {
+ "category"
+ "author"
}
}
index . setSettings ( settings )
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 {
setSettings of "myIndex" `with` IndexSettings (
attributesForFaceting = Some ( Seq (
"author" ,
"category" ,
))
)
}
Sometimes, you may have many facet values. For instance, if you have a book index, you may also have a lot of different authors. The engine can’t return more than 1,000 values per facet , so if you have more than that, you may want to let your users search for them. Do this by using 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
val settings = settings {
attributesForFaceting {
+ "category"
+ Searchable ( "author" )
}
}
index . setSettings ( settings )
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 {
setSettings of "myIndex" `with` IndexSettings (
attributesForFaceting = Some ( Seq (
"category" ,
"searchable(author)" ,
))
)
}
If you only need the filtering feature, use filterOnly
to reduce the index size and improve search speed. For example, you could automatically filter what genre the users can search based on the section of your website they’re on(without displaying genre as a clickable filter).
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
val settings = settings {
attributesForFaceting {
+ FilterOnly ( "category" )
+ "author"
}
}
index . setSettings ( settings )
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 {
setSettings of "myIndex" `with` IndexSettings (
attributesForFaceting = Some ( Seq (
"filterOnly(category)" ,
"author" ,
))
)
}
Don’t include colons (:
) in attribute names that you want to use for faceting because the filters
syntax relies on that character as a delimiter.
See also
© Algolia · Privacy Policy · Cookie settings