Nested boolean queries in elastic search

elasticsearch

Solution

`and`, `or`, and `not` queries are deprecated in elasticsearch 2.x. Elasticsearch documentation recommends the `bool` query instead. For instance, you could write your nested bool query as follow in filter context:

{
  "query":{
    "bool":{
      "must":[
        {
          "term":{
            "user":"a"
          }
        },
        {
          "term":{
            "metropolitan_area":"16"
          }
        }
      ],
      "filter":{
        "bool":{
          "should":[
            {
              "term":{
                "user":"519"
              }
            },
            {
              "term":{
                "user":"6"
              }
            },
            {
              "term":{
                "user":"5"
              }
            },
            {
              "term":{
                "user":"36"
              }
            },
            {
              "term":{
                "starts_at":"05072013"
              }
            },
            {
              "term":{
                "starts_at":"blank"
              }
            }
          ]
        }
      }
    }
  }
}

Problem

I am trying to make a query in elastic search that will do the following: I want it to check for a result that has (metropolitan_area of 16 AND starts_at of 05072013) OR (metropolitan_id of 16 AND starts_at "blank". This is my current query, but I feel like it needs to be nested somehow and I am unsure how to do that. ``` { "query" : { "bool" : { "must" : [ { "term" : {"user" : "a"} }, { "term" :{"metropolitan_area" : "16"} } ], "must_not" : [], "should" : [] } }, "filter" : { "or" : { "filters" : [ { "term" : {"user":"519"} }, { "term" : {"user":"6"} }, { "term" : {"user":"5"} }, { "term" : {"user":"36"} }, { "term" : {"starts_at":"05072013"} }, { "term" : {"starts_at":"blank"} } ] } } } ```

Original source

Related problems