我们知道在默认的情况下,搜索的结果是按照相关性来进行排序的。分数最高的排在前面,而分数低的向后依次排序。在绝大多数的情况下,这种排序是非常有效的,而且也适用我们的很多用例。即便针对分数,我们也可以对搜索的结果进行定制。关于这个分数是如何及算出来的,你可以参考我之前的文章 “Elasticsearch:使用 Elasticsearch 提高网站搜索查询的相关性”。我们也可以使用一下方法来定制我们的分数。你可以阅读如下的文章:
然而,有时,我们并比想按照默认的分数进行排序。比如我们想按照姓氏的顺序来排序,或者按照价格的高低来排序,或者按照距离的远近来进行排序。Elasticsearch 允许你使用 sort 来使你在特定字段上添加一种或多种排序。 每种排序也可以颠倒。 排序是在每个字段级别上定义的,_score 的特殊字段名称按分数排序,_doc 按索引顺序排序。
在今天的文章中,我来详述如何使用 sort 来进行排序。
准备数据
我们先在 Kibana 中使用如下的命令来创建一个叫做 mybooks 的索引:
1. PUT mybooks
2. {
3. "mappings": {
4. "properties": {
5. "date": {
6. "type": "date"
7. },
8. "description": {
9. "type": "text"
10. },
11. "offer": {
12. "type": "nested",
13. "properties": {
14. "name": {
15. "type": "keyword"
16. },
17. "price": {
18. "type": "long"
19. }
20. }
21. },
22. "position": {
23. "type": "long"
24. },
25. "price": {
26. "type": "long"
27. },
28. "publisher": {
29. "type": "keyword"
30. },
31. "quantity": {
32. "type": "long"
33. },
34. "title": {
35. "type": "text",
36. "fields": {
37. "keyword": {
38. "type": "keyword",
39. "ignore_above": 256
40. }
41. }
42. },
43. "uuid": {
44. "type": "keyword"
45. },
46. "location": {
47. "type": "geo_point"
48. }
49. }
50. }
51. }
在上面的索引中,我有意识地设置了整型类,date,geo_point,text,keyword 及 nested 各个类型的数据。接着我们使用如下的命令来写入数据到索引中:
1. POST _bulk/
2. {"index":{"_index":"mybooks", "_id":"1"}}
3. {"uuid":"11111","position":1, "title":"Joe Bates", "description":"Joe Testere nice guy", "date":"2015-10-22","price":[1, 2, 3], "quantity":50, "publisher": "RELX", "offer": [{"name":"book1", "price": 5}], "location":{"lat":"39.718256","lon":"116.367910"}}
4. {"index":{"_index":"mybooks", "_id":"2"}}
5. {"uuid":"22222","position":2, "title":"Joe Tester","description":"Joe Testere nice guy", "date":"2015-10-22", "price":[4, 5, 6], "quantity":50, "publisher": "RELX", "offer": [{"name":"book2", "price": 5}], "location":{"lat":"39.718256","lon":"116.367910"}}
6. {"index":{"_index":"mybooks", "_id":"3"}}
7. {"uuid":"33333", "position":3, "title":"Bill Baloney","description":"Bill Testere nice guy","date":"2016-06-12","price":[7, 8, 9], "quantity":34, "publisher": "Thomson Reuters", "offer": [{"name":"book3", "price": 10}], "location":{"lat":"40.718256","lon":"117.367910"}}
8. {"index":{"_index":"mybooks", "_id":"4"}}
9. {"uuid":"44444", "position":4, "title":"Bill Klingon","description":"Bill is not a nice guy", "date":"2017-09-21","price":[1, 5, 8], "quantity":33, "offer": [{"name":"book4", "price": 20}],"location":{"lat":"41.718256", "lon":"118.367910"}}
在上面,我们使用了4个文档数据。数据不必太多,只要能说明问题即可。其中第一个文档和第二个文档的日期是一样的。
使用日期来进行排序
我们对搜索的结果按照日期进行排序:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "date": {
9. "order": "desc"
10. }
11. }
12. ],
13. "_source": false,
14. "fields": [
15. "date"
16. ]
17. }
在上面,我们把搜索的结果按照 date 进行降序排序。我们可以看到如下的结果:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "4",
7. "_score" : null,
8. "fields" : {
9. "date" : [
10. "2017-09-21T00:00:00.000Z"
11. ]
12. },
13. "sort" : [
14. 1505952000000
15. ]
16. },
17. {
18. "_index" : "mybooks",
19. "_id" : "3",
20. "_score" : null,
21. "fields" : {
22. "date" : [
23. "2016-06-12T00:00:00.000Z"
24. ]
25. },
26. "sort" : [
27. 1465689600000
28. ]
29. },
30. {
31. "_index" : "mybooks",
32. "_id" : "1",
33. "_score" : null,
34. "fields" : {
35. "date" : [
36. "2015-10-22T00:00:00.000Z"
37. ]
38. },
39. "sort" : [
40. 1445472000000
41. ]
42. },
43. {
44. "_index" : "mybooks",
45. "_id" : "2",
46. "_score" : null,
47. "fields" : {
48. "date" : [
49. "2015-10-22T00:00:00.000Z"
50. ]
51. },
52. "sort" : [
53. 1445472000000
54. ]
55. }
56. ]
57. }
58. }
首先,我们可以看到 _score 的值为 null。当我一旦使用 sort 时,那么原来的 _score 就没有任何的意义了。上面的结果显示它是按照 date 的降序进行返回的。在很多的时候,我们甚至可以对多个字段来进行排序,比如:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match": {
5. "title": "joe"
6. }
7. },
8. "sort": [
9. {
10. "date": {
11. "order": "desc"
12. }
13. },
14. {
15. "title.keyword": {
16. "order": "desc"
17. }
18. }
19. ],
20. "_source": false,
21. "fields": [
22. "date",
23. "title"
24. ]
25. }
在上面,我们搜索 title 里含有 joe 的文档,并按照 date 降序排列。如果两个 date 的时间是一致的,那么再按照 title.keyword 来进行降序排序。请注意我们必须使用 title.keyword 而不是 title,这是因为我们不可以针对 text 字段来进行排序的。上面命令的结果为:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "2",
7. "_score" : null,
8. "fields" : {
9. "date" : [
10. "2015-10-22T00:00:00.000Z"
11. ],
12. "title" : [
13. "Joe Tester"
14. ]
15. },
16. "sort" : [
17. 1445472000000,
18. "Joe Tester"
19. ]
20. },
21. {
22. "_index" : "mybooks",
23. "_id" : "1",
24. "_score" : null,
25. "fields" : {
26. "date" : [
27. "2015-10-22T00:00:00.000Z"
28. ],
29. "title" : [
30. "Joe Bates"
31. ]
32. },
33. "sort" : [
34. 1445472000000,
35. "Joe Bates"
36. ]
37. }
38. ]
39. }
40. }
如果我们把 title.keyword 按照升序排序的话,就是这样的命令:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match": {
5. "title": "joe"
6. }
7. },
8. "sort": [
9. {
10. "date": {
11. "order": "desc"
12. }
13. },
14. {
15. "title.keyword": {
16. "order": "asc"
17. }
18. }
19. ],
20. "_source": false,
21. "fields": [
22. "date",
23. "title"
24. ]
25. }
注意:_doc 除了是最有效的排序顺序之外没有真正的用例。 因此,如果你不关心文档返回的顺序,那么你应该按 _doc 排序。 这在 scrolling 时特别有用。
Sort mode 选项
Elasticsearch 支持按数组或多值字段排序。 mode 选项控制选择哪个数组值来对其所属的文档进行排序。 mode 选项可以具有以下值:
min | 选择最低值。 |
max | 选择最高值。 |
sum | 使用所有值的总和作为排序值。 仅适用于基于数字的数组字段。 |
avg | 使用所有值的平均值作为排序值。 仅适用于基于数字的数组字段。 |
median | 使用所有值的中位数作为排序值。 仅适用于基于数字的数组字段。 |
升序排序中的默认排序模式是 min — 选取最小值。 默认的降序排序模式是max — 取最高值。
针对我们的例子,我们有一个字段 price。它是一个多值的字段。我们现在来对它进行排序,比如:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "price": {
9. "order": "desc",
10. "mode": "avg"
11. }
12. }
13. ],
14. "_source": false,
15. "fields": [
16. "price"
17. ]
18. }
在上面,我们针对 price 进行平均值排序:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "3",
7. "_score" : null,
8. "fields" : {
9. "price" : [
10. 7,
11. 8,
12. 9
13. ]
14. },
15. "sort" : [
16. 8
17. ]
18. },
19. {
20. "_index" : "mybooks",
21. "_id" : "2",
22. "_score" : null,
23. "fields" : {
24. "price" : [
25. 4,
26. 5,
27. 6
28. ]
29. },
30. "sort" : [
31. 5
32. ]
33. },
34. {
35. "_index" : "mybooks",
36. "_id" : "4",
37. "_score" : null,
38. "fields" : {
39. "price" : [
40. 1,
41. 5,
42. 8
43. ]
44. },
45. "sort" : [
46. 5
47. ]
48. },
49. {
50. "_index" : "mybooks",
51. "_id" : "1",
52. "_score" : null,
53. "fields" : {
54. "price" : [
55. 1,
56. 2,
57. 3
58. ]
59. },
60. "sort" : [
61. 2
62. ]
63. }
64. ]
65. }
66. }
如上所示,我们在 sort 字段里,我们可以看到 price 的平均值。搜索的结果是按照平均值降序进行排列的。
对数字字段进行排序
对于数字字段,也可以使用 numeric_type 选项将值从一种类型转换为另一种类型。 此选项接受以下值:["double", "long", "date", "date_nanos"] 并可用于跨多个数据流或索引的搜索,其中排序字段映射不同。
例如考虑这两个索引:
1. PUT /index_double
2. {
3. "mappings": {
4. "properties": {
5. "field": { "type": "double" }
6. }
7. }
8. }
10. PUT /index_long
11. {
12. "mappings": {
13. "properties": {
14. "field": { "type": "long" }
15. }
16. }
17. }
由于 field 在第一个索引中被映射为双精度(double),在第二个索引中被映射为长型数(long),因此默认情况下无法使用此字段对查询两个索引的请求进行排序。 但是,你可以使用 numeric_type 选项将类型强制为一种或另一种,以便为所有索引强制使用特定类型:
1. POST /index_long,index_double/_search
2. {
3. "sort" : [
4. {
5. "field" : {
6. "numeric_type" : "double"
7. }
8. }
9. ]
10. }
在上面的示例中,index_long 索引的值被强制转换为双精度,以便与 index_double 索引生成的值兼容。 也可以将浮点字段转换为 long 但请注意,在这种情况下,浮点被替换为小于或等于参数的最大值(如果值为负,则大于或等于)并且等于 为一个数学整数。
此选项还可用于将使用毫秒分辨率的日期字段转换为具有纳秒分辨率的 date_nanos 字段。 例如考虑这两个索引:
1. PUT /index_double
2. {
3. "mappings": {
4. "properties": {
5. "field": { "type": "date" }
6. }
7. }
8. }
10. PUT /index_long
11. {
12. "mappings": {
13. "properties": {
14. "field": { "type": "date_nanos" }
15. }
16. }
17. }
这些索引中的值以不同的分辨率存储,因此对这些字段进行排序将始终将 date 排序在 date_nanos 之前(升序)。 使用 numeric_type 类型选项,可以为排序设置单个分辨率,设置为 date 会将 date_nanos 转换为毫秒分辨率,而 date_nanos 会将 date 字段中的值转换为纳秒分辨率:
1. POST /index_long,index_double/_search
2. {
3. "sort" : [
4. {
5. "field" : {
6. "numeric_type" : "date_nanos"
7. }
8. }
9. ]
10. }
警告:为避免溢出,转换为 date_nanos 不能应用于 1970 年之前和 2262 年之后的日期,因为纳秒表示为 long。
在嵌套对象中排序
Elasticsearch 还支持按一个或多个嵌套(nested)对象内的字段进行排序。 嵌套字段支持的排序具有 nested 排序选项,具有以下属性:
path | 定义要排序的嵌套对象。 实际的排序字段必须是此嵌套对象内的直接字段。 按嵌套字段排序时,此字段为必填项。 |
filter | 嵌套路径内的内部对象应匹配的过滤器,以便通过排序考虑其字段值。 常见的情况是在嵌套过滤器或查询中重复查询/过滤器。 默认情况下,没有 filter 处于活动状态。 |
max_children | 选择排序值时要考虑的每个根文档的最大子项数。 默认为无限制。 |
nested | 与顶级嵌套相同,但适用于当前嵌套对象中的另一个嵌套路径。 |
注意:如果在没有嵌套上下文的排序中定义了嵌套字段,Elasticsearch 将抛出错误。
嵌套排序示例
在下面的示例中,offer 是一个 nested 类型的字段。 需要指定嵌套 path; 否则,Elasticsearch 不知道需要捕获哪些嵌套级别的排序值。
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "offer.price": {
9. "mode": "avg",
10. "order": "desc",
11. "nested": {
12. "path": "offer",
13. "filter": {
14. "term": {
15. "offer.name": "book3"
16. }
17. }
18. }
19. }
20. }
21. ]
22. }
在上面,我们是以 offer.price 来进行降序排列。我们针对 offer.name 为 book3 得到排序的优先级。上面的排序结果为:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "3",
7. "_score" : null,
8. "_source" : {
9. "uuid" : "33333",
10. "position" : 3,
11. "title" : "Bill Baloney",
12. "description" : "Bill Testere nice guy",
13. "date" : "2016-06-12",
14. "price" : [
15. 7,
16. 8,
17. 9
18. ],
19. "quantity" : 34,
20. "publisher" : "Thomson Reuters",
21. "offer" : [
22. {
23. "name" : "book3",
24. "price" : 10
25. }
26. ],
27. "location" : {
28. "lat" : "40.718256",
29. "lon" : "117.367910"
30. }
31. },
32. "sort" : [
33. 10
34. ]
35. },
36. {
37. "_index" : "mybooks",
38. "_id" : "1",
39. "_score" : null,
40. "_source" : {
41. "uuid" : "11111",
42. "position" : 1,
43. "title" : "Joe Bates",
44. "description" : "Joe Testere nice guy",
45. "date" : "2015-10-22",
46. "price" : [
47. 1,
48. 2,
49. 3
50. ],
51. "quantity" : 50,
52. "publisher" : "RELX",
53. "offer" : [
54. {
55. "name" : "book1",
56. "price" : 5
57. }
58. ],
59. "location" : {
60. "lat" : "39.718256",
61. "lon" : "116.367910"
62. }
63. },
64. "sort" : [
65. -9223372036854775808
66. ]
67. },
68. {
69. "_index" : "mybooks",
70. "_id" : "2",
71. "_score" : null,
72. "_source" : {
73. "uuid" : "22222",
74. "position" : 2,
75. "title" : "Joe Tester",
76. "description" : "Joe Testere nice guy",
77. "date" : "2015-10-22",
78. "price" : [
79. 4,
80. 5,
81. 6
82. ],
83. "quantity" : 50,
84. "publisher" : "RELX",
85. "offer" : [
86. {
87. "name" : "book2",
88. "price" : 5
89. }
90. ],
91. "location" : {
92. "lat" : "39.718256",
93. "lon" : "116.367910"
94. }
95. },
96. "sort" : [
97. -9223372036854775808
98. ]
99. },
100. {
101. "_index" : "mybooks",
102. "_id" : "4",
103. "_score" : null,
104. "_source" : {
105. "uuid" : "44444",
106. "position" : 4,
107. "title" : "Bill Klingon",
108. "description" : "Bill is not a nice guy",
109. "date" : "2017-09-21",
110. "price" : [
111. 1,
112. 5,
113. 8
114. ],
115. "quantity" : 33,
116. "offer" : [
117. {
118. "name" : "book4",
119. "price" : 20
120. }
121. ],
122. "location" : {
123. "lat" : "41.718256",
124. "lon" : "118.367910"
125. }
126. },
127. "sort" : [
128. -9223372036854775808
129. ]
130. }
131. ]
132. }
133. }
从上面的结果可以看出来,offer.name 为 book3 的文档排名最高。
在下面的示例中,parent 字段和 child 字段属于 nested 类型。 每一层都需要指定 nested.path; 否则,Elasticsearch 不知道需要捕获哪些嵌套级别的排序值。
1. POST /_search
2. {
3. "query": {
4. "nested": {
5. "path": "parent",
6. "query": {
7. "bool": {
8. "must": {"range": {"parent.age": {"gte": 21}}},
9. "filter": {
10. "nested": {
11. "path": "parent.child",
12. "query": {"match": {"parent.child.name": "matt"}}
13. }
14. }
15. }
16. }
17. }
18. },
19. "sort" : [
20. {
21. "parent.child.age" : {
22. "mode" : "min",
23. "order" : "asc",
24. "nested": {
25. "path": "parent",
26. "filter": {
27. "range": {"parent.age": {"gte": 21}}
28. },
29. "nested": {
30. "path": "parent.child",
31. "filter": {
32. "match": {"parent.child.name": "matt"}
33. }
34. }
35. }
36. }
37. }
38. ]
39. }
在按脚本排序和按地理距离排序时也支持嵌套排序。
缺失值
missing 参数指定应如何处理缺少排序字段的文档:缺失值可以设置为 _last、_first 或自定义值(将用于缺少文档作为排序值)。 默认值为 _last。
例如:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "publisher": {
9. "order": "desc",
10. "missing": "_first"
11. }
12. }
13. ],
14. "_source": false,
15. "fields": [
16. "publisher"
17. ]
18. }
在上,我们在 sort 时,定义如果缺失 publisher 这个字段,那么就排到前面去。上面的搜索结果是:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "4",
7. "_score" : null,
8. "sort" : [
9. null
10. ]
11. },
12. {
13. "_index" : "mybooks",
14. "_id" : "3",
15. "_score" : null,
16. "fields" : {
17. "publisher" : [
18. "Thomson Reuters"
19. ]
20. },
21. "sort" : [
22. "Thomson Reuters"
23. ]
24. },
25. {
26. "_index" : "mybooks",
27. "_id" : "1",
28. "_score" : null,
29. "fields" : {
30. "publisher" : [
31. "RELX"
32. ]
33. },
34. "sort" : [
35. "RELX"
36. ]
37. },
38. {
39. "_index" : "mybooks",
40. "_id" : "2",
41. "_score" : null,
42. "fields" : {
43. "publisher" : [
44. "RELX"
45. ]
46. },
47. "sort" : [
48. "RELX"
49. ]
50. }
51. ]
52. }
53. }
我们可以看到 _id 为 4 的文档排在第一位,而其它含有 publisher 的文档是按照降序进行排列的。
注意:如果嵌套的内部对象与nested.filter 不匹配,则使用缺失值。
忽略未映射的字段
默认情况下,如果没有与字段关联的映射,则搜索请求将失败。 unmapped_type 选项允许你忽略没有映射的字段并且不按它们排序。 此参数的值用于确定要发出的排序值。 这是如何使用它的示例。我们来创建一个如下另外一个索引:
1. PUT mybooks1
2. {
3. "mappings": {
4. "properties": {
5. "date": {
6. "type": "date"
7. },
8. "description": {
9. "type": "text"
10. },
11. "offer": {
12. "type": "nested",
13. "properties": {
14. "name": {
15. "type": "keyword"
16. },
17. "price": {
18. "type": "long"
19. }
20. }
21. },
22. "position": {
23. "type": "long"
24. },
25. "publisher": {
26. "type": "keyword"
27. },
28. "quantity": {
29. "type": "long"
30. },
31. "title": {
32. "type": "text",
33. "fields": {
34. "keyword": {
35. "type": "keyword",
36. "ignore_above": 256
37. }
38. }
39. },
40. "uuid": {
41. "type": "keyword"
42. },
43. "location": {
44. "type": "geo_point"
45. }
46. }
47. }
48. }
在上面,我们创建了一个叫做 mybooks1 的索引。请注意它和之前的 mybooks 索引的区别就是它没有了之前的 price 字段。其它的字段都一样。我们使用如下命令来创建一个文档:
1. POST _bulk/
2. {"index":{"_index":"mybooks", "_id":"1"}}
3. {"uuid":"99999","position":1, "title":"Joe Bates", "description":"Joe Testere nice guy", "date":"2015-10-22", "quantity":50, "publisher": "RELX", "offer": [{"name":"book1", "price": 5}], "location":{"lat":"39.718256","lon":"116.367910"}}
我们使用如下的搜索:
1. GET mybooks*/_search?filter_path=**.hits
2. {
3. "query": {
4. "match": {
5. "title": "joe"
6. }
7. },
8. "sort": [
9. {
10. "price": {
11. "order": "desc",
12. "mode": "avg"
13. }
14. }
15. ]
16. }
上面的命令返回的结果为:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "2",
7. "_score" : null,
8. "_source" : {
9. "uuid" : "22222",
10. "position" : 2,
11. "title" : "Joe Tester",
12. "description" : "Joe Testere nice guy",
13. "date" : "2015-10-22",
14. "price" : [
15. 4,
16. 5,
17. 6
18. ],
19. "quantity" : 50,
20. "publisher" : "RELX",
21. "offer" : [
22. {
23. "name" : "book2",
24. "price" : 5
25. }
26. ],
27. "location" : {
28. "lat" : "39.718256",
29. "lon" : "116.367910"
30. }
31. },
32. "sort" : [
33. 5
34. ]
35. },
36. {
37. "_index" : "mybooks",
38. "_id" : "1",
39. "_score" : null,
40. "_source" : {
41. "uuid" : "99999",
42. "position" : 1,
43. "title" : "Joe Bates",
44. "description" : "Joe Testere nice guy",
45. "date" : "2015-10-22",
46. "quantity" : 50,
47. "publisher" : "RELX",
48. "offer" : [
49. {
50. "name" : "book1",
51. "price" : 5
52. }
53. ],
54. "location" : {
55. "lat" : "39.718256",
56. "lon" : "116.367910"
57. }
58. },
59. "sort" : [
60. -9223372036854775808
61. ]
62. }
63. ]
64. }
65. }
如果查询的任何索引没有价格映射,则 Elasticsearch 将处理它,就好像存在 long 类型的映射一样,该索引中的所有文档都没有该字段的值。
地理距离排序
允许按 _geo_distance 排序。 这是一个示例,在我们的 mybooks 索引中 location 是一个 geo_point 的类型。我们可以做如下的查询:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "_geo_distance": {
9. "location": {
10. "lat": 39.718256,
11. "lon": 116.367910
12. },
13. "order": "asc",
14. "unit": "km",
15. "mode": "min",
16. "distance_type": "arc",
17. "ignore_unmapped": true
18. }
19. }
20. ],
21. "_source": false,
22. "fields": [
23. "location"
24. ]
25. }
在上面,我们以经纬度地点 [116.367910, 39.718256] 为中心,其它文档距离这个地点按照升序进行排列。上面命令搜索的结果为:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "2",
7. "_score" : null,
8. "fields" : {
9. "location" : [
10. {
11. "coordinates" : [
12. 116.36791,
13. 39.718256
14. ],
15. "type" : "Point"
16. }
17. ]
18. },
19. "sort" : [
20. 0.0
21. ]
22. },
23. {
24. "_index" : "mybooks",
25. "_id" : "1",
26. "_score" : null,
27. "fields" : {
28. "location" : [
29. {
30. "coordinates" : [
31. 116.36791,
32. 39.718256
33. ],
34. "type" : "Point"
35. }
36. ]
37. },
38. "sort" : [
39. 0.0
40. ]
41. },
42. {
43. "_index" : "mybooks",
44. "_id" : "3",
45. "_score" : null,
46. "fields" : {
47. "location" : [
48. {
49. "coordinates" : [
50. 117.36791,
51. 40.718256
52. ],
53. "type" : "Point"
54. }
55. ]
56. },
57. "sort" : [
58. 139.9034456345085
59. ]
60. },
61. {
62. "_index" : "mybooks",
63. "_id" : "4",
64. "_score" : null,
65. "fields" : {
66. "location" : [
67. {
68. "coordinates" : [
69. 118.36791,
70. 41.718256
71. ],
72. "type" : "Point"
73. }
74. ]
75. },
76. "sort" : [
77. 279.0300719062577
78. ]
79. }
80. ]
81. }
82. }
我们可以看到有两个文档的距离是 0,而另外的两个文档的距离是 139.9034456345085 及 279.0300719062577 公里。
上面的参数描述如下:
distance_type | 如何计算距离。 可以是圆弧(默认),也可以是平面(速度更快,但在长距离和靠近极点时不准确)。 |
mode | 如果一个字段有多个地理点,该怎么办。 默认情况下,升序排序时考虑最短距离,降序排序时考虑最长距离。 支持的值为最小值、最大值、中值和平均值。 |
unit | 计算排序值时使用的单位。 默认值为 m(米)。 |
ignore_unmapped | 指示是否应将未映射的字段视为缺失值。 将其设置为 true 等效于在字段排序中指定 unmapped_type。 默认值为 false(未映射的字段会导致搜索失败)。 |
注意:地理距离排序不支持可配置的缺失值:当文档没有用于距离计算的字段的值时,距离将始终被视为等于 Infinity。
上面的搜索也支持如下的格式的坐标表示:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "_geo_distance": {
9. "location": [
10. 116.36791,
11. 39.718256
12. ],
13. "order": "asc",
14. "unit": "km",
15. "mode": "min",
16. "distance_type": "arc",
17. "ignore_unmapped": true
18. }
19. }
20. ],
21. "_source": false,
22. "fields": [
23. "location"
24. ]
25. }
我们也可以使用 WKT 字符串:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "_geo_distance": {
9. "location": "POINT(116.36791 39.718256)",
10. "order": "asc",
11. "unit": "km",
12. "mode": "min",
13. "distance_type": "arc",
14. "ignore_unmapped": true
15. }
16. }
17. ],
18. "_source": false,
19. "fields": [
20. "location"
21. ]
22. }
我们甚至可以使用 Geohash 代码。经过转换 (116.36791 39.718256) 可以转换为 wx4cbn2x8gd:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "_geo_distance": {
9. "location": "wx4cbn2x8gd",
10. "order": "asc",
11. "unit": "km",
12. "mode": "min",
13. "distance_type": "arc",
14. "ignore_unmapped": true
15. }
16. }
17. ],
18. "_source": false,
19. "fields": [
20. "location"
21. ]
22. }
显然这个取决于 geohash 的精度。在上面的搜索中,在前两个的搜索结果中,sort 里的值不再是 0 了。说明它还是有一点误差的:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "2",
7. "_score" : null,
8. "fields" : {
9. "location" : [
10. {
11. "coordinates" : [
12. 116.36791,
13. 39.718256
14. ],
15. "type" : "Point"
16. }
17. ]
18. },
19. "sort" : [
20. 7.30239509326306E-5
21. ]
22. },
23. {
24. "_index" : "mybooks",
25. "_id" : "1",
26. "_score" : null,
27. "fields" : {
28. "location" : [
29. {
30. "coordinates" : [
31. 116.36791,
32. 39.718256
33. ],
34. "type" : "Point"
35. }
36. ]
37. },
38. "sort" : [
39. 7.30239509326306E-5
40. ]
41. },
多个参考点
多个地理点可以作为包含任何 geo_point 格式的数组传递,例如
1. GET /_search
2. {
3. "sort": [
4. {
5. "_geo_distance": {
6. "pin.location": [ [ -70, 40 ], [ -71, 42 ] ],
7. "order": "asc",
8. "unit": "km"
9. }
10. }
11. ],
12. "query": {
13. "term": { "user": "kimchy" }
14. }
15. }
文档的最终距离将是文档中包含的所有点到排序请求中给定的所有点的最小/最大/平均(通过模式定义)距离。
基于脚本的排序
允许根据自定义脚本进行排序,这是一个示例。
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "_script": {
9. "type": "number",
10. "order": "desc",
11. "script": {
12. "lang": "painless",
13. "source": """
14. doc['quantity'].value * params.factor
15. """,
16. "params": {
17. "factor": 2
18. }
19. }
20. }
21. }
22. ],
23. "_source": false,
24. "fields": [
25. "quantity"
26. ]
27. }
在上面,我们想知道 quantity 乘以 2 的结果来进行排序:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "1",
7. "_score" : null,
8. "fields" : {
9. "quantity" : [
10. 50
11. ]
12. },
13. "sort" : [
14. 100.0
15. ]
16. },
17. {
18. "_index" : "mybooks",
19. "_id" : "2",
20. "_score" : null,
21. "fields" : {
22. "quantity" : [
23. 50
24. ]
25. },
26. "sort" : [
27. 100.0
28. ]
29. },
30. {
31. "_index" : "mybooks",
32. "_id" : "3",
33. "_score" : null,
34. "fields" : {
35. "quantity" : [
36. 34
37. ]
38. },
39. "sort" : [
40. 68.0
41. ]
42. },
43. {
44. "_index" : "mybooks",
45. "_id" : "4",
46. "_score" : null,
47. "fields" : {
48. "quantity" : [
49. 33
50. ]
51. },
52. "sort" : [
53. 66.0
54. ]
55. }
56. ]
57. }
58. }
我们可以使用如下的查询来查看到底哪本书压货的钱最多。在下面,我们取没本书的平均价格:
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "query": {
4. "match_all": {}
5. },
6. "sort": [
7. {
8. "_script": {
9. "type": "number",
10. "order": "desc",
11. "script": {
12. "lang": "painless",
13. "source": """
14. double total = 0.0;
15. for (int i = 0; i < doc['price'].length; ++i) {
16. total += (double)doc['price'][i];
17. }
19. total = total/3;
20. return total*doc['quantity'].value
21. """
22. }
23. }
24. }
25. ],
26. "_source": false,
27. "fields": [
28. "quantity"
29. ]
30. }
上面的查询返回的结果为:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "3",
7. "_score" : null,
8. "fields" : {
9. "quantity" : [
10. 34
11. ]
12. },
13. "sort" : [
14. 272.0
15. ]
16. },
17. {
18. "_index" : "mybooks",
19. "_id" : "2",
20. "_score" : null,
21. "fields" : {
22. "quantity" : [
23. 50
24. ]
25. },
26. "sort" : [
27. 250.0
28. ]
29. },
30. {
31. "_index" : "mybooks",
32. "_id" : "4",
33. "_score" : null,
34. "fields" : {
35. "quantity" : [
36. 33
37. ]
38. },
39. "sort" : [
40. 154.0
41. ]
42. },
43. {
44. "_index" : "mybooks",
45. "_id" : "1",
46. "_score" : null,
47. "fields" : {
48. "quantity" : [
49. 50
50. ]
51. },
52. "sort" : [
53. 100.0
54. ]
55. }
56. ]
57. }
58. }
从上面,我们可以看出来 _id 为 3 的书压货的钱是最多的。
跟踪分数
对字段进行排序时,不计算分数。 通过将 track_scores 设置为 true,仍然会计算和跟踪分数。
1. GET mybooks/_search?filter_path=**.hits
2. {
3. "track_scores": true,
4. "query": {
5. "match": {
6. "title": "joe"
7. }
8. },
9. "sort": [
10. {
11. "date": {
12. "order": "desc"
13. },
14. "title.keyword": {
15. "order": "desc"
16. },
17. "quantity": {
18. "order": "desc"
19. }
20. }
21. ]
22. }
上面的命令返回的结果为:
1. {
2. "hits" : {
3. "hits" : [
4. {
5. "_index" : "mybooks",
6. "_id" : "2",
7. "_score" : 0.6931471,
8. "_source" : {
9. "uuid" : "22222",
10. "position" : 2,
11. "title" : "Joe Tester",
12. "description" : "Joe Testere nice guy",
13. "date" : "2015-10-22",
14. "price" : [
15. 4,
16. 5,
17. 6
18. ],
19. "quantity" : 50,
20. "publisher" : "RELX",
21. "offer" : [
22. {
23. "name" : "book2",
24. "price" : 5
25. }
26. ],
27. "location" : {
28. "lat" : "39.718256",
29. "lon" : "116.367910"
30. }
31. },
32. "sort" : [
33. 1445472000000,
34. "Joe Tester",
35. 50
36. ]
37. },
38. {
39. "_index" : "mybooks",
40. "_id" : "1",
41. "_score" : 0.6931471,
42. "_source" : {
43. "uuid" : "11111",
44. "position" : 1,
45. "title" : "Joe Bates",
46. "description" : "Joe Testere nice guy",
47. "date" : "2015-10-22",
48. "price" : [
49. 1,
50. 2,
51. 3
52. ],
53. "quantity" : 50,
54. "publisher" : "RELX",
55. "offer" : [
56. {
57. "name" : "book1",
58. "price" : 5
59. }
60. ],
61. "location" : {
62. "lat" : "39.718256",
63. "lon" : "116.367910"
64. }
65. },
66. "sort" : [
67. 1445472000000,
68. "Joe Bates",
69. 50
70. ]
71. }
72. ]
73. }
74. }
内存考虑
排序时,相关的排序字段值被加载到内存中。 这意味着每个分片应该有足够的内存来容纳它们。 对于基于字符串的类型,排序的字段不应被分析/标记化。 对于数字类型,如果可能,建议将类型显式设置为更窄的类型(如 short、integer 和 float)。