7

I have two distinct query, and I want to combine them with an 'OR'/'AND' in between. How do I do that?

For example, for the given queries I just want to run Query1 OR Query2 in the elasticsearch.

Query1:

{
   "query": {
      "filtered": {
         "query": { 
            "query_string":{  
               "query":"Batman",
               "default_operator":"AND",
               "fields"::[  
                  "Movies._all"
               ]
            }
         },
         "filter": {
            "bool": {
               "must": [  
                  {  
                     "query":{  
                        "filtered":{  
                           "filter":{  
                              "and":[  
                                 {  
                                    "term":{  
                                       "cast.firstName":"Christian "
                                    }
                                 },
                                 {  
                                    "term":{  
                                       "cast.lastName":"Bale"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

Query2:

{
   "query": {
      "filtered": {
         "query": { 
            "query_string":{  
               "query":"Dark Knight",
               "default_operator":"AND",
               "fields"::[  
                  "Movies._all"
               ]
            }
         },
         "filter": {
            "bool": {
               "must": [  
                  {  
                     "query":{  
                        "filtered":{  
                           "filter":{  
                              "and":[  
                                 {  
                                    "term":{  
                                       "director.firstName":"Christopher"
                                    }
                                 },
                                 {  
                                    "term":{  
                                       "director.lastName":"Nolan"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

1 Answer 1

16

You need to use a bool query Something like below will work fine -

{
  "query" : {
    "bool" : { 
      "must" : [
        { // Query1 },
        { // Query2}
      ]
    }
  }
}

Use must for AND and should for OR

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work as I would expect. It still returns documents even if one of the must clauses doesn't hit.

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.