@@ -5,8 +5,35 @@ Allows to add one or more sort on specific fields. Each sort can be
5
5
reversed as well. The sort is defined on a per field level, with special
6
6
field name for `_score` to sort by score, and `_doc` to sort by index order.
7
7
8
+ Assuming the following index mapping:
9
+
10
+ [source,js]
11
+ --------------------------------------------------
12
+ PUT /my_index
13
+ {
14
+ "mappings": {
15
+ "my_type": {
16
+ "properties": {
17
+ "post_date": { "type": "date" },
18
+ "user": {
19
+ "type": "string",
20
+ "fielddata": "true"
21
+ },
22
+ "name": {
23
+ "type": "string",
24
+ "fielddata": "true"
25
+ },
26
+ "age": { "type": "integer" }
27
+ }
28
+ }
29
+ }
30
+ }
31
+ --------------------------------------------------
32
+ // CONSOLE
33
+
8
34
[source,js]
9
35
--------------------------------------------------
36
+ GET /my_index/my_type/_search
10
37
{
11
38
"sort" : [
12
39
{ "post_date" : {"order" : "asc"}},
@@ -20,6 +47,8 @@ field name for `_score` to sort by score, and `_doc` to sort by index order.
20
47
}
21
48
}
22
49
--------------------------------------------------
50
+ // CONSOLE
51
+ // TEST[continued]
23
52
24
53
NOTE: `_doc` has no real use-case besides being the most efficient sort order.
25
54
So if you don't care about the order in which documents are returned, then you
@@ -60,20 +89,28 @@ to. The `mode` option can have the following values:
60
89
===== Sort mode example usage
61
90
62
91
In the example below the field price has multiple prices per document.
63
- In this case the result hits will be sort by price ascending based on
92
+ In this case the result hits will be sorted by price ascending based on
64
93
the average price per document.
65
94
66
95
[source,js]
67
96
--------------------------------------------------
68
- curl -XPOST 'localhost:9200/_search' -d '{
97
+ PUT /my_index/my_type/1
98
+ {
99
+ "product": "chocolate",
100
+ "price": [20, 4]
101
+ }
102
+
103
+ POST /_search
104
+ {
69
105
"query" : {
70
- ...
106
+ "term" : { "product" : "chocolate" }
71
107
},
72
108
"sort" : [
73
109
{"price" : {"order" : "asc", "mode" : "avg"}}
74
110
]
75
- }'
111
+ }
76
112
--------------------------------------------------
113
+ // CONSOLE
77
114
78
115
[[nested-sorting]]
79
116
==== Sorting within nested objects.
@@ -101,9 +138,10 @@ The `nested_path` needs to be specified; otherwise, elasticsearch doesn't know o
101
138
102
139
[source,js]
103
140
--------------------------------------------------
104
- curl -XPOST 'localhost:9200/_search' -d '{
141
+ POST /_search
142
+ {
105
143
"query" : {
106
- ...
144
+ "term" : { "product" : "chocolate" }
107
145
},
108
146
"sort" : [
109
147
{
@@ -117,8 +155,9 @@ curl -XPOST 'localhost:9200/_search' -d '{
117
155
}
118
156
}
119
157
]
120
- }'
158
+ }
121
159
--------------------------------------------------
160
+ // CONSOLE
122
161
123
162
Nested sorting is also supported when sorting by
124
163
scripts and sorting by geo distance.
@@ -132,15 +171,17 @@ will be used for missing docs as the sort value). For example:
132
171
133
172
[source,js]
134
173
--------------------------------------------------
174
+ GET /_search
135
175
{
136
176
"sort" : [
137
- { "price" : {"missing" : "_last"} },
177
+ { "price" : {"missing" : "_last"} }
138
178
],
139
179
"query" : {
140
- "term" : { "user " : "kimchy " }
180
+ "term" : { "product " : "chocolate " }
141
181
}
142
182
}
143
183
--------------------------------------------------
184
+ // CONSOLE
144
185
145
186
NOTE: If a nested inner object doesn't match with
146
187
the `nested_filter` then a missing value is used.
@@ -155,15 +196,17 @@ example of how it can be used:
155
196
156
197
[source,js]
157
198
--------------------------------------------------
199
+ GET /_search
158
200
{
159
201
"sort" : [
160
- { "price" : {"unmapped_type" : "long"} },
202
+ { "price" : {"unmapped_type" : "long"} }
161
203
],
162
204
"query" : {
163
- "term" : { "user " : "kimchy " }
205
+ "term" : { "product " : "chocolate " }
164
206
}
165
207
}
166
208
--------------------------------------------------
209
+ // CONSOLE
167
210
168
211
If any of the indices that are queried doesn't have a mapping for `price`
169
212
then Elasticsearch will handle it as if there was a mapping of type
@@ -176,6 +219,7 @@ Allow to sort by `_geo_distance`. Here is an example:
176
219
177
220
[source,js]
178
221
--------------------------------------------------
222
+ GET /_search
179
223
{
180
224
"sort" : [
181
225
{
@@ -193,6 +237,7 @@ Allow to sort by `_geo_distance`. Here is an example:
193
237
}
194
238
}
195
239
--------------------------------------------------
240
+ // CONSOLE
196
241
197
242
198
243
@@ -209,6 +254,7 @@ The following formats are supported in providing the coordinates:
209
254
210
255
[source,js]
211
256
--------------------------------------------------
257
+ GET /_search
212
258
{
213
259
"sort" : [
214
260
{
@@ -227,13 +273,15 @@ The following formats are supported in providing the coordinates:
227
273
}
228
274
}
229
275
--------------------------------------------------
276
+ // CONSOLE
230
277
231
278
===== Lat Lon as String
232
279
233
280
Format in `lat,lon`.
234
281
235
282
[source,js]
236
283
--------------------------------------------------
284
+ GET /_search
237
285
{
238
286
"sort" : [
239
287
{
@@ -249,11 +297,13 @@ Format in `lat,lon`.
249
297
}
250
298
}
251
299
--------------------------------------------------
300
+ // CONSOLE
252
301
253
302
===== Geohash
254
303
255
304
[source,js]
256
305
--------------------------------------------------
306
+ GET /_search
257
307
{
258
308
"sort" : [
259
309
{
@@ -269,6 +319,7 @@ Format in `lat,lon`.
269
319
}
270
320
}
271
321
--------------------------------------------------
322
+ // CONSOLE
272
323
273
324
===== Lat Lon as Array
274
325
@@ -277,6 +328,7 @@ conform with http://geojson.org/[GeoJSON].
277
328
278
329
[source,js]
279
330
--------------------------------------------------
331
+ GET /_search
280
332
{
281
333
"sort" : [
282
334
{
@@ -292,6 +344,7 @@ conform with http://geojson.org/[GeoJSON].
292
344
}
293
345
}
294
346
--------------------------------------------------
347
+ // CONSOLE
295
348
296
349
297
350
==== Multiple reference points
@@ -316,9 +369,10 @@ Allow to sort based on custom scripts, here is an example:
316
369
317
370
[source,js]
318
371
--------------------------------------------------
372
+ GET /_search
319
373
{
320
374
"query" : {
321
- ....
375
+ "term" : { "user" : "kimchy" }
322
376
},
323
377
"sort" : {
324
378
"_script" : {
@@ -334,6 +388,7 @@ Allow to sort based on custom scripts, here is an example:
334
388
}
335
389
}
336
390
--------------------------------------------------
391
+ // CONSOLE
337
392
338
393
339
394
==== Track Scores
@@ -343,6 +398,7 @@ When sorting on a field, scores are not computed. By setting
343
398
344
399
[source,js]
345
400
--------------------------------------------------
401
+ GET /_search
346
402
{
347
403
"track_scores": true,
348
404
"sort" : [
@@ -355,6 +411,7 @@ When sorting on a field, scores are not computed. By setting
355
411
}
356
412
}
357
413
--------------------------------------------------
414
+ // CONSOLE
358
415
359
416
==== Memory Considerations
360
417
0 commit comments