Sorting results helps you see data in the order you want, like from highest to lowest or alphabetically.
Sorting results in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"sort": [
{ "field_name": { "order": "asc" } }
]
}Use asc for ascending order (smallest to largest or A to Z).
Use desc for descending order (largest to smallest or Z to A).
Examples
price field from lowest to highest.Elasticsearch
{
"sort": [
{ "price": { "order": "asc" } }
]
}date field from newest to oldest.Elasticsearch
{
"sort": [
{ "date": { "order": "desc" } }
]
}name field.Elasticsearch
{
"sort": [
{ "name.keyword": { "order": "asc" } }
]
}Sample Program
This query finds all documents and sorts them by the age field from highest to lowest.
Elasticsearch
{
"query": {
"match_all": {}
},
"sort": [
{ "age": { "order": "desc" } }
]
}Important Notes
If the field you sort by is text, use the keyword version (like name.keyword) to sort alphabetically.
You can sort by multiple fields by adding more objects inside the sort array.
Summary
Sorting arranges your search results in the order you want.
Use asc for ascending and desc for descending order.
You can sort by numbers, dates, or keywords for text fields.
Practice
1. What does the
sort parameter do in an Elasticsearch query?easy
Solution
Step 1: Understand the purpose of
Thesortsortparameter is used to order search results by a field in ascending or descending order.Step 2: Differentiate from other parameters
Filtering narrows results, mapping changes structure, and deleting removes data, none of which are sorting.Final Answer:
It arranges the search results in a specified order. -> Option AQuick Check:
Sorting = arranging results [OK]
Hint: Sort means order your results by a field [OK]
Common Mistakes:
- Confusing sorting with filtering
- Thinking sort deletes or changes data
- Mixing sort with index settings
2. Which of the following is the correct syntax to sort results by the field
price in descending order?easy
Solution
Step 1: Check Elasticsearch sort syntax
Sorting uses an array of objects with field name and order as "asc" or "desc" strings.Step 2: Validate each option
"sort": [{"price": "desc"}] uses correct JSON array and "desc" keyword. Others use invalid keywords or formats.Final Answer:
"sort": [{"price": "desc"}] -> Option AQuick Check:
Correct syntax uses array and "desc" [OK]
Hint: Use array with field and "asc" or "desc" strings [OK]
Common Mistakes:
- Using wrong keywords like "descending" or "down"
- Not using an array for sort
- Incorrect JSON object structure
3. Given this Elasticsearch query snippet, what is the order of the returned documents by
age?
{
"sort": [{"age": "asc"}]
}medium
Solution
Step 1: Understand the meaning of "asc" in sorting
"asc" means ascending order, so smaller values come first.Step 2: Apply to the field "age"
Sorting by age ascending means documents with lower age values appear before higher ones.Final Answer:
Documents are sorted from youngest to oldest. -> Option DQuick Check:
"asc" = ascending = smallest to largest [OK]
Hint: "asc" means smallest to largest values [OK]
Common Mistakes:
- Confusing "asc" with descending order
- Thinking sorting is random
- Assuming alphabetical sort on numbers
4. Identify the error in this Elasticsearch sort snippet:
{
"sort": {"date": "asc"}
}medium
Solution
Step 1: Check the required structure for sort
Sort expects an array of objects, even if sorting by one field.Step 2: Analyze the given snippet
The snippet uses an object directly, missing the array brackets [].Final Answer:
Sort must be an array, not a single object. -> Option BQuick Check:
Sort = array of objects [OK]
Hint: Sort always uses an array, even for one field [OK]
Common Mistakes:
- Using object instead of array for sort
- Wrong order keywords like "ascending"
- Thinking sort can't be used on dates
5. You want to sort search results first by
category alphabetically ascending, then by price descending. Which is the correct sort clause?hard
Solution
Step 1: Understand multi-level sorting order
Sorting applies fields in order: first field sorts primary, second field breaks ties.Step 2: Match required order
First by category ascending, then by price descending means category asc first, price desc second.Step 3: Validate syntax
Sort must be an array of objects with field and order pairs.Final Answer:
"sort": [{"category": "asc"}, {"price": "desc"}] -> Option CQuick Check:
Primary asc category, secondary desc price [OK]
Hint: List sort fields in order inside array [OK]
Common Mistakes:
- Reversing sort order of fields
- Using object instead of array for multiple fields
- Mixing ascending and descending incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
