Search code examples
elasticsearchelasticsearch-plugin

confusion about elasticsearch documentation about containing json for bool query


In the elasticsearch doc for a bool query at this link: https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-bool-query.html

It doesn't say the containing structure. If I just use bool the way they have it, it's totally wrong. I need to surround this with some silly combination of query/filter/ filtered query. I'm not sure what is the correct way to form a json query in elastic. The documents seem to be completely contradictory in many places about what goes where and how. Any elasticsearch experts out there that know about how to properly form a query?


Solution

  • First of all, there is a "bool" query, and a "bool" filter, and they go in different places and do slightly different things. As a general rule, if you can use a filter do it (many of them can be cached, and are a little faster even if not). If you need a "match" then you need a query.

    The example on the page you referenced could actually be used either way:

    As a query:

    POST /test_index/_search
    {
       "query": {
          "bool": {
             "must": {
                "term": {
                   "user": "kimchy"
                }
             },
             "must_not": {
                "range": {
                   "age": {
                      "from": 10,
                      "to": 20
                   }
                }
             },
             "should": [
                {
                   "term": {
                      "tag": "wow"
                   }
                },
                {
                   "term": {
                      "tag": "elasticsearch"
                   }
                }
             ],
             "minimum_should_match": 1,
             "boost": 1
          }
       }
    }
    

    Or as a filter (in a filtered query):

    POST /test_index/_search
    {
       "query": {
          "filtered": {
             "filter": {
                "bool": {
                   "must": {
                      "term": {
                         "user": "kimchy"
                      }
                   },
                   "must_not": {
                      "range": {
                         "age": {
                            "from": 10,
                            "to": 20
                         }
                      }
                   },
                   "should": [
                      {
                         "term": {
                            "tag": "wow"
                         }
                      },
                      {
                         "term": {
                            "tag": "elasticsearch"
                         }
                      }
                   ],
                   "minimum_should_match": 1
                }
             }
          }
       }
    }
    

    Also I totally get the frustration with the ES documents. I've been working with them for a couple of years now, and they don't seem to be getting any better. Maybe the people in charge of documentation just don't care all that much. The conspiracy theory view would be that bad documentation helps the company sell professional services.