Assuming I have a document with three fields: name
, company
, email
each one mapped with edge-ngram
{
"name": "John",
"company": "John's company",
"email": "johndoe@gmail.com"
}
When searching for "john" I want to be able to get each field score individually
{
"query": {
"bool": {
"should": [
{ "match": { "name": "john" }},
{ "match": { "company": "john" }},
{ "match": { "email": "john" }}
]
}
}
}
In this example the score from each match
clause is added together, then divided by the number of match
clauses. So is there anyway to obtain the score from each match
clause individually not just the final score for the whole document?
I think setting "explain": true is also not ideal since it provides very low-level details of scoring (inefficient and difficult to parse).
Elasticsearch recently added the ability to return the score of named queries in #94564
We can use named queries to track which queries matched returned documents and return the score for each named query by setting the request parameter named include_named_queries_score
which controls whether scores associated with the matched queries are returned or not.
The final query would look like this:
GET /_search?include_named_queries_score
{
"query": {
"bool": {
"should": [
{ "match": { "name": { "query": "john", "_name": "name" } } },
{ "match": { "company": { "query": "john", "_name": "company" } } },
{ "match": { "email": { "query": "john", "_name": "email" } } }
]
}
}
}