Problem:
Use named queries for debugging bool queries.
Solution Summary:
To distinguish different matched queries in case of a nested bool query, we can use named queries within each nexted types (must, must_not, should). Each filter and query can accept a _name in its top level definition. The search response will include for each hit the matched_queries it matched on. The tagging of queries and filters make most sense for the bool query.
We can also use named queries with term queries.
Prerequisites:
Set up accounts index from accounts.json as explained here.
Solution Steps:
Initial State
GET /accounts/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "lane"
}
}
],
"should": [
{
"match": {
"address": "mill"
}
}
]
}
}
}
Using query element to specify name
GET /accounts/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": {
"query": "lane",
"_name": "match_lane"
}
}
}
],
"should": [
{
"match": {
"address": {
"query": "mill",
"_name": "should_lane"
}
}
}
]
}
}
}
Result Explanation
There will be a new section "matched_queries". Following are the values for some of the documents for above query.
...
"matched_queries": [
"should_lane",
"match_lane"
]
...
"matched_queries": [
"match_lane"
]
...
Note on should query
With should, the clause (query) should appear in the matching document.
If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. Note that there are documents in the above results that has not matched with the should clause.
If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.
TODO
-
Give any other syntax of using named query than above.
-
Give an example for using named query with filter context.
Recent comments