Having constantly seen data sorted alphabetically, you may find yourself wanting to follow suit. However, while sorting alphabetically makes sense for a database, it doesn’t for a search engine and we strongly discourage it. This isn’t to say your use case is invalid; rather, we want to encourage caution and due consideration.
When you sort your data alphabetically, you don’t only devalue Algolia’s tie-breaking algorithm: you essentially disable it. If you sort alphabetically first, then the only objects requiring tie-breaking are those with the exact same name: there will likely be few objects that fit this criterea.
Alphabetically structured results lead users to search through data by hand, while a search engine, like Algolia, should ensure that they don’t need to.
Warnings aside, it is still possible to sort alphabetically. However, because the attributes you use for sorting must be numeric or boolean values, sorting alphabetically is a non-trivial problem and can only be implemented through the API.
Using the API
To sort an index alphabetically, you need to modify its custom ranking, and set custom
as the index’s first ranking criterion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $index->setSettings([
'customRanking' => [
'asc(textual_attribute)'
],
'ranking' => [
'custom',
'typo',
'geo',
'words',
'filters',
'proximity',
'attribute',
'exact'
]
]);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| index.set_settings({
customRanking: [
'asc(textual_attribute)'
],
ranking: [
'custom'
'typo',
'geo',
'words',
'filters',
'proximity',
'attribute',
'exact'
]
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| index.setSettings({
customRanking: [
'asc(textual_attribute)'
],
ranking: [
'custom',
'typo',
'geo',
'words',
'filters',
'proximity',
'attribute',
'exact'
]
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| index.set_settings({
'customRanking': [
'asc(textual_attribute)'
],
'ranking': [
'custom',
'typo',
'geo',
'words',
'filters',
'proximity',
'attribute',
'exact'
]
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| index.setSettings([
"customRanking": [
"asc(textual_attribute)"
],
"ranking": [
"custom",
"typo",
"geo",
"words",
"filters",
"proximity",
"attribute",
"exact"
]
])
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| index.setSettings(
new JSONObject().put(
"customRanking",
new JSONArray()
.put("asc(textual_attribute)")
),
new JSONObject().put(
"ranking",
new JSONArray()
.put("articles_date_desc")
.put("typo")
.put("geo")
.put("words")
.put("filters")
.put("proximity")
.put("attribute")
.put("exact")
)
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| IndexSettings settings = new IndexSettings
{
customRanking = new List<string>
{
"asc(textual_attribute)"
}
ranking = new List<string>
{
"custom",
"typo",
"geo",
"words",
"filters",
"proximity",
"attribute",
"exact"
}
};
index.SetSettings(settings);
// Asynchronous
await index.SetSettingsAsync(settings);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| index.setSettings(
new IndexSettings()
.setCustomRanking(
Arrays.asList(
"asc(textual_attribute)"
)
)
.setRanking(
Arrays.asList(
"typo",
"geo",
"words",
"filters",
"proximity",
"attribute",
"exact",
"custom"
)
)
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| res, err := index.SetSettings(search.Settings{
CustomRanking: opt.CustomRanking(
"asc(textual_attribute",
),
Ranking: opt.Ranking(
"custom",
"typo",
"geo",
"words",
"filters",
"proximity",
"attribute",
"exact",
),
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| client.execute {
setSettings of "index" `with` IndexSettings(
customRanking = Some(Seq(
CustomRanking.asc("textual_attribute"),
)),
ranking = Some(Seq(
Ranking.custom,
Ranking.typo,
Ranking.geo,
Ranking.words,
Ranking.filters,
Ranking.proximity,
Ranking.attribute,
Ranking.exact
)),
)
}
|
Caveats
Algolia is not locale-aware. Strings are ordered by lexicographical order of their Unicode characters. That may be acceptable for English text, less so for other languages (especially those with diacritics).