# Filter documents by nested objects

**URL:** <https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503>\
**Category:** Elasticsearch\
**Created:** [October 30, 2012, 5:35pm UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503 "2012-10-30T17:35:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harry\_Walter](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@Harry\_Walter](https://discuss.elastic.co/u/Harry_Walter)\
**Post date:** [October 30, 2012, 5:35pm UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503/1 "2012-10-30T17:35:32Z")

</div>

Based on this data i've been testing  
with: [https://gist.github.com/a0d78e7c32ec76f03681](https://gist.github.com/a0d78e7c32ec76f03681) I've been trying to work  
out how to get a set of documents back based on the nested objects.

The filter needs to be an AND however this seems to match nothing when I  
try.

Does anyone know how to filter (or query) documents based on it's nested  
objects and using an AND type operation to recover documents that have  
matching nested objects?

--

---

<div class="post-metadata">

**Author:** ![11111](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/11111/32/2406_2.png) [@11111](https://discuss.elastic.co/u/11111)\
**Post date:** [October 30, 2012, 7:25pm UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503/2 "2012-10-30T19:25:28Z")

</div>

Try to omit 'match\_all' and 'and' (you don't need 'and' for 1 query) in  
nested filter:

'{  
"fields": ["title", "slug", "tags"],  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"nested": {  
"\_scope": "tag\_search",  
"path": "tags",  
"query": {  
"term": { "tags.slug": ["cars", "news"] }  
}  
}  
}  
}  
}  
}  
}'

вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter  
написал:

