# Can ES return only the part of an array that match the query

**URL:** <https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069>\
**Category:** Elasticsearch\
**Created:** [December 13, 2012, 3:42pm UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069 "2012-12-13T15:42:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Margaret\_2](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Margaret\_2](https://discuss.elastic.co/u/Margaret_2)\
**Post date:** [December 13, 2012, 3:42pm UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069/1 "2012-12-13T15:42:46Z")

</div>

I have documents like this:  
{  
... ...,  
"id":"abc001",  
"nominations":[  
{  
"comment":"test",  
"events":[  
{  
"name":"aaa",  
"year":"2012"  
},{  
"name":"bbb",  
"year":"2011"  
}  
]  
},  
... ...  
]  
}

As you can see, there are two nested array, 'nominations' and 'events'.

When I search for docs that match {"term":{"events.name":"aaa"}} and  
{"term":{"events.year":"2012"}}, the document above will be returned.

But in practice, array 'events' may contains many elements like  
"{"name":"bbb","year":"2011"}" that don't match the query and also are  
useless to users. So my question is, can Elasticsearch return a document  
with array 'events' that contains element "{"name":"aaa","year":"2012"}"  
only? If not, is there any other possible ways to solve this problem? Or  
should I change the structure of the document?

Thanks a lot!

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 13, 2012, 4:06pm UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069/2 "2012-12-13T16:06:53Z")

</div>

You have to look at nested docs:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

HTH  
David

Le 13 décembre 2012 à 16:42, Margaret [xiaowen.huang.bjtu@gmail.com](mailto:xiaowen.huang.bjtu@gmail.com) a écrit :

> I have documents like this:  
> {  
> ... ...,  
> "id":"abc001",  
> "nominations":[  
> {  
> "comment":"test",  
> "events":[  
> {  
> "name":"aaa",  
> "year":"2012"  
> },{  
> "name":"bbb",  
> "year":"2011"  
> }  
> ]  
> },  
> ... ...  
> ]  
> }
> 
> As you can see, there are two nested array, 'nominations' and 'events'.
> 
> When I search for docs that match {"term":{"events.name":"aaa"}} and  
> {"term":{"events.year":"2012"}}, the document above will be returned.
> 
> But in practice, array 'events' may contains many elements like  
> "{"name":"bbb","year":"2011"}" that don't match the query and also are useless  
> to users. So my question is, can Elasticsearch return a document with array  
> 'events' that contains element "{"name":"aaa","year":"2012"}" only? If not, is  
> there any other possible ways to solve this problem? Or should I change the  
> structure of the document?
> 
> Thanks a lot!
> 
> --

--  
David Pilato  
[http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![Margaret\_2](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Margaret\_2](https://discuss.elastic.co/u/Margaret_2)\
**Post date:** [December 14, 2012, 3:17am UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069/3 "2012-12-14T03:17:41Z")

</div>

Thanks for reply, David.

Let me describe my problem more explicitly.  
I tried the example given in Nested Type(  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)) and  
Nested Query:  
$ curl -XGET '[http://localhost:9200/test/es/\_search](http://localhost:9200/test/es/_search)' -d '  
{  
"query":{  
"nested":{  
"path":"obj1",  
"query":{  
"bool":{  
"must":[  
{  
"text":{"obj1.name":"blue"}  
},  
{  
"range":{"obj1.count":{"lt":5}}  
}  
]  
}  
}  
}  
}  
}'.

The query result is:  
{"took":0,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max\_score":1.724915,"hits":[{"\_index":"test","\_type":"es","\_id":"GrmFSJo-RSetmjevjloA-Q","\_score":1.724915,  
"\_source" :  
{  
"obj1":[  
{  
"name":"blue",  
"count":4  
}  
,{  
"name":"green",  
"count":6  
}  
]  
}}]}}

But only element {"name":"blue","count":4} in "obj1" matches the query. Can  
ES return result like this:  
{  
"obj1":[  
{  
"name":"blue",  
"count":4  
}  
]  
}  
?

Thanks a lot.

2012/12/14 David Pilato [david@pilato.fr](mailto:david@pilato.fr)

> \*\*  
> You have to look at nested docs:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)
> 
> HTH  
> David
> 
> Le 13 décembre 2012 à 16:42, Margaret [xiaowen.huang.bjtu@gmail.com](mailto:xiaowen.huang.bjtu@gmail.com) a  
> écrit :
> 
> I have documents like this:  
> {  
> ... ...,  
> "id":"abc001",  
> "nominations":[  
> {  
> "comment":"test",  
> "events":[  
> {  
> "name":"aaa",  
> "year":"2012"  
> },{  
> "name":"bbb",  
> "year":"2011"  
> }  
> ]  
> },  
> ... ...  
> ]  
> }
> 
> As you can see, there are two nested array, 'nominations' and 'events'.
> 
> When I search for docs that match {"term":{"events.name":"aaa"}} and  
> {"term":{"events.year":"2012"}}, the document above will be returned.
> 
> But in practice, array 'events' may contains many elements like  
> "{"name":"bbb","year":"2011"}" that don't match the query and also are  
> useless to users. So my question is, can Elasticsearch return a document  
> with array 'events' that contains element "{"name":"aaa","year":"2012"}"  
> only? If not, is there any other possible ways to solve this problem? Or  
> should I change the structure of the document?
> 
> Thanks a lot!
> 
> --
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> --

--  
Thanks,  
\*  
\*  
\*Xiaowen Huang \*| _黄晓雯_  
Student of School of Software Engineering  
Beijing Jiaotong University  
Add.:No.3 Shangyuancun, Haidian District, Beijing, P.R.China  
Tel.: +86-152-1058-2730  
Email: [Xiaowen.Huang.bjtu@gmail.com](mailto:Xiaowen.Huang.bjtu@gmail.com)

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 14, 2012, 6:40am UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069/4 "2012-12-14T06:40:21Z")

