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 #18160
This commit is contained in:
parent
9d5537d874
commit
48ea9137da
|
@ -5,8 +5,35 @@ Allows to add one or more sort on specific fields. Each sort can be
|
||||||
reversed as well. The sort is defined on a per field level, with special
|
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.
|
field name for `_score` to sort by score, and `_doc` to sort by index order.
|
||||||
|
|
||||||
|
Assuming the following index mapping:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
PUT /my_index
|
||||||
|
{
|
||||||
|
"mappings": {
|
||||||
|
"my_type": {
|
||||||
|
"properties": {
|
||||||
|
"post_date": { "type": "date" },
|
||||||
|
"user": {
|
||||||
|
"type": "string",
|
||||||
|
"fielddata": "true"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"fielddata": "true"
|
||||||
|
},
|
||||||
|
"age": { "type": "integer" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
|
[source,js]
|
||||||
|
--------------------------------------------------
|
||||||
|
GET /my_index/my_type/_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{ "post_date" : {"order" : "asc"}},
|
{ "post_date" : {"order" : "asc"}},
|
||||||
|
@ -20,6 +47,8 @@ field name for `_score` to sort by score, and `_doc` to sort by index order.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
// TEST[continued]
|
||||||
|
|
||||||
NOTE: `_doc` has no real use-case besides being the most efficient sort order.
|
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
|
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:
|
||||||
===== Sort mode example usage
|
===== Sort mode example usage
|
||||||
|
|
||||||
In the example below the field price has multiple prices per document.
|
In the example below the field price has multiple prices per document.
|
||||||
In this case the result hits will be sort by price ascending based on
|
In this case the result hits will be sorted by price ascending based on
|
||||||
the average price per document.
|
the average price per document.
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
curl -XPOST 'localhost:9200/_search' -d '{
|
PUT /my_index/my_type/1
|
||||||
|
{
|
||||||
|
"product": "chocolate",
|
||||||
|
"price": [20, 4]
|
||||||
|
}
|
||||||
|
|
||||||
|
POST /_search
|
||||||
|
{
|
||||||
"query" : {
|
"query" : {
|
||||||
...
|
"term" : { "product" : "chocolate" }
|
||||||
},
|
},
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{"price" : {"order" : "asc", "mode" : "avg"}}
|
{"price" : {"order" : "asc", "mode" : "avg"}}
|
||||||
]
|
]
|
||||||
}'
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
[[nested-sorting]]
|
[[nested-sorting]]
|
||||||
==== Sorting within nested objects.
|
==== Sorting within nested objects.
|
||||||
|
@ -101,9 +138,10 @@ The `nested_path` needs to be specified; otherwise, elasticsearch doesn't know o
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
curl -XPOST 'localhost:9200/_search' -d '{
|
POST /_search
|
||||||
|
{
|
||||||
"query" : {
|
"query" : {
|
||||||
...
|
"term" : { "product" : "chocolate" }
|
||||||
},
|
},
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{
|
{
|
||||||
|
@ -117,8 +155,9 @@ curl -XPOST 'localhost:9200/_search' -d '{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}'
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
Nested sorting is also supported when sorting by
|
Nested sorting is also supported when sorting by
|
||||||
scripts and sorting by geo distance.
|
scripts and sorting by geo distance.
|
||||||
|
@ -132,15 +171,17 @@ will be used for missing docs as the sort value). For example:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{ "price" : {"missing" : "_last"} },
|
{ "price" : {"missing" : "_last"} }
|
||||||
],
|
],
|
||||||
"query" : {
|
"query" : {
|
||||||
"term" : { "user" : "kimchy" }
|
"term" : { "product" : "chocolate" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
NOTE: If a nested inner object doesn't match with
|
NOTE: If a nested inner object doesn't match with
|
||||||
the `nested_filter` then a missing value is used.
|
the `nested_filter` then a missing value is used.
|
||||||
|
@ -155,15 +196,17 @@ example of how it can be used:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{ "price" : {"unmapped_type" : "long"} },
|
{ "price" : {"unmapped_type" : "long"} }
|
||||||
],
|
],
|
||||||
"query" : {
|
"query" : {
|
||||||
"term" : { "user" : "kimchy" }
|
"term" : { "product" : "chocolate" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
If any of the indices that are queried doesn't have a mapping for `price`
|
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
|
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:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{
|
{
|
||||||
|
@ -193,6 +237,7 @@ Allow to sort by `_geo_distance`. Here is an example:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -209,6 +254,7 @@ The following formats are supported in providing the coordinates:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{
|
{
|
||||||
|
@ -227,6 +273,7 @@ The following formats are supported in providing the coordinates:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
===== Lat Lon as String
|
===== Lat Lon as String
|
||||||
|
|
||||||
|
@ -234,6 +281,7 @@ Format in `lat,lon`.
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{
|
{
|
||||||
|
@ -249,11 +297,13 @@ Format in `lat,lon`.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
===== Geohash
|
===== Geohash
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{
|
{
|
||||||
|
@ -269,6 +319,7 @@ Format in `lat,lon`.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
===== Lat Lon as Array
|
===== Lat Lon as Array
|
||||||
|
|
||||||
|
@ -277,6 +328,7 @@ conform with http://geojson.org/[GeoJSON].
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"sort" : [
|
"sort" : [
|
||||||
{
|
{
|
||||||
|
@ -292,6 +344,7 @@ conform with http://geojson.org/[GeoJSON].
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
|
|
||||||
==== Multiple reference points
|
==== Multiple reference points
|
||||||
|
@ -316,9 +369,10 @@ Allow to sort based on custom scripts, here is an example:
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"query" : {
|
"query" : {
|
||||||
....
|
"term" : { "user" : "kimchy" }
|
||||||
},
|
},
|
||||||
"sort" : {
|
"sort" : {
|
||||||
"_script" : {
|
"_script" : {
|
||||||
|
@ -334,6 +388,7 @@ Allow to sort based on custom scripts, here is an example:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
|
|
||||||
==== Track Scores
|
==== Track Scores
|
||||||
|
@ -343,6 +398,7 @@ When sorting on a field, scores are not computed. By setting
|
||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
GET /_search
|
||||||
{
|
{
|
||||||
"track_scores": true,
|
"track_scores": true,
|
||||||
"sort" : [
|
"sort" : [
|
||||||
|
@ -355,6 +411,7 @@ When sorting on a field, scores are not computed. By setting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
// CONSOLE
|
||||||
|
|
||||||
==== Memory Considerations
|
==== Memory Considerations
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue