# Getting specific fields from objects embedded in arrays

**URL:** <https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028>\
**Category:** Elasticsearch\
**Created:** [November 30, 2011, 3:24pm UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028 "2011-11-30T15:24:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eran\_Kutner\_2](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@Eran\_Kutner\_2](https://discuss.elastic.co/u/Eran_Kutner_2)\
**Post date:** [November 30, 2011, 3:24pm UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/1 "2011-11-30T15:24:42Z")

</div>

Hi,  
I have an index with objects that look like this:  
{  
a : [  
{ x:1, y:2 },  
{ x:2, y:3 },  
]  
}

I'd like to get only the "x" members from the array objects. I expected  
that specifying "a.x" in the fields list would work and return something  
like:  
{  
a: [  
{ x:1 },  
{ x:2 }  
]  
}  
but it doesn't. It just doesn't return anything.

Is there another syntax I should use?

Thanks,  
Eran

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 30, 2011, 3:30pm UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/2 "2011-11-30T15:30:12Z")

</div>

Which version are you using? The following gist will return "a.x" : [1, 2]:  
[gist:1409479 · GitHub](https://gist.github.com/1409479).

p.s. Next time, please provide information similar to the gist I provided.

On Wed, Nov 30, 2011 at 5:24 PM, Eran Kutner [eran@gigya-inc.com](mailto:eran@gigya-inc.com) wrote:

> Hi,  
> I have an index with objects that look like this:  
> {  
> a : [  
> { x:1, y:2 },  
> { x:2, y:3 },  
> ]  
> }
> 
> I'd like to get only the "x" members from the array objects. I expected  
> that specifying "a.x" in the fields list would work and return something  
> like:  
> {  
> a: [  
> { x:1 },  
> { x:2 }  
> ]  
> }  
> but it doesn't. It just doesn't return anything.
> 
> Is there another syntax I should use?
> 
> Thanks,  
> Eran

---

<div class="post-metadata">

**Author:** ![Eran\_Kutner\_2](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@Eran\_Kutner\_2](https://discuss.elastic.co/u/Eran_Kutner_2)\
**Post date:** [November 30, 2011, 7:07pm UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/3 "2011-11-30T19:07:59Z")

</div>

Sorry about that, I thought I was asking a usage question, not reporting an  
issue.  
I'm using 0.17.6 and your query doesn't return any fields.

Running: curl 'localhost:9200/test/\_search?q=\*&fields=a'  
Doesn't return anything either.

Only : curl 'localhost:9200/test/\_search?q=_&fields=\_source.a'  
Works and returns the object "a" but: curl  
'localhost:9200/test/\_search?q=_&fields=\_source.a.x'  
Doesn't return anything again.

-eran

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [December 1, 2011, 10:56am UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/4 "2011-12-01T10:56:01Z")

</div>

This type of "navigation" is supported in 0.18 (where you should not use  
\_source prefix at all any more).

On Wed, Nov 30, 2011 at 9:07 PM, Eran Kutner [eran@gigya-inc.com](mailto:eran@gigya-inc.com) wrote:

> Sorry about that, I thought I was asking a usage question, not reporting  
> an issue.  
> I'm using 0.17.6 and your query doesn't return any fields.
> 
> Running: curl 'localhost:9200/test/\_search?q=\*&fields=a'  
> Doesn't return anything either.
> 
> Only : curl 'localhost:9200/test/\_search?q=_&fields=\_source.a'  
> Works and returns the object "a" but: curl  
> 'localhost:9200/test/\_search?q=_&fields=\_source.a.x'  
> Doesn't return anything again.
> 
> -eran

---

<div class="post-metadata">

**Author:** ![Eran\_Kutner\_2](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@Eran\_Kutner\_2](https://discuss.elastic.co/u/Eran_Kutner_2)\
**Post date:** [December 1, 2011, 7:14pm UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/5 "2011-12-01T19:14:52Z")

</div>

Thanks Shay, tested now with 0.18.5 and it does indeed work as you said,  
however it feels to me the response should be structured differently.  
Since ElasticSearch is document oriented I would expect the search result  
to keep the original document's structure, but limit the returned fields to  
the requested ones, so my expectation would be to get back:  
{  
a: [  
{ x:1 },  
{ x:2 }  
]  
}

not:  
{  
a.x: [1, 2]  
}

This becomes more apparent if you fetch multiple fields, the original  
document structure is completely lost. By the way, if you do the same thing  
with MongoDB you get back a structure like I would expect.

-eran

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [December 1, 2011, 7:55pm UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/6 "2011-12-01T19:55:46Z")

</div>

I understand, but this is how it works. I find it more usable the way it  
works currently (and less "noisy" in terms of data), especially if we are  
talking about "deep" navigation. But, its possible to provide other  
formats...

On Thu, Dec 1, 2011 at 9:14 PM, Eran Kutner [eran@gigya-inc.com](mailto:eran@gigya-inc.com) wrote:

> Thanks Shay, tested now with 0.18.5 and it does indeed work as you said,  
> however it feels to me the response should be structured differently.  
> Since Elasticsearch is document oriented I would expect the search result  
> to keep the original document's structure, but limit the returned fields to  
> the requested ones, so my expectation would be to get back:
> 
> {  
> a: [  
> { x:1 },  
> { x:2 }  
> ]  
> }
> 
> not:  
> {  
> a.x: [1, 2]  
> }
> 
> This becomes more apparent if you fetch multiple fields, the original  
> document structure is completely lost. By the way, if you do the same thing  
> with MongoDB you get back a structure like I would expect.
> 
> -eran

---

<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:46am UTC](https://discuss.elastic.co/t/getting-specific-fields-from-objects-embedded-in-arrays/6028/7 "2017-07-06T03:46:42Z")

</div>