</div>

Can you gist a curl recreation?  
It will help to see what is your mapping and how you create your doc.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 14 déc. 2012 à 04:17, Xiaowen Huang [xiaowen.huang.bjtu@gmail.com](mailto:xiaowen.huang.bjtu@gmail.com) a écrit :

> Thanks for reply, David.
> 
> Let me describe my problem more explicitly.  
> I tried the example given in Nested Type([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)) and Nested Query:  
> $ curl -XGET '[http://localhost:9200/test/es/\_search](http://localhost:9200/test/es/_search)' -d '  
> {  
> "query":{  
> "nested":{  
> "path":"obj1",  
> "query":{  
> "bool":{  
> "must":[  
> {  
> "text":{"obj1.name":"blue"}  
> },  
> {  
> "range":{"obj1.count":{"lt":5}}  
> }  
> ]  
> }  
> }  
> }  
> }  
> }'.
> 
> The query result is:  
> {"took":0,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max\_score":1.724915,"hits":[{"\_index":"test","\_type":"es","\_id":"GrmFSJo-RSetmjevjloA-Q","\_score":1.724915, "\_source" :  
> {  
> "obj1":[  
> {  
> "name":"blue",  
> "count":4  
> }  
> ,{  
> "name":"green",  
> "count":6  
> }  
> ]  
> }}]}}
> 
> But only element {"name":"blue","count":4} in "obj1" matches the query. Can ES return result like this:  
> {  
> "obj1":[  
> {  
> "name":"blue",  
> "count":4  
> }  
> ]  
> }  
> ?
> 
> Thanks a lot.
> 
> 2012/12/14 David Pilato [david@pilato.fr](mailto:david@pilato.fr)  
> You have to look at nested docs: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)
> 
> HTH  
> David
> 
> Le 13 décembre 2012 à 16:42, Margaret [xiaowen.huang.bjtu@gmail.com](mailto:xiaowen.huang.bjtu@gmail.com) a écrit :
> 
> > I have documents like this:  
> > {  
> > ... ...,  
> > "id":"abc001",  
> > "nominations":[  
> > {  
> > "comment":"test",  
> > "events":[  
> > {  
> > "name":"aaa",  
> > "year":"2012"  
> > },{  
> > "name":"bbb",  
> > "year":"2011"  
> > }  
> > ]  
> > },  
> > ... ...  
> > ]  
> > }
> > 
> > As you can see, there are two nested array, 'nominations' and 'events'.
> > 
> > When I search for docs that match {"term":{"events.name":"aaa"}} and {"term":{"events.year":"2012"}}, the document above will be returned.
> > 
> > But in practice, array 'events' may contains many elements like "{"name":"bbb","year":"2011"}" that don't match the query and also are useless to users. So my question is, can Elasticsearch return a document with array 'events' that contains element "{"name":"aaa","year":"2012"}" only? If not, is there any other possible ways to solve this problem? Or should I change the structure of the document?
> > 
> > Thanks a lot!
> > 
> > --
> 
> ## -- David Pilato [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/) [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/) Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> --  
> Thanks,
> 
> Xiaowen Huang | 黄晓雯  
> Student of School of Software Engineering  
> Beijing Jiaotong University  
> Add.:No.3 Shangyuancun, Haidian District, Beijing, P.R.China  
> Tel.: +86-152-1058-2730  
> Email: [Xiaowen.Huang.bjtu@gmail.com](mailto:Xiaowen.Huang.bjtu@gmail.com)
> 
> --

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [December 19, 2012, 11:18pm UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069/5 "2012-12-19T23:18:38Z")

</div>

You want less than all the member of the elements in the array. Oh how  
much, I'd like that too.

My understanding is that nested objects can prevent "cross object"  
matches (searching for +name:"blue" +count:6 and incorrectly getting a  
match), but when you match the parent document with a nested query you  
can only get back either  
(1) the whole parent (outer) document, or  
(2) certain stored fields of the parent  
that is all.

I believe at this time, the only thing you can do is ask for documents  
that have at least 1 nested or 1 child document that matches.  
If you switch to children docs you could:  
(1) search for parent documents  
(2) search for children documents that match your query of children, but  
have the right parents.

Then combine the results.

If the criteria for matching children is not for scoring the relevancy  
of the parent, you can put the matching of children in a filter and make  
sure that result is cached, so even though you are going back again and  
repeating the same possibly complex filter, the filtered children is  
already done and it need only be further filtered by the requested  
parent IDs.

Because of filter caching two requests can be very fast.

-Paul

On 12/13/2012 7:17 PM, Xiaowen Huang wrote:

> The query result is:  
> {"took":0,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max\_score":1.724915,"hits":[{"\_index":"test","\_type":"es","\_id":"GrmFSJo-RSetmjevjloA-Q","\_score":1.724915,  
> "\_source" :  
> {  
> "obj1":[  
> {  
> "name":"blue",  
> "count":4  
> }  
> ,{  
> "name":"green",  
> "count":6  
> }  
> ]  
> }}]}}
> 
> But only element {"name":"blue","count":4} in "obj1" matches the  
> query. Can ES return result like this:  
> {  
> "obj1":[  
> {  
> "name":"blue",  
> "count":4  
> }  
> ]  
> }  
> ?
> 
> Thanks a lot.

--

---

<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:59am UTC](https://discuss.elastic.co/t/can-es-return-only-the-part-of-an-array-that-match-the-query/10069/6 "2017-07-06T02:59:14Z")

</div>


