How to Use Index Sorting for Performance in Elasticsearch
Use
index.sort in your Elasticsearch index settings to pre-sort documents by one or more fields. This helps Elasticsearch quickly return sorted results without extra sorting at query time, improving performance especially for large datasets.Syntax
The index.sort setting defines how documents are sorted when indexed. It includes:
- fields: An array of fields to sort by, in order of priority.
- order: Sort direction, either
asc(ascending) ordesc(descending).
This sorting is applied at index time, so queries that sort on these fields run faster.
json
{
"settings": {
"index": {
"sort": {
"fields": ["field1", "field2"],
"order": ["asc", "desc"]
}
}
}
}Example
This example creates an index with index sorting on timestamp ascending and user_id descending. Queries sorting by these fields will be faster.
json
PUT /my-index
{
"settings": {
"index": {
"sort": {
"fields": ["timestamp", "user_id"],
"order": ["asc", "desc"]
}
}
},
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"user_id": { "type": "keyword" },
"message": { "type": "text" }
}
}
}Output
{"acknowledged":true,"shards_acknowledged":true,"index":"my-index"}
Common Pitfalls
1. Index sorting cannot be changed after index creation. You must define it when creating the index.
2. Sorting on fields not defined in index.sort.fields does not benefit from index sorting. Queries sorting on other fields still require runtime sorting.
3. Using index sorting on high-cardinality fields may increase indexing time and disk usage. Choose fields wisely.
json
PUT /wrong-index
{
"settings": {
"index": {
"sort": {
"fields": ["nonexistent_field"],
"order": ["asc"]
}
}
}
}
# Correct approach:
PUT /correct-index
{
"settings": {
"index": {
"sort": {
"fields": ["timestamp"],
"order": ["asc"]
}
}
}
}Output
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Field [nonexistent_field] is not mapped"}],"type":"illegal_argument_exception","reason":"Field [nonexistent_field] is not mapped"}}
Quick Reference
| Setting | Description | Notes |
|---|---|---|
| index.sort.fields | Fields to sort by at index time | Must be mapped fields |
| index.sort.order | Sort order for each field | Use 'asc' or 'desc' |
| Immutable | Index sorting cannot be changed after creation | Plan before indexing |
| Performance | Speeds up queries sorting on these fields | Best for frequent sort queries |
| Limitations | Increases indexing cost and disk usage | Avoid high-cardinality fields |
Key Takeaways
Define
index.sort fields and order when creating the index to improve sorted query speed.Index sorting only helps queries sorting on the specified fields; other sorts still require runtime sorting.
You cannot change index sorting after index creation, so plan your sorting fields carefully.
Using index sorting may increase indexing time and disk space, especially with many unique values.
Choose fields with moderate cardinality and frequent sort usage for best performance benefits.