|
@@ -1,5 +1,5 @@
|
|
|
-[[request-body-search-sort]]
|
|
|
-==== Sort
|
|
|
+[[sort-search-results]]
|
|
|
+=== Sort search results
|
|
|
|
|
|
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
|
|
@@ -48,12 +48,12 @@ NOTE: `_doc` has no real use-case besides being the most efficient sort order.
|
|
|
So if you don't care about the order in which documents are returned, then you
|
|
|
should sort by `_doc`. This especially helps when <<request-body-search-scroll,scrolling>>.
|
|
|
|
|
|
-===== Sort Values
|
|
|
+==== Sort Values
|
|
|
|
|
|
The sort values for each document returned are also returned as part of
|
|
|
the response.
|
|
|
|
|
|
-===== Sort Order
|
|
|
+==== Sort Order
|
|
|
|
|
|
The `order` option can have the following values:
|
|
|
|
|
@@ -64,7 +64,7 @@ The `order` option can have the following values:
|
|
|
The order defaults to `desc` when sorting on the `_score`, and defaults
|
|
|
to `asc` when sorting on anything else.
|
|
|
|
|
|
-===== Sort mode option
|
|
|
+==== Sort mode option
|
|
|
|
|
|
Elasticsearch supports sorting by array or multi-valued fields. The `mode` option
|
|
|
controls what array value is picked for sorting the document it belongs
|
|
@@ -84,7 +84,7 @@ 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 usage
|
|
|
+===== Sort mode example usage
|
|
|
|
|
|
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
|
|
@@ -109,7 +109,7 @@ POST /_search
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-===== Sorting numeric fields
|
|
|
+==== Sorting numeric fields
|
|
|
|
|
|
For numeric fields it is also possible to cast the values from one type
|
|
|
to another using the `numeric_type` option.
|
|
@@ -227,7 +227,7 @@ To avoid overflow, the conversion to `date_nanos` cannot be applied on dates bef
|
|
|
1970 and after 2262 as nanoseconds are represented as longs.
|
|
|
|
|
|
[[nested-sorting]]
|
|
|
-===== Sorting within nested objects.
|
|
|
+==== Sorting within nested objects.
|
|
|
|
|
|
Elasticsearch also supports sorting by
|
|
|
fields that are inside one or more nested objects. The sorting by nested
|
|
@@ -253,7 +253,7 @@ field support has a `nested` sort option with the following properties:
|
|
|
NOTE: Elasticsearch will throw an error if a nested field is defined in a sort without
|
|
|
a `nested` context.
|
|
|
|
|
|
-====== Nested sorting examples
|
|
|
+===== Nested sorting examples
|
|
|
|
|
|
In the below example `offer` is a field of type `nested`.
|
|
|
The nested `path` needs to be specified; otherwise, Elasticsearch doesn't know on what nested level sort values need to be captured.
|
|
@@ -331,7 +331,7 @@ POST /_search
|
|
|
Nested sorting is also supported when sorting by
|
|
|
scripts and sorting by geo distance.
|
|
|
|
|
|
-===== Missing Values
|
|
|
+==== Missing Values
|
|
|
|
|
|
The `missing` parameter specifies how docs which are missing
|
|
|
the sort field should be treated: The `missing` value can be
|
|
@@ -357,7 +357,7 @@ GET /_search
|
|
|
NOTE: If a nested inner object doesn't match with
|
|
|
the `nested.filter` then a missing value is used.
|
|
|
|
|
|
-===== Ignoring Unmapped Fields
|
|
|
+==== Ignoring Unmapped Fields
|
|
|
|
|
|
By default, the search request will fail if there is no mapping
|
|
|
associated with a field. The `unmapped_type` option allows you to ignore
|
|
@@ -383,7 +383,7 @@ 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.
|
|
|
|
|
|
[[geo-sorting]]
|
|
|
-===== Geo Distance Sorting
|
|
|
+==== Geo Distance Sorting
|
|
|
|
|
|
Allow to sort by `_geo_distance`. Here is an example, assuming `pin.location` is a field of type `geo_point`:
|
|
|
|
|
@@ -438,7 +438,7 @@ have values for the field that is used for distance computation.
|
|
|
|
|
|
The following formats are supported in providing the coordinates:
|
|
|
|
|
|
-====== Lat Lon as Properties
|
|
|
+===== Lat Lon as Properties
|
|
|
|
|
|
[source,console]
|
|
|
--------------------------------------------------
|
|
@@ -462,7 +462,7 @@ GET /_search
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-====== Lat Lon as String
|
|
|
+===== Lat Lon as String
|
|
|
|
|
|
Format in `lat,lon`.
|
|
|
|
|
@@ -485,7 +485,7 @@ GET /_search
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-====== Geohash
|
|
|
+===== Geohash
|
|
|
|
|
|
[source,console]
|
|
|
--------------------------------------------------
|
|
@@ -506,7 +506,7 @@ GET /_search
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-====== Lat Lon as Array
|
|
|
+===== Lat Lon as Array
|
|
|
|
|
|
Format in `[lon, lat]`, note, the order of lon/lat here in order to
|
|
|
conform with http://geojson.org/[GeoJSON].
|
|
@@ -531,7 +531,7 @@ GET /_search
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
-===== Multiple reference points
|
|
|
+==== Multiple reference points
|
|
|
|
|
|
Multiple geo points can be passed as an array containing any `geo_point` format, for example
|
|
|
|
|
@@ -560,7 +560,7 @@ The final distance for a document will then be `min`/`max`/`avg` (defined via `m
|
|
|
|
|
|
|
|
|
|
|
|
-===== Script Based Sorting
|
|
|
+==== Script Based Sorting
|
|
|
|
|
|
Allow to sort based on custom scripts, here is an example:
|
|
|
|
|
@@ -588,7 +588,7 @@ GET /_search
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
-===== Track Scores
|
|
|
+==== Track Scores
|
|
|
|
|
|
When sorting on a field, scores are not computed. By setting
|
|
|
`track_scores` to true, scores will still be computed and tracked.
|
|
@@ -609,7 +609,7 @@ GET /_search
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
|
|
|
-===== Memory Considerations
|
|
|
+==== Memory Considerations
|
|
|
|
|
|
When sorting, the relevant sorted field values are loaded into memory.
|
|
|
This means that per shard, there should be enough memory to contain
|