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

2 Answers 2

12

The correctly nested boolean expression is shown below:

{
  "filter": {
    "or" : [{
       "and" : [
          { "term" : { "metropolian_area" : 16 } },
          { "term" : { "starts_at" : 0123213 } }
       ]
    }, {
       "and" : [
          { "term" : ... },
          { "term" : ... }
       ]
    }]
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Do you happen to know the answer to this question? I've tried all sorts of things, but still had no luck. stackoverflow.com/questions/18647228/…
I'm busy, can you ping me in #rom-rb at freenode later the week? I think I can help you, it looks easy.
Is there a way to provide the same functionality with "Boolean queries"?
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.