AI Hints
Home›Elasticsearch Tutorial›Relevance and Scoring
Elasticsearch · Lesson 62 of 120

The explain API

Show your working.

⏱ 2 min read▶ Video at 58:43🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Why did the fleece jacket beat the rain jacket? Don't guess. Add explain true, and Elasticsearch shows how it calculated every score.

  • Why this order?
  • Don't guess
  • explain: true

Explain a score

We search the name for rain jacket, and set explain to true. We keep only the score, and the top lines of the explanation. Run it. The score is a sum of two parts, one for rain, and one for jacket.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source.name,hits.hits._explanation.value,hits.hits._explanation.details.description
{
  "query": { "match": { "name": "rain jacket" } },
  "explain": true,
  "size": 1,
  "_source": ["name"]
}
Output
{
  "hits": {
    "hits": [
      {
        "_source": {"name": "Storm Rain Jacket"},
        "_explanation": {
          "value": 4.7802,
          "details": [
            {
              "description": "weight(name:rain in 18) [PerFieldSimilarity], result of:"
            },
            {
              "description": "weight(name:jacket in 18) [PerFieldSimilarity], result
              of:"
            }
          ]
        }
      }
    ]
  }
}
Also uses _load
catalog

Reading an explanation

The top value is the final score. Below it, each matching term has its own weight. Each weight shows the idf, the term frequency, and the field length parts. It's long, but it never lies.

keyvalue
valuethe final score
detailsone per term
weightidf × tf
Usedebug rankings

Summary

Explain true shows exactly how each score was calculated. Use it when the ranking surprises you. Next, making some fields count more.

Key takeaways
  • explain: true
  • Sum of terms
  • Debug
Next up: Field Boosting — name matters more