Learn Elasticsearch Sorting

Rating: 5
  
 
21198

What is Sorting?

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.

What is Elasticsearch Sorting?

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.

Sort By Descending Order

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"
       }
    ]
 }'

MindMajix Youtube Channel

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

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!
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Elasticsearch TrainingMar 30 to Apr 14View Details
Elasticsearch TrainingApr 02 to Apr 17View Details
Elasticsearch TrainingApr 06 to Apr 21View Details
Elasticsearch TrainingApr 09 to Apr 24View Details
Last updated: 03 Apr 2023
About Author

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.

read more
Recommended Courses

1 / 15