> Based on this data i've been testing with:  
> [Search articles by tag slug · GitHub](https://gist.github.com/a0d78e7c32ec76f03681) I've been trying to work out  
> how to get a set of documents back based on the nested objects.
> 
> The filter needs to be an AND however this seems to match nothing when I  
> try.
> 
> Does anyone know how to filter (or query) documents based on it's nested  
> objects and using an AND type operation to recover documents that have  
> matching nested objects?

--

---

<div class="post-metadata">

**Author:** ![11111](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/11111/32/2406_2.png) [@11111](https://discuss.elastic.co/u/11111)\
**Post date:** [October 30, 2012, 7:27pm UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503/3 "2012-10-30T19:27:02Z")

</div>

Also there's a typo: "term": { "tags.slug": ["cars", "news"] }  
Should be "term_s_": { "tags.slug": ["cars", "news"] }

вторник, 30 октября 2012 г., 23:25:28 UTC+4 пользователь Кирилл Шнуров  
написал:

> Try to omit 'match\_all' and 'and' (you don't need 'and' for 1 query) in  
> nested filter:
> 
> '{  
> "fields": ["title", "slug", "tags"],  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "nested": {  
> "\_scope": "tag\_search",  
> "path": "tags",  
> "query": {  
> "term": { "tags.slug": ["cars", "news"] }  
> }  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter  
> написал:
> 
> > Based on this data i've been testing with:  
> > [Search articles by tag slug · GitHub](https://gist.github.com/a0d78e7c32ec76f03681) I've been trying to work  
> > out how to get a set of documents back based on the nested objects.
> > 
> > The filter needs to be an AND however this seems to match nothing when I  
> > try.
> > 
> > Does anyone know how to filter (or query) documents based on it's nested  
> > objects and using an AND type operation to recover documents that have  
> > matching nested objects?

--

---

<div class="post-metadata">

**Author:** ![Harry\_Walter](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@Harry\_Walter](https://discuss.elastic.co/u/Harry_Walter)\
**Post date:** [October 31, 2012, 11:06am UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503/4 "2012-10-31T11:06:27Z")

</div>

Thanks for the reply.

Testing with your query returns me all results as it is doing an OR on the  
tags.slug field. Say I have two documents, both have a nested tag with slug  
"cars", each also has a secondary nested tag, first document with "news"  
seconds with "reviews". With your query I get back all documents, however  
I'd only want to one that has BOTH "cars" and "news" for the tags.slug  
field.

On Tuesday, October 30, 2012 7:27:02 PM UTC, Кирилл Шнуров wrote:

> Also there's a typo: "term": { "tags.slug": ["cars", "news"] }  
> Should be "term_s_": { "tags.slug": ["cars", "news"] }
> 
> вторник, 30 октября 2012 г., 23:25:28 UTC+4 пользователь Кирилл Шнуров  
> написал:
> 
> > Try to omit 'match\_all' and 'and' (you don't need 'and' for 1 query) in  
> > nested filter:
> > 
> > '{  
> > "fields": ["title", "slug", "tags"],  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "filter": {  
> > "nested": {  
> > "\_scope": "tag\_search",  
> > "path": "tags",  
> > "query": {  
> > "term": { "tags.slug": ["cars", "news"] }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }'
> > 
> > вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter  
> > написал:
> > 
> > > Based on this data i've been testing with:  
> > > [Search articles by tag slug · GitHub](https://gist.github.com/a0d78e7c32ec76f03681) I've been trying to work  
> > > out how to get a set of documents back based on the nested objects.
> > > 
> > > The filter needs to be an AND however this seems to match nothing when I  
> > > try.
> > > 
> > > Does anyone know how to filter (or query) documents based on it's nested  
> > > objects and using an AND type operation to recover documents that have  
> > > matching nested objects?

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 31, 2012, 12:16pm UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503/5 "2012-10-31T12:16:03Z")

</div>

If I understood you correctly, you are trying to find one term in one  
nested document and another term in another nested document and both terms  
have to be present for the parent to match. If this is the case, you need  
to move "AND" operation to the parent document level because nested query  
can only match a single nested document at a  
time: [Search articles by tag slug · GitHub](https://gist.github.com/3e9a2daf3e6f2b12412b#file_search_documents.sh)

BTW, if this is a frequently used query, it might make sense to pull the  
tags to the parent by setting include\_in\_parent=true on the nested object  
mapping.

On Wednesday, October 31, 2012 7:06:28 AM UTC-4, Harry Walter wrote:

> Thanks for the reply.
> 
> Testing with your query returns me all results as it is doing an OR on the  
> tags.slug field. Say I have two documents, both have a nested tag with slug  
> "cars", each also has a secondary nested tag, first document with "news"  
> seconds with "reviews". With your query I get back all documents, however  
> I'd only want to one that has BOTH "cars" and "news" for the tags.slug  
> field.
> 
> On Tuesday, October 30, 2012 7:27:02 PM UTC, Кирилл Шнуров wrote:
> 
> > Also there's a typo: "term": { "tags.slug": ["cars", "news"] }  
> > Should be "term_s_": { "tags.slug": ["cars", "news"] }
> > 
> > вторник, 30 октября 2012 г., 23:25:28 UTC+4 пользователь Кирилл Шнуров  
> > написал:
> > 
> > > Try to omit 'match\_all' and 'and' (you don't need 'and' for 1 query) in  
> > > nested filter:
> > > 
> > > '{  
> > > "fields": ["title", "slug", "tags"],  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "filter": {  
> > > "nested": {  
> > > "\_scope": "tag\_search",  
> > > "path": "tags",  
> > > "query": {  
> > > "term": { "tags.slug": ["cars", "news"] }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'
> > > 
> > > вторник, 30 октября 2012 г., 21:35:32 UTC+4 пользователь Harry Walter  
> > > написал:
> > > 
> > > > Based on this data i've been testing with:  
> > > > [Search articles by tag slug · GitHub](https://gist.github.com/a0d78e7c32ec76f03681) I've been trying to work  
> > > > out how to get a set of documents back based on the nested objects.
> > > > 
> > > > The filter needs to be an AND however this seems to match nothing when  
> > > > I try.
> > > > 
> > > > Does anyone know how to filter (or query) documents based on it's  
> > > > nested objects and using an AND type operation to recover documents that  
> > > > have matching nested objects?

--

---

<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, 3:06am UTC](https://discuss.elastic.co/t/filter-documents-by-nested-objects/9503/6 "2017-07-06T03:06:42Z")

</div>


