Skip to content

Commit

Permalink
Add CONSOLE annotation to sort documentation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Isabel Drost-Fromm committed May 17, 2016
1 parent 9d5537d commit 48ea913
Showing 1 changed file with 69 additions and 12 deletions.
81 changes: 69 additions & 12 deletions docs/reference/search/request/sort.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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" : [
{
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -176,6 +219,7 @@ Allow to sort by `_geo_distance`. Here is an example:

[source,js]
--------------------------------------------------
GET /_search
{
"sort" : [
{
Expand All @@ -193,6 +237,7 @@ Allow to sort by `_geo_distance`. Here is an example:
}
}
--------------------------------------------------
// CONSOLE



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

[source,js]
--------------------------------------------------
GET /_search
{
"sort" : [
{
Expand All @@ -227,13 +273,15 @@ The following formats are supported in providing the coordinates:
}
}
--------------------------------------------------
// CONSOLE

===== Lat Lon as String

Format in `lat,lon`.

[source,js]
--------------------------------------------------
GET /_search
{
"sort" : [
{
Expand All @@ -249,11 +297,13 @@ Format in `lat,lon`.
}
}
--------------------------------------------------
// CONSOLE

===== Geohash

[source,js]
--------------------------------------------------
GET /_search
{
"sort" : [
{
Expand All @@ -269,6 +319,7 @@ Format in `lat,lon`.
}
}
--------------------------------------------------
// CONSOLE

===== Lat Lon as Array

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

[source,js]
--------------------------------------------------
GET /_search
{
"sort" : [
{
Expand All @@ -292,6 +344,7 @@ conform with http://geojson.org/[GeoJSON].
}
}
--------------------------------------------------
// CONSOLE


==== Multiple reference points
Expand All @@ -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" : {
Expand All @@ -334,6 +388,7 @@ Allow to sort based on custom scripts, here is an example:
}
}
--------------------------------------------------
// CONSOLE


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

[source,js]
--------------------------------------------------
GET /_search
{
"track_scores": true,
"sort" : [
Expand All @@ -355,6 +411,7 @@ When sorting on a field, scores are not computed. By setting
}
}
--------------------------------------------------
// CONSOLE

==== Memory Considerations

Expand Down

0 comments on commit 48ea913

Please sign in to comment.