Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Multiple Queries with 'OR' or 'AND' in Elasticsearch

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"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}
like image 387
Debashish Paul Avatar asked Feb 19 '15 01:02

Debashish Paul


People also ask

How do I merge two queries in Elasticsearch?

You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses. You may want to schearch for both the field you want, and then aggregate by the most important field.

Can Elasticsearch do joins?

Joining queriesedit Instead, Elasticsearch offers two forms of join which are designed to scale horizontally. Documents may contain fields of type nested . These fields are used to index arrays of objects, where each object can be queried (with the nested query) as an independent document.

Should VS must Elasticsearch?

must means: Clauses that must match for the document to be included. should means: If these clauses match, they increase the _score ; otherwise, they have no effect. They are simply used to refine the relevance score for each document.

What are compound queries?

compound query A query that uses set operators (UNION, UNION ALL, INTERSECT, or MINUS) to combine two or more simple or complex statements. Each simple or complex statement in a compound query is called a component query.


1 Answers

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

like image 93
Vineeth Mohan Avatar answered Nov 15 '22 16:11

Vineeth Mohan