0
0
Elasticsearchquery~20 mins

Sorting results in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Elasticsearch Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output order of this Elasticsearch sort query?

Given the following Elasticsearch query, what is the order of the returned documents by their price field?

Elasticsearch
{
  "query": { "match_all": {} },
  "sort": [
    { "price": { "order": "asc" } }
  ]
}
ADocuments sorted from lowest to highest price
BDocuments sorted from highest to lowest price
CDocuments sorted alphabetically by price field
DDocuments returned in random order
Attempts:
2 left
💡 Hint

Look at the order value inside the sort array.

Predict Output
intermediate
2:00remaining
What happens if you sort by a non-existent field in Elasticsearch?

Consider this Elasticsearch query that sorts by a field rating which does not exist in any document. What will be the result?

Elasticsearch
{
  "query": { "match_all": {} },
  "sort": [
    { "rating": { "order": "desc" } }
  ]
}
AElasticsearch throws an error about missing field
BDocuments are returned in original order because <code>rating</code> is missing
CDocuments are sorted with missing <code>rating</code> treated as highest values
DDocuments are sorted with missing <code>rating</code> treated as lowest values
Attempts:
2 left
💡 Hint

By default ('_last'), missing fields are treated as highest values.

🔧 Debug
advanced
2:00remaining
Why does this Elasticsearch sort query cause an error?

Look at this Elasticsearch query that tries to sort by date descending but causes an error. What is the cause?

Elasticsearch
{
  "query": { "match_all": {} },
  "sort": [
    { "date": "desc" }
  ]
}
AThe <code>desc</code> value must be uppercase
BThe <code>date</code> field does not exist in the index
CSorting by date requires a script, not a field name
DThe sort syntax is invalid; the order must be inside an object with key <code>order</code>
Attempts:
2 left
💡 Hint

Check the correct syntax for sorting by a field with order.

🧠 Conceptual
advanced
2:00remaining
How does Elasticsearch handle sorting by multiple fields?

Given this sort clause, how does Elasticsearch order the documents?

{
  "sort": [
    { "price": { "order": "asc" } },
    { "rating": { "order": "desc" } }
  ]
}
ADocuments are sorted first by <code>price</code> ascending, then by <code>rating</code> descending for ties
BDocuments are sorted by <code>rating</code> descending, then by <code>price</code> ascending
CDocuments are sorted randomly ignoring the fields
DDocuments are sorted by the sum of <code>price</code> and <code>rating</code>
Attempts:
2 left
💡 Hint

Think about the order of fields in the sort array.

Predict Output
expert
2:00remaining
What is the output of this Elasticsearch sort with nested field and missing values?

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?

Elasticsearch
{
  "query": { "match_all": {} },
  "sort": [
    {
      "author.name.keyword": {
        "order": "asc",
        "missing": "_last"
      }
    }
  ]
}
ADocuments with missing <code>author.name.keyword</code> appear first, others sorted Z to A
BDocuments with <code>author.name.keyword</code> missing appear at the end, others sorted A to Z
CDocuments are sorted by <code>author.name.keyword</code> descending ignoring missing
DElasticsearch throws an error because <code>missing</code> is not a valid parameter
Attempts:
2 left
💡 Hint

Check the missing parameter usage in sorting.