# How to 'OR' two different nested queries?

**URL:** <https://discuss.elastic.co/t/how-to-or-two-different-nested-queries/13487>\
**Category:** Elasticsearch\
**Created:** [September 6, 2013, 9:20am UTC](https://discuss.elastic.co/t/how-to-or-two-different-nested-queries/13487 "2013-09-06T09:20:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjamin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin/32/2147_2.png) [@benjamin](https://discuss.elastic.co/u/benjamin)\
**Post date:** [September 6, 2013, 9:20am UTC](https://discuss.elastic.co/t/how-to-or-two-different-nested-queries/13487/1 "2013-09-06T09:20:26Z")

</div>

I have a User mapping like:

name: {  
type: "string"  
},  
followed\_groups: {  
type: "nested",  
properties: {  
created\_at: {  
type: "string"  
},  
group\_id: {  
type: "string"  
}  
}  
},  
id: {  
type: "integer"  
},  
member\_of\_groups: {  
type: "nested",  
properties: {  
created\_at: {  
type: "string"  
},  
group\_id: {  
type: "string"  
}  
}  
}

And I'd like to get users who are either following a group or a member of  
it.

Each individual query is ok using nested query like:

"query":{  
"nested":{  
"query":{  
"terms":{  
"followed\_groups.group\_id":[  
"22"  
]  
}  
},  
"path":"followed\_groups"  
}  
}

But I cannot find a way to combine the two with a OR condition. Is it  
possible please?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [September 6, 2013, 10:20am UTC](https://discuss.elastic.co/t/how-to-or-two-different-nested-queries/13487/2 "2013-09-06T10:20:57Z")

</div>

Hiya

You want to wrap both nested clauses in a bool-should clause. Also, given  
that you're not doing any scoring here, just using group membership as a  
filter, it'd be faster to use filters instead of queries:

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"should": [  
{  
"nested": {  
"path": "followed\_groups",  
"filter": {  
"terms": { "followed\_groups.group\_id": [22]}  
}  
}  
},  
{  
"nested": {  
"path": "member\_of\_groups",  
"filter": {  
"terms": { "member\_of\_groups.group\_id": [22]}  
}  
}  
}  
]  
}  
}  
}  
}  
}

While we're about it - you're not correlating eg group\_id with created\_at,  
so you may want to consider adding `include_in_parent: true` to your nested  
definitions. This will index the group\_id and created\_at fields in the  
parent document as well, in which case the query just becomes:

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"should": [  
{ "terms": { "followed\_groups.group\_id": [22]}},  
{ "terms": { "member\_of\_groups.group\_id": [22]}}  
]  
}  
}  
}  
}  
}

clint

--  
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).  
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, 2:17am UTC](https://discuss.elastic.co/t/how-to-or-two-different-nested-queries/13487/3 "2017-07-06T02:17:53Z")

</div>


