# Combining Multiple Queries with 'OR' or 'AND'

**URL:** <https://discuss.elastic.co/t/combining-multiple-queries-with-or-or-and/22279>\
**Category:** Elasticsearch\
**Created:** [February 19, 2015, 11:29pm UTC](https://discuss.elastic.co/t/combining-multiple-queries-with-or-or-and/22279 "2015-02-19T23:29:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Debashish\_Paul](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@Debashish\_Paul](https://discuss.elastic.co/u/Debashish_Paul)\
**Post date:** [February 19, 2015, 11:29pm UTC](https://discuss.elastic.co/t/combining-multiple-queries-with-or-or-and/22279/1 "2015-02-19T23:29:21Z")

</div>

Hi,  
Question

I am trying to combine 2 user search queries using AND and OR as operation.  
I am aware of combining queries where we can merge filters, but I want to  
merge entire queries like { {BIG Elastic Query1} AND {BIG Elastic Query2} }.  
Details

For instance say a user performs a search for "batman in movies type with  
filters of Christian and Bale" and another query "Dark Knight in type  
tvshows with filters of Christopher Nolan". I want to combine both queries  
so I can look for both batman movies and Dark Knight tvshows, but not Dark  
knight movies or batman tvshows.

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

{  
"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":[  
"tvshows.\_all"  
]  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"query":{  
"filtered":{  
"filter":{  
"and":[  
{  
"term":{  
"director.firstName":"Christopher"  
}  
},  
{  
"term":{  
"director.lastName":"Nolan"  
}  
}  
]  
}  
}  
}  
}  
]  
}  
}  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [February 20, 2015, 6:59am UTC](https://discuss.elastic.co/t/combining-multiple-queries-with-or-or-and/22279/2 "2015-02-20T06:59:51Z")

</div>

Hi,

## bool query should work:

## { "query": { "bool": { "should": [{ "filtered": { // Query1 "query": {...}, "filter": {...} } }, { "filtered": { // Query2 "query": {...}, "filter": {...} } }] } } }

Masaru

On Fri, Feb 20, 2015 at 8:29 AM, Debashish Paul [shimanbb@gmail.com](mailto:shimanbb@gmail.com) wrote:

> Hi,  
> Question
> 
> I am trying to combine 2 user search queries using AND and OR as  
> operation. I am aware of combining queries where we can merge filters, but  
> I want to merge entire queries like { {BIG Elastic Query1} AND {BIG Elastic  
> Query2} }.  
> Details
> 
> For instance say a user performs a search for "batman in movies type with  
> filters of Christian and Bale" and another query "Dark Knight in type  
> tvshows with filters of Christopher Nolan". I want to combine both queries  
> so I can look for both batman movies and Dark Knight tvshows, but not Dark  
> knight movies or batman tvshows.
> 
> For example, for the given queries  
> I just want to run Query1 OR Query2 in the elasticsearch.  
> Query 1:
> 
> {  
> "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":[  
> "tvshows.\_all"  
> ]  
> }  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "query":{  
> "filtered":{  
> "filter":{  
> "and":[  
> {  
> "term":{  
> "director.firstName":"Christopher"  
> }  
> },  
> {  
> "term":{  
> "director.lastName":"Nolan"  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGmu3c1GF4sQX\_Pa7RRL2xOFz0gXs2r5-dtx55AsToxCgWtKcQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGmu3c1GF4sQX_Pa7RRL2xOFz0gXs2r5-dtx55AsToxCgWtKcQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:31am UTC](https://discuss.elastic.co/t/combining-multiple-queries-with-or-or-and/22279/3 "2017-07-06T00:31:18Z")

</div>


