Allows adding one or more sorts on specific fields. Each sort can be reversed as well. The sort is defined on a per-field level, with a special field name for _score
to sort by score, and _doc
to sort by index order.
In order to sort by relevance, we need to represent relevance as a value. In Elasticsearch, the relevance score is represented by the floating-point number returned in the search results as the _score, so the default sort order is _score descending.
In the previous example, we searched for movies from 1962. In this case, all hits have the same score. They both match the query equally. Unless told otherwise, ElasticSearch will then resort to sorting the results by their IDs. In cases such as these, or when we don’t want to sort by the score, we can tell ElasticSearch how to sort the results by adding a sort property to the request body.
If you want to enrich your career and become a professional in Elasticsearch, then enroll in "Elasticsearch Training". This course will help you to achieve excellence in this domain. |
The sort property’s value should be an array with JSON objects. The first object describes the primary way the results should be sorted, the second how results with the same value for the primary way should be sorted and so on.
In its simplest form, an object in the sort array is an object with a single property whose name matches the field to sort by and whose value is the order by which to sort, “asc” for ascending or “desc” for descending.
So, a request searching for all movies and sorting them by newest first can look like this:
A search request that searches for everything in the ‘movies’ index and sorts the result based on the ‘year’ property in descending order.
curl -XPOST "https://localhost:9200/movies/_search" -d'
{
"query": {
"match_all": {}
},
"sort": [
{
"year": "desc"
}
]
}'
When there’s a sort property in the search request body, ElasticSearch won’t only apply the sorting rule(s) found in it. It will also extend the hit objects with a property explaining what value(s) were used for the hit during sorting.
Search results in Sense when using a sort property in the request body.
There is more to sorting in ElasticSearch. For instance, we can re-introduce sorting by score by adding the string “_score” as one of the values in the sort array and we can tell ElasticSearch how it should handle missing values.
Explore Elasticsearch Sample Resumes! Download & Edit, Get Noticed by Top Employers! |
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Elasticsearch Training | Dec 24 to Jan 08 | View Details |
Elasticsearch Training | Dec 28 to Jan 12 | View Details |
Elasticsearch Training | Dec 31 to Jan 15 | View Details |
Elasticsearch Training | Jan 04 to Jan 19 | View Details |
Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.