# Elasticsearch relevance score calculation

**URL:** https://discuss.elastic.co/t/elasticsearch-relevance-score-calculation/172857
**Category:** Elasticsearch
**Created:** [March 18, 2019, 7:48pm UTC](https://discuss.elastic.co/t/elasticsearch-relevance-score-calculation/172857 "2019-03-18T19:48:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)
#### Post date: [March 18, 2019, 7:48pm UTC](https://discuss.elastic.co/t/elasticsearch-relevance-score-calculation/172857/1 "2019-03-18T19:48:20Z")

</div>

Hi there,  
I am newbie to Elasticsearch and got lost on how ES calculates the relevance score for the query result.  
I am using ES v 6.0 and by running the explain API as follows, i got nothing but the following

GET /myinde/doc/\_explain  
{  
"query" : {  
"match" : { "audoc.authlast" : "mark" }  
}  
}

{  
"\_index": "myindex",  
"\_type": "doc",  
"\_id": "\_explain",  
"\_version": 5,  
"result": "updated",  
"\_shards": {  
"total": 2,  
"successful": 2,  
"failed": 0  
},  
"\_seq\_no": 16266195,  
"\_primary\_term": 2  
}

please, can you elaborate how is ES calculates the relevance score? and how to explain the relevance in the result above?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 19, 2019, 10:00am UTC](https://discuss.elastic.co/t/elasticsearch-relevance-score-calculation/172857/2 "2019-03-19T10:00:32Z")

</div>

That's a little unfortunate. What you've done with that request is add a document to an index.  
The correct URL for the [explain API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html) includes a doc ID on the end of the URL. This is missing from your URL so it has assumed `_explain` is an ID (underscores are legal prefixes for doc IDs but in an ideal world we wouldn't allow that).  
Presumably you issued the command in Kibana Console and because browsers don't like sending bodies with GETs the request is silently converted into a POST to send the body.

So all of the above was converted into a request to index a document with a `query` field in the body. This may have unfortunately updated your index mapping too, introducing new fields.  
The response is not an explanation of a match but a receipt for your newly created document.

---

<div class="post-metadata">

### Author: ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)
#### Post date: [April 1, 2019, 5:10pm UTC](https://discuss.elastic.co/t/elasticsearch-relevance-score-calculation/172857/3 "2019-04-01T17:10:54Z")

</div>

Thank you Mark! yes, i have realized i have to pass explicit doc\_id for the explain API later. unfortunately, the previous GET was indexed as a document as per your explanation.

I will need to modify the mapping index because it was updated. though i still not sure how relevancy works. below was the modified explain i did run. what i am confused about that first records with high score having authors with high frequencey (i.e. 7) and with special characters in their name (i.e. M'ark), while 5th/7th record in result having same frequency (7) and without special characters in their name. i thought the authors without special characters would show first (i.e. Mark)!!

GET /evcafereindex2/author/aut\_M64d649f815d2e14138dM725110178163220/\_explain  
{

```
  "query" : {

    "match" : { "audoc.authlast": {"query": "mark" }}

  }

```

}

result

{  
"\_index": "myindex",  
"\_type": "author",  
"\_id": "123",  
"matched": true,  
"explanation": {  
"value": 14.357386,  
"description": "sum of:",  
"details": [  
{  
"value": 14.357386,  
"description": "weight(audoc.authlast:mark in 127662) [PerFieldSimilarity], result of:",  
"details": [  
{  
"value": 14.357386,  
"description": "score(doc=127662,freq=7.0 = termFreq=7.0\n), product of:",  
"details": [  
{  
"value": 9.65505,  
"description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",  
"details": [  
{  
"value": 150,  
"description": "docFreq",  
"details":   
},  
{  
"value": 2347855,  
"description": "docCount",  
"details":   
}  
]  
},  
{  
"value": 1.4870337,  
"description": "tfNorm, computed as (freq \* (k1 + 1)) / (freq + k1 \* (1 - b + b \* fieldLength / avgFieldLength)) from:",  
"details": [  
{  
"value": 7,  
"description": "termFreq=7.0",  
"details":   
},  
{  
"value": 1.2,  
"description": "parameter k1",  
"details":   
},  
{  
"value": 0.75,  
"description": "parameter b",  
"details":   
},  
{  
"value": 2.094112,  
"description": "avgFieldLength",  
"details":   
},  
{  
"value": 7.111111,  
"description": "fieldLength",  
"details":   
}  
]  
}  
]  
}  
]  
},  
{  
"value": 0,  
"description": "match on required clause, product of:",  
"details": [  
{  
"value": 0,  
"description": "# clause",  
"details":   
},  
{  
"value": 1,  
"description": "\_type:author",  
"details":   
}  
]  
}  
]  
}  
}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 29, 2019, 5:10pm UTC](https://discuss.elastic.co/t/elasticsearch-relevance-score-calculation/172857/4 "2019-04-29T17:10:55Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
