Skip to content

Commit 48ea913

Browse files
author
Isabel Drost-Fromm
committed
Add CONSOLE annotation to sort documentation
This adds CONSOLE to sort docs in order to automatically execute the doc snippets. Fixes a few minor types along the way. Relates to elastic#18160
1 parent 9d5537d commit 48ea913

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

docs/reference/search/request/sort.asciidoc

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,35 @@ Allows to add one or more sort on specific fields. Each sort can be
55
reversed as well. The sort is defined on a per field level, with special
66
field name for `_score` to sort by score, and `_doc` to sort by index order.
77

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+
834
[source,js]
935
--------------------------------------------------
36+
GET /my_index/my_type/_search
1037
{
1138
"sort" : [
1239
{ "post_date" : {"order" : "asc"}},
@@ -20,6 +47,8 @@ field name for `_score` to sort by score, and `_doc` to sort by index order.
2047
}
2148
}
2249
--------------------------------------------------
50+
// CONSOLE
51+
// TEST[continued]
2352

2453
NOTE: `_doc` has no real use-case besides being the most efficient sort order.
2554
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:
6089
===== Sort mode example usage
6190

6291
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
6493
the average price per document.
6594

6695
[source,js]
6796
--------------------------------------------------
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+
{
69105
"query" : {
70-
...
106+
"term" : { "product" : "chocolate" }
71107
},
72108
"sort" : [
73109
{"price" : {"order" : "asc", "mode" : "avg"}}
74110
]
75-
}'
111+
}
76112
--------------------------------------------------
113+
// CONSOLE
77114

78115
[[nested-sorting]]
79116
==== Sorting within nested objects.
@@ -101,9 +138,10 @@ The `nested_path` needs to be specified; otherwise, elasticsearch doesn't know o
101138

102139
[source,js]
103140
--------------------------------------------------
104-
curl -XPOST 'localhost:9200/_search' -d '{
141+
POST /_search
142+
{
105143
"query" : {
106-
...
144+
"term" : { "product" : "chocolate" }
107145
},
108146
"sort" : [
109147
{
@@ -117,8 +155,9 @@ curl -XPOST 'localhost:9200/_search' -d '{
117155
}
118156
}
119157
]
120-
}'
158+
}
121159
--------------------------------------------------
160+
// CONSOLE
122161

123162
Nested sorting is also supported when sorting by
124163
scripts and sorting by geo distance.
@@ -132,15 +171,17 @@ will be used for missing docs as the sort value). For example:
132171

133172
[source,js]
134173
--------------------------------------------------
174+
GET /_search
135175
{
136176
"sort" : [
137-
{ "price" : {"missing" : "_last"} },
177+
{ "price" : {"missing" : "_last"} }
138178
],
139179
"query" : {
140-
"term" : { "user" : "kimchy" }
180+
"term" : { "product" : "chocolate" }
141181
}
142182
}
143183
--------------------------------------------------
184+
// CONSOLE
144185

145186
NOTE: If a nested inner object doesn't match with
146187
the `nested_filter` then a missing value is used.
@@ -155,15 +196,17 @@ example of how it can be used:
155196

156197
[source,js]
157198
--------------------------------------------------
199+
GET /_search
158200
{
159201
"sort" : [
160-
{ "price" : {"unmapped_type" : "long"} },
202+
{ "price" : {"unmapped_type" : "long"} }
161203
],
162204
"query" : {
163-
"term" : { "user" : "kimchy" }
205+
"term" : { "product" : "chocolate" }
164206
}
165207
}
166208
--------------------------------------------------
209+
// CONSOLE
167210

168211
If any of the indices that are queried doesn't have a mapping for `price`
169212
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:
176219

177220
[source,js]
178221
--------------------------------------------------
222+
GET /_search
179223
{
180224
"sort" : [
181225
{
@@ -193,6 +237,7 @@ Allow to sort by `_geo_distance`. Here is an example:
193237
}
194238
}
195239
--------------------------------------------------
240+
// CONSOLE
196241

197242

198243

@@ -209,6 +254,7 @@ The following formats are supported in providing the coordinates:
209254

210255
[source,js]
211256
--------------------------------------------------
257+
GET /_search
212258
{
213259
"sort" : [
214260
{
@@ -227,13 +273,15 @@ The following formats are supported in providing the coordinates:
227273
}
228274
}
229275
--------------------------------------------------
276+
// CONSOLE
230277

231278
===== Lat Lon as String
232279

233280
Format in `lat,lon`.
234281

235282
[source,js]
236283
--------------------------------------------------
284+
GET /_search
237285
{
238286
"sort" : [
239287
{
@@ -249,11 +297,13 @@ Format in `lat,lon`.
249297
}
250298
}
251299
--------------------------------------------------
300+
// CONSOLE
252301

253302
===== Geohash
254303

255304
[source,js]
256305
--------------------------------------------------
306+
GET /_search
257307
{
258308
"sort" : [
259309
{
@@ -269,6 +319,7 @@ Format in `lat,lon`.
269319
}
270320
}
271321
--------------------------------------------------
322+
// CONSOLE
272323

273324
===== Lat Lon as Array
274325

@@ -277,6 +328,7 @@ conform with http://geojson.org/[GeoJSON].
277328

278329
[source,js]
279330
--------------------------------------------------
331+
GET /_search
280332
{
281333
"sort" : [
282334
{
@@ -292,6 +344,7 @@ conform with http://geojson.org/[GeoJSON].
292344
}
293345
}
294346
--------------------------------------------------
347+
// CONSOLE
295348

296349

297350
==== Multiple reference points
@@ -316,9 +369,10 @@ Allow to sort based on custom scripts, here is an example:
316369

317370
[source,js]
318371
--------------------------------------------------
372+
GET /_search
319373
{
320374
"query" : {
321-
....
375+
"term" : { "user" : "kimchy" }
322376
},
323377
"sort" : {
324378
"_script" : {
@@ -334,6 +388,7 @@ Allow to sort based on custom scripts, here is an example:
334388
}
335389
}
336390
--------------------------------------------------
391+
// CONSOLE
337392

338393

339394
==== Track Scores
@@ -343,6 +398,7 @@ When sorting on a field, scores are not computed. By setting
343398

344399
[source,js]
345400
--------------------------------------------------
401+
GET /_search
346402
{
347403
"track_scores": true,
348404
"sort" : [
@@ -355,6 +411,7 @@ When sorting on a field, scores are not computed. By setting
355411
}
356412
}
357413
--------------------------------------------------
414+
// CONSOLE
358415

359416
==== Memory Considerations
360417

0 commit comments

Comments
 (0)