# OR query

**URL:** <https://discuss.elastic.co/t/or-query/15060>\
**Category:** Elasticsearch\
**Created:** [December 31, 2013, 5:37am UTC](https://discuss.elastic.co/t/or-query/15060 "2013-12-31T05:37:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul1/32/46819_2.png) [@paul1](https://discuss.elastic.co/u/paul1)\
**Post date:** [December 31, 2013, 5:37am UTC](https://discuss.elastic.co/t/or-query/15060/1 "2013-12-31T05:37:06Z")

</div>

My query is as below , which gives me all the colleges with state code  
"MA" i want all the colleges that are in "MA" or "NY" how to add OR filter

{  
"query": {  
"filtered": {  
"query": {  
"nested": {  
"path": "programs",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"programs.progName": "Computer and Information Sciences"  
}  
},  
{  
"range": {  
"programs.Bachelor": {  
"gt": 0  
}  
}  
}  
]  
}  
}  
}  
},  
"filter": {  
"and": {  
"filters": [  
{  
"bool": {  
"must": [  
{  
"term": {  
"state": "MA"  
}  
},  
{  
"range": {  
"costOutofstateTution": {  
"gte": 0,  
"lte": 30000  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}  
}  
}

--  
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/9614e30f-60e8-44cc-b614-6e5c18f2bc22%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9614e30f-60e8-44cc-b614-6e5c18f2bc22%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![paul1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul1/32/46819_2.png) [@paul1](https://discuss.elastic.co/u/paul1)\
**Post date:** [December 31, 2013, 6:03am UTC](https://discuss.elastic.co/t/or-query/15060/2 "2013-12-31T06:03:36Z")

</div>

I got the query wotking by using

{  
"query\_string": {  
"default\_field": "state",  
"query": "MA NY"  
}  
}

- Paul  
On Tuesday, 31 December 2013 11:07:06 UTC+5:30, paul wrote:

> My query is as below , which gives me all the colleges with state code  
> "MA" i want all the colleges that are in "MA" or "NY" how to add OR filter
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "nested": {  
> "path": "programs",  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "match": {  
> "programs.progName": "Computer and Information  
> Sciences"  
> }  
> },  
> {  
> "range": {  
> "programs.Bachelor": {  
> "gt": 0  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> },  
> "filter": {  
> "and": {  
> "filters": [  
> {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "state": "MA"  
> }  
> },  
> {  
> "range": {  
> "costOutofstateTution": {  
> "gte": 0,  
> "lte": 30000  
> }  
> }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }

--  
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/d23102f3-3180-4cdc-9d51-8ca960c7bcd0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d23102f3-3180-4cdc-9d51-8ca960c7bcd0%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [December 31, 2013, 5:21pm UTC](https://discuss.elastic.co/t/or-query/15060/3 "2013-12-31T17:21:09Z")

</div>

You are better of using a proper boolean filter for better performance.  
Queries cannot be cached and query string query analyzes the terms. Here is  
an example of your filter with a nested bool (should) filter:

"filter": {  
"and": {  
"filters": [  
{  
"bool": {  
"must": [  
{  
"bool": {  
"should": [  
{  
"term": {  
"state": "MA"  
}  
},  
{  
"term": {  
"state": "NY"  
}  
}  
]  
}  
},  
{  
"range": {  
"costOutofstateTution": {  
"gte": 0,  
"lte": 30000  
}  
}  
}  
]  
}  
}  
]  
}  
}

Cheers,

Ivan

On Mon, Dec 30, 2013 at 10:03 PM, paul [avinashpaul85@gmail.com](mailto:avinashpaul85@gmail.com) wrote:

> I got the query wotking by using
> 
> {  
> "query\_string": {  
> "default\_field": "state",  
> "query": "MA NY"  
> }  
> }
> 
> - Paul
> 
> On Tuesday, 31 December 2013 11:07:06 UTC+5:30, paul wrote:
> 
> > My query is as below , which gives me all the colleges with state code  
> > "MA" i want all the colleges that are in "MA" or "NY" how to add OR filter
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "nested": {  
> > "path": "programs",  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "match": {  
> > "programs.progName": "Computer and Information  
> > Sciences"  
> > }  
> > },  
> > {  
> > "range": {  
> > "programs.Bachelor": {  
> > "gt": 0  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > },  
> > "filter": {  
> > "and": {  
> > "filters": [  
> > {  
> > "bool": {  
> > "must": [  
> > {  
> > "term": {  
> > "state": "MA"  
> > }  
> > },  
> > {  
> > "range": {  
> > "costOutofstateTution": {  
> > "gte": 0,  
> > "lte": 30000  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> --  
> 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/d23102f3-3180-4cdc-9d51-8ca960c7bcd0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d23102f3-3180-4cdc-9d51-8ca960c7bcd0%40googlegroups.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CALY%3DcQC5FF-F%3DLJzpsVUvcq1n%2B%2B\_9DFcKgRFJ0r%3Dv3SS7jX\_tQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC5FF-F%3DLJzpsVUvcq1n%2B%2B_9DFcKgRFJ0r%3Dv3SS7jX_tQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![paul1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul1/32/46819_2.png) [@paul1](https://discuss.elastic.co/u/paul1)\
**Post date:** [January 2, 2014, 4:20am UTC](https://discuss.elastic.co/t/or-query/15060/4 "2014-01-02T04:20:22Z")

</div>

Thank you Ivan will definitely try it out.

-paul

On Tue, Dec 31, 2013 at 10:51 PM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> You are better of using a proper boolean filter for better performance.  
> Queries cannot be cached and query string query analyzes the terms. Here is  
> an example of your filter with a nested bool (should) filter:
> 
> "filter": {  
> "and": {  
> "filters": [  
> {  
> "bool": {  
> "must": [  
> {  
> "bool": {  
> "should": [  
> {  
> "term": {  
> "state": "MA"  
> }  
> },  
> {  
> "term": {  
> "state": "NY"  
> }  
> }  
> ]  
> }  
> },  
> {  
> "range": {  
> "costOutofstateTution": {  
> "gte": 0,  
> "lte": 30000  
> }  
> }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }
> 
> Cheers,
> 
> Ivan
> 
> On Mon, Dec 30, 2013 at 10:03 PM, paul [avinashpaul85@gmail.com](mailto:avinashpaul85@gmail.com) wrote:
> 
> > I got the query wotking by using
> > 
> > {  
> > "query\_string": {  
> > "default\_field": "state",  
> > "query": "MA NY"  
> > }  
> > }
> > 
> > - Paul
> > 
> > On Tuesday, 31 December 2013 11:07:06 UTC+5:30, paul wrote:
> > 
> > > My query is as below , which gives me all the colleges with state code  
> > > "MA" i want all the colleges that are in "MA" or "NY" how to add OR filter
> > > 
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "nested": {  
> > > "path": "programs",  
> > > "query": {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "match": {  
> > > "programs.progName": "Computer and Information  
> > > Sciences"  
> > > }  
> > > },  
> > > {  
> > > "range": {  
> > > "programs.Bachelor": {  
> > > "gt": 0  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "filter": {  
> > > "and": {  
> > > "filters": [  
> > > {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "term": {  
> > > "state": "MA"  
> > > }  
> > > },  
> > > {  
> > > "range": {  
> > > "costOutofstateTution": {  
> > > "gte": 0,  
> > > "lte": 30000  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > 
> > --  
> > 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/d23102f3-3180-4cdc-9d51-8ca960c7bcd0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d23102f3-3180-4cdc-9d51-8ca960c7bcd0%40googlegroups.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/rd6Lh\_U0lzI/unsubscribe](https://groups.google.com/d/topic/elasticsearch/rd6Lh_U0lzI/unsubscribe).  
> To unsubscribe from this group and all its topics, 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/CALY%3DcQC5FF-F%3DLJzpsVUvcq1n%2B%2B\_9DFcKgRFJ0r%3Dv3SS7jX\_tQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC5FF-F%3DLJzpsVUvcq1n%2B%2B_9DFcKgRFJ0r%3Dv3SS7jX_tQ%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CAO066G0Ng7dhg3U8L%3Dc49%2BDkM\_xWP5feXNYN%3Dfa6Nx55oqSn%3Dw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAO066G0Ng7dhg3U8L%3Dc49%2BDkM_xWP5feXNYN%3Dfa6Nx55oqSn%3Dw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:58am UTC](https://discuss.elastic.co/t/or-query/15060/5 "2017-07-06T01:58:42Z")

</div>


