Home  >  Blog  >   Elasticsearch  > 

Learn Elasticsearch Sorting

Rating: 5



Allows to add one or more sort 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.

Learn how to use Elasticsearch, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for FreeElasticsearch Training”.  Demo!

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.

MindMajix Youtube Channel

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

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

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
Elasticsearch TrainingMay 21 to Jun 05
Elasticsearch TrainingMay 23 to Jun 07
Elasticsearch TrainingMay 28 to Jun 12
Elasticsearch TrainingMay 30 to Jun 14
Last updated: 08 Apr 2022
About Author
Ravindra Savaram

Ravindra Savaram is a Content 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.

Recommended Courses

1 /13