Allows you to add one or more sorts on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score, and _doc to sort by index order.
Assuming the following index mapping:
_doc has no real use-case besides being the most efficient sort order. So if you dont care about the order in which documents are returned, then you should sort by _doc. This especially helps when scrolling.
The sort values for each document returned are also returned as part of the response.
The order option can have the following values:
asc | Sort in ascending order |
desc | Sort in descending order |
The order defaults to desc when sorting on the _score, and defaults to asc when sorting on anything else.
Elasticsearch supports sorting by array or multi-valued fields. The mode option controls what array value is picked for sorting the document it belongs to. The mode option can have the following values:
min | Pick the lowest value. |
max | Pick the highest value. |
sum | Use the sum of all values as sort value. Only applicable for number based array fields. |
avg | Use the average of all values as sort value. Only applicable for number based array fields. |
median | Use the median of all values as sort value. Only applicable for number based array fields. |
The default sort mode in the ascending sort order is min the lowest value is picked. The default sort mode in the descending order is max the highest value is picked.
Sort mode example usageedit
In the example below the field price has multiple prices per document. In this case the result hits will be sorted by price ascending based on the average price per document.
Sorting within nested objects.edit
Elasticsearch also supports sorting by fields that are inside one or more nested objects. The sorting by nested field support has a nested sort option with the following properties:
Nested sort options before Elasticseach 6.1
The nested_path and nested_filter options have been deprecated in favor of the options documented above.
Nested sorting examplesedit
In the below example offer is a field of type nested. The nested path needs to be specified; otherwise, Elasticsearch doesnt know on what nested level sort values need to be captured.
In the below example parent and child fields are of type nested. The nested_path needs to be specified at each level; otherwise, Elasticsearch doesnt know on what nested level sort values need to be captured.
Nested sorting is also supported when sorting by scripts and sorting by geo distance.
The missing parameter specifies how docs which are missing the sort field should be treated: The missing value can be set to _last, _first, or a custom value [that will be used for missing docs as the sort value]. The default is _last.
For example:
If a nested inner object doesnt match with the nested_filter then a missing value is used.
Ignoring Unmapped Fieldsedit
By default, the search request will fail if there is no mapping associated with a field. The unmapped_type option allows you to ignore fields that have no mapping and not sort by them. The value of this parameter is used to determine what sort values to emit. Here is an example of how it can be used:
If any of the indices that are queried doesnt have a mapping for price then Elasticsearch will handle it as if there was a mapping of type long, with all documents in this index having no value for this field.
Allow to sort by _geo_distance. Here is an example, assuming pin.location is a field of type geo_point:
geo distance sorting does not support configurable missing values: the distance will always be considered equal to Infinity when a document does not have values for the field that is used for distance computation.
The following formats are supported in providing the coordinates:
Lat Lon as Propertiesedit
Format in lat,lon.
Format in [lon, lat], note, the order of lon/lat here in order to conform with GeoJSON.
Multiple reference pointsedit
Multiple geo points can be passed as an array containing any geo_point format, for example
and so forth.
The final distance for a document will then be min/max/avg [defined via mode] distance of all points contained in the document to all points given in the sort request.
Allow to sort based on custom scripts, here is an example:
When sorting on a field, scores are not computed. By setting track_scores to true, scores will still be computed and tracked.
Memory Considerationsedit
When sorting, the relevant sorted field values are loaded into memory. This means that per shard, there should be enough memory to contain them. For string based types, the field sorted on should not be analyzed / tokenized. For numeric types, if possible, it is recommended to explicitly set the type to narrower types [like short, integer and float].