Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
        }
      ]
    }
  }
}
like image 774
Aristata Avatar asked Apr 26 '13 19:04

Aristata


1 Answers

The correctly nested boolean expression is shown below:

{
  "filter": {
    "or" : [{
       "and" : [
          { "term" : { "metropolian_area" : 16 } },
          { "term" : { "starts_at" : 0123213 } }
       ]
    }, {
       "and" : [
          { "term" : ... },
          { "term" : ... }
       ]
    }]
  }
}
like image 141
mbj Avatar answered Oct 22 '22 22:10

mbj