https://www.elastic.co/guide/en/elasticsearch/reference/7.8/sort-search-results.html
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.
允许您在特定字段上添加一种或多种排序。每种类型也可以颠倒。排序是在每个字段级别上定义的,具有特殊的字段名称,_score
用于按分数_doc
排序和按索引顺序排序。
Assuming the following index mapping:
假设以下索引映射
PUT /my-index-000001
{
"mappings": {
"properties": {
"post_date": { "type": "date" },
"user": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"age": { "type": "integer" }
}
}
}
GET /my-index-000001/_search
{
"sort" : [
{ "post_date" : {"order" : "asc"}},
"user",
{ "name" : "desc" },
{ "age" : "desc" },
"_score"
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
_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 scrolling.
_doc
除了是最有效的排序顺序之外,没有实际用例。因此,如果您不关心文档的返回顺序,则应按排序_doc
。这在滚动时特别有用。
Sort Values
The sort values for each document returned are also returned as part of the response.
返回的每个文档的排序值也作为响应的一部分返回。
Sort Order
The order
option can have the following values:
该order
选项可以具有以下值:
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.
该顺序默认为desc
在上排序时_score
,默认为asc
在其他任何东西上排序时。
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 to. The mode
option can have the following values:
Elasticsearch支持按数组或多值字段排序。该mode
选项控制选择哪个数组值对它所属的文档进行排序。该mode
选项可以具有以下值:
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.
升序排列的默认排序模式是min
-选择最低值。降序排列的默认排序模式是max
-选择最高值。
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 the average price per document.
在下面的示例中,字段价格每个文档具有多个价格。在这种情况下,结果命中将根据每个文档的平均价格按价格升序排序。
PUT /my-index-000001/_doc/1?refresh
{
"product": "chocolate",
"price": [20, 4]
}
POST /_search
{
"query" : {
"term" : { "product" : "chocolate" }
},
"sort" : [
{"price" : {"order" : "asc", "mode" : "avg"}}
]
}
Sorting numeric fields
For numeric fields it is also possible to cast the values from one type to another using the numeric_type
option. This option accepts the following values: ["double", "long", "date", "date_nanos"
] and can be useful for cross-index search if the sort field is mapped differently on some indices.
对于数字字段,也可以使用该numeric_type
选项将值从一种类型转换为另一种类型。此选项接受以下值:[ "double", "long", "date", "date_nanos"
],如果排序字段在某些索引上的映射方式不同,则对交叉索引搜索很有用。
Consider for instance these two indices:
例如,考虑以下两个索引:
PUT /index_double
{
"mappings": {
"properties": {
"field": { "type": "double" }
}
}
}
PUT /index_long
{
"mappings": {
"properties": {
"field": { "type": "long" }
}
}
}
Since field
is mapped as a double
in the first index and as a long
in the second index, it is not possible to use this field to sort requests that query both indices by default. However you can force the type to one or the other with the numeric_type
option in order to force a specific type for all indices:
由于在第一个索引中field
映射为a double
,在第二个索引中映射为a long
,因此默认情况下无法使用此字段对查询两个索引的请求进行排序。但是,您可以使用numeric_type
选项将类型强制为一个或另一个,以便对所有索引强制使用特定类型:
POST /index_long,index_double/_search
{
"sort" : [
{
"field" : {
"numeric_type" : "double"
}
}
]
}
In the example above, values for the index_long
index are casted to a double in order to be compatible with the values produced by the index_double
index. It is also possible to transform a floating point field into a long
but note that in this case floating points are replaced by the largest value that is less than or equal (greater than or equal if the value is negative) to the argument and is equal to a mathematical integer.
在上面的示例中,index_long
索引的值被强制转换为双精度,以便与index_double
索引产生的值兼容 。也可以将浮点字段转换为a,long
但请注意,在这种情况下,浮点数将替换为小于等于参数(等于或等于负)的最大值。一个数学整数。
This option can also be used to convert a date
field that uses millisecond resolution to a date_nanos
field with nanosecond resolution. Consider for instance these two indices:
此选项还可以用于将date
使用毫秒分辨率的date_nanos
字段转换为具有纳秒分辨率的字段。例如,考虑以下两个索引:
PUT /index_double
{
"mappings": {
"properties": {
"field": { "type": "date" }
}
}
}
PUT /index_long
{
"mappings": {
"properties": {
"field": { "type": "date_nanos" }
}
}
}
Values in these indices are stored with different resolutions so sorting on these fields will always sort the date
before the date_nanos
(ascending order). With the numeric_type
type option it is possible to set a single resolution for the sort, setting to date
will convert the date_nanos
to the millisecond resolution while date_nanos
will convert the values in the date
field to the nanoseconds resolution:
这些索引中的值以不同的分辨率存储,因此在这些字段上进行排序将始终对date
前date_nanos
(升序)进行排序。使用numeric_type
type选项,可以为排序设置单个分辨率,设置为date
将转换date_nanos
为毫秒分辨率,而date_nanos
将date
字段中的值转换为纳秒分辨率:
POST /index_long,index_double/_search
{
"sort" : [
{
"field" : {
"numeric_type" : "date_nanos"
}
}
]
}
To avoid overflow, the conversion to date_nanos
cannot be applied on dates before 1970 and after 2262 as nanoseconds are represented as longs.
为避免溢出,到date_nanos
1970年之前和2262年之后的日期不能应用转换,因为纳秒表示为long。
Sorting within nested objects.
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:
Elasticsearch还支持按一个或多个嵌套对象内部的字段排序。“按嵌套字段nested
排序” 支持具有以下属性的sort选项:
-
path
Defines on which nested object to sort. The actual sort field must be a direct field inside this nested object. When sorting by nested field, this field is mandatory.
定义要在哪个嵌套对象上排序。实际的排序字段必须是此嵌套对象内的直接字段。当按嵌套字段排序时,此字段是必填字段。
-
filter
A filter that the inner objects inside the nested path should match with in order for its field values to be taken into account by sorting. Common case is to repeat the query / filter inside the nested filter or query. By default no
nested_filter
is active.嵌套路径内的内部对象应与其匹配的过滤器,以便通过排序将其字段值考虑在内。常见的情况是在嵌套的过滤器或查询中重复查询/过滤器。默认情况下,no
nested_filter
为活动状态。 -
max_children
The maximum number of children to consider per root document when picking the sort value. Defaults to unlimited.
选择排序值时,每个根文档要考虑的最大子级数。默认为无限制。
-
nested
Same as top-level
nested
but applies to another nested path within the current nested object.与顶层相同,
nested
但适用于当前嵌套对象内的另一个嵌套路径。
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.
在下面的示例中offer
是type字段nested
。path
需要指定嵌套;否则,Elasticsearch不知道需要捕获什么嵌套级别的排序值。
POST /_search
{
"query" : {
"term" : { "product" : "chocolate" }
},
"sort" : [
{
"offer.price" : {
"mode" : "avg",
"order" : "asc",
"nested": {
"path": "offer",
"filter": {
"term" : { "offer.color" : "blue" }
}
}
}
}
]
}
In the below example parent
and child
fields are of type nested
. The nested_path
needs to be specified at each level; otherwise, Elasticsearch doesn’t know on what nested level sort values need to be captured.
在下面的示例中parent
,child
字段的类型为nested
。的nested_path
需要在每个级别指定; 否则,Elasticsearch不知道需要捕获什么嵌套级别的排序值。
POST /_search
{
"query": {
"nested": {
"path": "parent",
"query": {
"bool": {
"must": {"range": {"parent.age": {"gte": 21}}},
"filter": {
"nested": {
"path": "parent.child",
"query": {"match": {"parent.child.name": "matt"}}
}
}
}
}
}
},
"sort" : [
{
"parent.child.age" : {
"mode" : "min",
"order" : "asc",
"nested": {
"path": "parent",
"filter": {
"range": {"parent.age": {"gte": 21}}
},
"nested": {
"path": "parent.child",
"filter": {
"match": {"parent.child.name": "matt"}
}
}
}
}
}
]
}
Nested sorting is also supported when sorting by scripts and sorting by geo distance.
按脚本排序和按地理距离排序时,也支持嵌套排序。
Missing Valuesedit
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
.
该missing
参数指定如何被丢失排序字段文档应被视为:该missing
值可以被设置为_last
,_first
或自定义的值(将被用于缺少文档作为排序值)。默认值为_last
。
For example:
例如:
GET /_search
{
"sort" : [
{ "price" : {"missing" : "_last"} }
],
"query" : {
"term" : { "product" : "chocolate" }
}
}
If a nested inner object doesn’t match with the nested_filter
then a missing value is used.
如果嵌套的内部对象与不匹配,nested_filter
则使用缺失值。
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:
默认情况下,如果没有与字段关联的映射,搜索请求将失败。该unmapped_type
选项使您可以忽略没有映射的字段,也不能按它们排序。此参数的值用于确定要发出的排序值。这是一个如何使用它的示例:
GET /_search
{
"sort" : [
{ "price" : {"unmapped_type" : "long"} }
],
"query" : {
"term" : { "product" : "chocolate" }
}
}
If any of the indices that are queried doesn’t 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.
如果查询的任何索引都没有对应的映射,price
那么Elasticsearch会像处理type那样处理它 long
,该索引中的所有文档对此字段都没有值。
Geo Distance Sorting
Allow to sort by _geo_distance
. Here is an example, assuming pin.location
is a field of type geo_point
:
允许按排序_geo_distance
。这是一个示例,假设pin.location
是一个type字段geo_point
:
GET /_search
{
"sort" : [
{
"_geo_distance" : {
"pin.location" : [-70, 40],
"order" : "asc",
"unit" : "km",
"mode" : "min",
"distance_type" : "arc",
"ignore_unmapped": true
}
}
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
-
distance_type
How to compute the distance. Can either be
arc
(default), orplane
(faster, but inaccurate on long distances and close to the poles).如何计算距离。可以是
arc
(默认值),也可以是plane
(更快,但在长距离和极点附近不准确)。 -
mode
What to do in case a field has several geo points. By default, the shortest distance is taken into account when sorting in ascending order and the longest distance when sorting in descending order. Supported values are
min
,max
,median
andavg
.如果一个字段具有多个地理位置,该怎么办。默认情况下,按升序排序时考虑最短距离,按降序排序时考虑最长距离。支持的值是
min
,max
,median
和avg
。 -
unit
The unit to use when computing sort values. The default is
m
(meters).计算排序值时使用的单位。默认值为
m
(米)。 -
ignore_unmapped
Indicates if the unmapped field should be treated as a missing value. Setting it to
true
is equivalent to specifying anunmapped_type
in the field sort. The default isfalse
(unmapped field cause the search to fail).指示是否应将未映射的字段视为缺失值。将其设置
true
为等于unmapped_type
在字段排序中指定。默认值为false
(未映射的字段会导致搜索失败)。
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.
地理距离排序不支持可配置的缺失值:该距离将始终被视为等于Infinity
文档不具有用于距离计算的字段的值时。
The following formats are supported in providing the coordinates:
提供坐标时支持以下格式:
Lat Lon as Properties
GET /_search
{
"sort" : [
{
"_geo_distance" : {
"pin.location" : {
"lat" : 40,
"lon" : -70
},
"order" : "asc",
"unit" : "km"
}
}
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
Lat Lon as String
Format in lat,lon
.
格式为lat,lon
。
GET /_search
{
"sort": [
{
"_geo_distance": {
"pin.location": "40,-70",
"order": "asc",
"unit": "km"
}
}
],
"query": {
"term": { "user": "kimchy" }
}
}
Geohash
GET /_search
{
"sort": [
{
"_geo_distance": {
"pin.location": "drm3btev3e86",
"order": "asc",
"unit": "km"
}
}
],
"query": {
"term": { "user": "kimchy" }
}
}
Lat Lon as Array
Format in [lon, lat]
, note, the order of lon/lat here in order to conform with GeoJSON.
请在[lon, lat]
此处格式化lon / lat的顺序,以符合GeoJSON。
GET /_search
{
"sort": [
{
"_geo_distance": {
"pin.location": [ -70, 40 ],
"order": "asc",
"unit": "km"
}
}
],
"query": {
"term": { "user": "kimchy" }
}
}
Multiple reference points
Multiple geo points can be passed as an array containing any geo_point
format, for example
多个地理位置可以作为包含任何geo_point
格式的数组传递,例如
GET /_search
{
"sort": [
{
"_geo_distance": {
"pin.location": [ [ -70, 40 ], [ -71, 42 ] ],
"order": "asc",
"unit": "km"
}
}
],
"query": {
"term": { "user": "kimchy" }
}
}
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.
然后为一个文件的最终距离将是min
/ max
/ avg
(通过定义mode
包含在文档中的排序请求中给出的所有点中的所有点的距离)。
Script Based Sorting
Allow to sort based on custom scripts, here is an example:
允许基于自定义脚本进行排序,这是一个示例:
GET /_search
{
"query": {
"term": { "user": "kimchy" }
},
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "doc['field_name'].value * params.factor",
"params": {
"factor": 1.1
}
},
"order": "asc"
}
}
}
Track Scores
When sorting on a field, scores are not computed. By setting track_scores
to true, scores will still be computed and tracked.
在字段上排序时,不计算分数。通过设置 track_scores
为true,仍将计算和跟踪分数。
GET /_search
{
"track_scores": true,
"sort" : [
{ "post_date" : {"order" : "desc"} },
{ "name" : "desc" },
{ "age" : "desc" }
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
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 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
).
排序时,将相关的排序字段值加载到内存中。这意味着每个分片应该有足够的内存来容纳它们。对于基于字符串的类型,不应分析/标记排序的字段。对于数值类型,如果可能的话,建议显式地将类型设为较窄类型(如short
,integer
和 float
)。