To sort search results in Elasticsearch, add a sort array to the _search request and name the field and direction, for example { "price": "asc" }. By default Elasticsearch sorts by relevance score (_score) in descending order, and once you give an explicit sort it orders by your fields instead. The elasticsearch sort option works on numeric, date, keyword and boolean fields, but not on analysed text fields.
This guide covers single and multi-field sorting, sorting arrays, missing values and the common mistakes.
Assume an index products with name (text with a keyword sub-field), price (float) and created_at (date).
The order is asc or desc. You can use the short form { "price": "asc" } too. Each hit in the response has a sort array with the value used, which is also what you need for deep pagination with search_after.
When you sort by a field, the _score is not computed unless you ask for it. Set "track_scores": true if you still need the score in the response.
Provide several entries in the array. Elasticsearch uses the next one only to break ties in the previous one:
Here results are ranked by relevance first, then by lowest price, then by newest. The special name _score lets you mix relevance with field sorting. Another special value is _doc, which means index order and is the cheapest sort when you do not care about order, for example when exporting.
A text field is analysed into separate tokens, so sorting on it fails or gives meaningless results. Elasticsearch rejects it by default because fielddata is disabled for text fields. Sort on a keyword version instead. With dynamic mapping, a string field gets a .keyword sub-field automatically:
For better control define the mapping yourself:
Keyword sorting is lexicographic by bytes, so uppercase letters sort before lowercase ones. If you need case-insensitive alphabetical order, use a keyword field with a lowercase normalizer, or an ICU collation keyword field from the ICU analysis plugin for language-aware order.
Avoid turning on fielddata for text fields just to sort. It loads all terms into heap memory and can hurt the cluster.
If some documents lack the field, they sort last by default. Use missing to change that:
You can also give a default value instead of _first or _last.
When a field holds multiple values (an array), you choose which one counts with mode: min, max, avg, sum or median. The default is min for ascending and max for descending sorts.
Sorting combines with from and size, but deep offsets are expensive because each shard has to collect and sort from + size hits. By default Elasticsearch limits from + size to 10,000 (setting index.max_result_window). For deep paging, use search_after with the sort values of the last hit, and add a unique tiebreaker field so the order is stable:
For search_after over changing data, pair it with a point in time (PIT) so the results are consistent between requests. A tiebreaker based on _id can be costly on large indices; a dedicated unique field is often better.
text field instead of its keyword sub-field._score ordering after adding a sort without _score in it.from values rather than search_after.long in one and keyword in another. Use unmapped_type to handle indices where the field does not exist.Results are sorted by _score descending when the query computes scores. For queries that do not score (such as filters only), the order is effectively the index order.
Text fields are analysed and have no doc values, so they cannot be sorted. Sort on a keyword sub-field such as name.keyword.
Use the date field name with asc or desc, for example { "created_at": "desc" }. Dates are stored as milliseconds (or nanoseconds for date_nanos) internally, so sorting is numeric.
Yes, with a _script sort, but it runs per document and is slower than sorting on an indexed field.
Add a sort array to your search to order hits by one or more fields, with _score and _doc as special values. Sort on keyword, numeric or date fields, never on analysed text, and control missing values and arrays with missing and mode. For deep pagination use search_after with a unique tiebreaker instead of large from offsets.