简体   繁体   中英

Nested boolean queries in elastic search

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"}
        }
      ]
    }
  }
}

The correctly nested boolean expression is shown below:

{
  "filter": {
    "or" : [{
       "and" : [
          { "term" : { "metropolian_area" : 16 } },
          { "term" : { "starts_at" : 0123213 } }
       ]
    }, {
       "and" : [
          { "term" : ... },
          { "term" : ... }
       ]
    }]
  }
}

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"
              }
            }
          ]
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM