9

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"}
        }
      ]
    }
  }
}
Aristata
  • 610
  • 2
  • 9
  • 18
  • see also [Nesting Boolean Queries](https://www.elastic.co/guide/en/elasticsearch/guide/master/combining-filters.html#_nesting_boolean_queries) – Ricardo Feb 09 '18 at 00:56

2 Answers2

12

The correctly nested boolean expression is shown below:

{
  "filter": {
    "or" : [{
       "and" : [
          { "term" : { "metropolian_area" : 16 } },
          { "term" : { "starts_at" : 0123213 } }
       ]
    }, {
       "and" : [
          { "term" : ... },
          { "term" : ... }
       ]
    }]
  }
}
mbj
  • 1,042
  • 9
  • 19
  • Do you happen to know the answer to this question? I've tried all sorts of things, but still had no luck. http://stackoverflow.com/questions/18647228/how-to-properly-write-boolean-or-logic-in-elasticsearch – Churro Sep 05 '13 at 23:14
  • I'm busy, can you ping me in #rom-rb at freenode later the week? I think I can help you, it looks easy. – mbj Sep 09 '13 at 12:42
  • 2
    Is there a way to provide the same functionality with "Boolean queries"? – Zouzias Jun 25 '14 at 08:03
  • 1
    http://www.elastic.co/guide/en/elasticsearch/guide/master/combining-filters.html#_nesting_boolean_filters – csdaraujo Apr 07 '15 at 23:29
6

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"
              }
            }
          ]
        }
      }
    }
  }
}
Dorjee Dhondup
  • 549
  • 6
  • 15