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
|
||||
field name for `_score` to sort by score, and `_doc` to sort by index order.
|
||||
|
||||
Assuming the following index mapping:
|
||||
|
||||
[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" : [
|
||||
{ "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.
|
||||
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
|
||||
|
||||
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.
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
curl -XPOST 'localhost:9200/_search' -d '{
|
||||
PUT /my_index/my_type/1
|
||||
{
|
||||
"product": "chocolate",
|
||||
"price": [20, 4]
|
||||
}
|
||||
|
||||
POST /_search
|
||||
{
|
||||
"query" : {
|
||||
...
|
||||
"term" : { "product" : "chocolate" }
|
||||
},
|
||||
"sort" : [
|
||||
{"price" : {"order" : "asc", "mode" : "avg"}}
|
||||
]
|
||||
}'
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
[[nested-sorting]]
|
||||
==== Sorting within nested objects.
|
||||
|
@ -101,9 +138,10 @@ The `nested_path` needs to be specified; otherwise, elasticsearch doesn't know o
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
curl -XPOST 'localhost:9200/_search' -d '{
|
||||
POST /_search
|
||||
{
|
||||
"query" : {
|
||||
...
|
||||
"term" : { "product" : "chocolate" }
|
||||
},
|
||||
"sort" : [
|
||||
{
|
||||
|
@ -117,8 +155,9 @@ curl -XPOST 'localhost:9200/_search' -d '{
|
|||
}
|
||||
}
|
||||
]
|
||||
}'
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
Nested sorting is also supported when sorting by
|
||||
scripts and sorting by geo distance.
|
||||
|
@ -132,15 +171,17 @@ will be used for missing docs as the sort value). For example:
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"sort" : [
|
||||
{ "price" : {"missing" : "_last"} },
|
||||
{ "price" : {"missing" : "_last"} }
|
||||
],
|
||||
"query" : {
|
||||
"term" : { "user" : "kimchy" }
|
||||
"term" : { "product" : "chocolate" }
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
NOTE: If a nested inner object doesn't match with
|
||||
the `nested_filter` then a missing value is used.
|
||||
|
@ -155,15 +196,17 @@ example of how it can be used:
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"sort" : [
|
||||
{ "price" : {"unmapped_type" : "long"} },
|
||||
{ "price" : {"unmapped_type" : "long"} }
|
||||
],
|
||||
"query" : {
|
||||
"term" : { "user" : "kimchy" }
|
||||
"term" : { "product" : "chocolate" }
|
||||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
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
|
||||
|
@ -176,6 +219,7 @@ Allow to sort by `_geo_distance`. Here is an example:
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"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]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"sort" : [
|
||||
{
|
||||
|
@ -227,6 +273,7 @@ The following formats are supported in providing the coordinates:
|
|||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
===== Lat Lon as String
|
||||
|
||||
|
@ -234,6 +281,7 @@ Format in `lat,lon`.
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"sort" : [
|
||||
{
|
||||
|
@ -249,11 +297,13 @@ Format in `lat,lon`.
|
|||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
===== Geohash
|
||||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"sort" : [
|
||||
{
|
||||
|
@ -269,6 +319,7 @@ Format in `lat,lon`.
|
|||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
===== Lat Lon as Array
|
||||
|
||||
|
@ -277,6 +328,7 @@ conform with http://geojson.org/[GeoJSON].
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"sort" : [
|
||||
{
|
||||
|
@ -292,6 +344,7 @@ conform with http://geojson.org/[GeoJSON].
|
|||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
|
||||
==== Multiple reference points
|
||||
|
@ -316,9 +369,10 @@ Allow to sort based on custom scripts, here is an example:
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"query" : {
|
||||
....
|
||||
"term" : { "user" : "kimchy" }
|
||||
},
|
||||
"sort" : {
|
||||
"_script" : {
|
||||
|
@ -334,6 +388,7 @@ Allow to sort based on custom scripts, here is an example:
|
|||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
|
||||
==== Track Scores
|
||||
|
@ -343,6 +398,7 @@ When sorting on a field, scores are not computed. By setting
|
|||
|
||||
[source,js]
|
||||
--------------------------------------------------
|
||||
GET /_search
|
||||
{
|
||||
"track_scores": true,
|
||||
"sort" : [
|
||||
|
@ -355,6 +411,7 @@ When sorting on a field, scores are not computed. By setting
|
|||
}
|
||||
}
|
||||
--------------------------------------------------
|
||||
// CONSOLE
|
||||
|
||||
==== Memory Considerations
|
||||
|
||||
|
|
Loading…
Reference in New Issue