[Recipes] Elasticsearch Debugging Tips - Named Queries With Bool Query

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

  1. Give any other syntax of using named query than above.

  2. Give an example for using named query with filter context.

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream