Given the following Elasticsearch query, what is the order of the returned documents by their price field?
{
"query": { "match_all": {} },
"sort": [
{ "price": { "order": "asc" } }
]
}Look at the order value inside the sort array.
The sort clause with order": "asc" sorts documents from the lowest to the highest value of the price field.
Consider this Elasticsearch query that sorts by a field rating which does not exist in any document. What will be the result?
{
"query": { "match_all": {} },
"sort": [
{ "rating": { "order": "desc" } }
]
}By default ('_last'), missing fields are treated as highest values.
Elasticsearch's default for missing sort values is '_last', treating them as higher than any existing value. For desc order, documents missing rating appear last.
Look at this Elasticsearch query that tries to sort by date descending but causes an error. What is the cause?
{
"query": { "match_all": {} },
"sort": [
{ "date": "desc" }
]
}Check the correct syntax for sorting by a field with order.
The correct syntax for sorting by a field with order is to use an object with the key order. Writing { "date": "desc" } is invalid. It should be { "date": { "order": "desc" } }.
Given this sort clause, how does Elasticsearch order the documents?
{
"sort": [
{ "price": { "order": "asc" } },
{ "rating": { "order": "desc" } }
]
}Think about the order of fields in the sort array.
Elasticsearch sorts documents by the first field in the sort array. If there are ties, it uses the next field to break ties, and so on. Here, it sorts by price ascending first, then by rating descending.
Consider documents with a nested field author.name. This query sorts by author.name.keyword ascending, treating missing values as last. What is the order of documents?
{
"query": { "match_all": {} },
"sort": [
{
"author.name.keyword": {
"order": "asc",
"missing": "_last"
}
}
]
}Check the missing parameter usage in sorting.
The missing": "_last" option tells Elasticsearch to put documents missing the field at the end of the sorted list. The order": "asc" sorts the existing values alphabetically from A to Z.