0
0
ElasticsearchHow-ToBeginner · 3 min read

How to Sort by Relevance Score in Elasticsearch

In Elasticsearch, search results are sorted by relevance score by default using the _score field. To explicitly sort by relevance, include "sort": [{"_score": {"order": "desc"}}] in your query to order results from highest to lowest relevance score.
📐

Syntax

The sort parameter in an Elasticsearch query controls the order of search results. To sort by relevance score, use the special field _score. The syntax looks like this:

  • "sort": an array specifying sorting rules.
  • {"_score": {"order": "desc"}}: sorts results by score in descending order (highest score first).

By default, Elasticsearch sorts by _score descending, so this is often implicit.

json
{
  "query": {
    "match": {
      "field_name": "search text"
    }
  },
  "sort": [
    {"_score": {"order": "desc"}}
  ]
}
💻

Example

This example shows a search query that matches documents containing "elasticsearch" in the "content" field and sorts the results by relevance score descending.

json
{
  "query": {
    "match": {
      "content": "elasticsearch"
    }
  },
  "sort": [
    {"_score": {"order": "desc"}}
  ]
}
Output
[ { "_id": "1", "_score": 3.5, "_source": {"content": "Elasticsearch tutorial"} }, { "_id": "2", "_score": 2.1, "_source": {"content": "Introduction to Elasticsearch"} }, { "_id": "3", "_score": 1.0, "_source": {"content": "Basic search guide"} } ]
⚠️

Common Pitfalls

Many users expect sorting by _score to always happen, but if you add other sort fields without including _score, Elasticsearch sorts by those fields and ignores relevance score.

Also, sorting by _score ascending ("order": "asc") will show least relevant results first, which is usually not desired.

json
{
  "query": {
    "match": {"content": "elasticsearch"}
  },
  "sort": [
    {"date": {"order": "desc"}}
  ]
}

// This sorts by date, ignoring relevance score.

{
  "query": {
    "match": {"content": "elasticsearch"}
  },
  "sort": [
    {"_score": {"order": "desc"}},
    {"date": {"order": "desc"}}
  ]
}

// This sorts first by relevance score, then by date if scores tie.
📊

Quick Reference

ConceptDescriptionExample
_scoreField representing relevance score{"sort": [{"_score": {"order": "desc"}}]}
Default sortingElasticsearch sorts by _score descending if no sort specifiedNo sort parameter needed
Sort by other fieldsOverrides _score sorting, relevance ignored{"sort": [{"date": {"order": "desc"}}]}
Combine sortingSort by _score then by another field{"sort": [{"_score": {"order": "desc"}}, {"date": {"order": "desc"}}]}

Key Takeaways

Elasticsearch sorts search results by relevance score (_score) descending by default.
Use the sort parameter with _score and order desc to explicitly sort by relevance.
Adding other sort fields without _score disables sorting by relevance.
Sorting _score ascending shows least relevant results first, usually unwanted.
Combine _score sorting with other fields by listing _score first in the sort array.