Describe the feature:
When building complex bool queries, it can be important to know which ones of the "should" clauses in the query caused a match.
Example use-case: building a simple recommendation feature, which recommends documents to users based on different criteria e.g. "matches a tag you are subscribed to" or "one of your friends has saved this document" or "similar to documents you have saved in the past" or "geolocation in the document matches your polygon-saved-search".
This feature can be constructed using a bool query, with each of the criteria provided as a separate should clause. This allows efficient retrieval and pagination. There's just one catch: if we want to tell the user "returned this document because your friend saved it and it's in your polygon-saved-search" we need to do a bunch of additional processing outside of Elasticsearch to figure out which of the clauses matched.
As far as I can tell, there is no way to get Elasticsearch to indicate which of the terms in a bool query matched the returned documents. This would be an extremely useful additional feature.
Here's one suggestion for how the syntax might work:
{
"bool": {
"must": {
"term": {
"user": "kimchy"
}
},
"should": [
{
"term": {
"tag": "wow"
},
"label": "wow"
},
{
"term": {
"tag": "elasticsearch"
},
"label": "elasticsearch"
},
{
"range": {
"age": {
"from": 10,
"to": 20
}
},
"label": "agerange"
}
],
"minimum_should_match": 1,
}
}
Then the hits returned could look something like this:
{
"hits": [
{
"_index": "my-index",
"_type": "doc",
"_id": "9314",
"_score": null,
"_matched_labels": ["agerange", "wow"]
}
]
}
How about Named Queries?
Fantastic! New feature request: link to that page from the docs for the bool query. I must have spent nearly an hour searching for a solution to this without stumbling across that page.
https://github.com/elastic/elasticsearch/pull/17255 - pull request to add a link to the named queries documentation from the bool query page.
@mattweber (long time no see) do you know of any way to get the score of the match rather than just whether or not the query matched?
@JnBrymn Hello! Unfortunately, I don't. #17116 might help, but I think you already have seen it.
Most helpful comment
How about Named Queries?