# How to iterate through array of objects

**URL:** <https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898>\
**Category:** Elasticsearch\
**Created:** [May 9, 2013, 12:09pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898 "2013-05-09T12:09:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 9, 2013, 12:09pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/1 "2013-05-09T12:09:47Z")

</div>

HI All,

I have index with mapping:  
"properties":{ "Availability":{"dynamic":"true","properties":{"Period":{  
"type":"string"},"Price":{"type":"double"}}} }

_I want to iterate through array of objects  
how can I do it in ES script using MVEL language?_

_What's important I particularly_\* want to access Period and Price for the  
same (\*_concurrent) index/id/object (don't known what is proper term in  
ES). \*  
For every Period there is assigned Price, so for example I want to access \*  
Availability[j].Period_ and _Availability[j].Price_ (for the same  
j=0,1,2,3,... and so on).

Thanks in advance and Best for All,  
Gzom

--  
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:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 10, 2013, 8:06am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/2 "2013-05-10T08:06:13Z")

</div>

Hey,

The MVEL functions you are searching for is doc['yourField'].values and a  
for loop in MVEL. Use it like this:

for (i : doc['a'].values) { do\_what\_you\_want\_here }

see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)

--Alex

On Thu, May 9, 2013 at 2:09 PM, [waldemar@pureholidayhomes.com](mailto:waldemar@pureholidayhomes.com) wrote:

> HI All,
> 
> I have index with mapping:  
> "properties":{ "Availability":{"dynamic":"true","properties":{"Period":{  
> "type":"string"},"Price":{"type":"double"}}} }
> 
> _I want to iterate through array of objects  
> how can I do it in ES script using MVEL language?_
> 
> _What's important I particularly_\* want to access Period and Price for  
> the same (\*\*concurrent) index/id/object (don't known what is proper term  
> in ES). \*  
> For every Period there is assigned Price, so for example I want to access  
> _Availability[j].Period_ and _Availability[j].Price_ (for the same  
> j=0,1,2,3,... and so on).
> 
> Thanks in advance and Best for All,  
> Gzom
> 
> --  
> 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).

--  
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:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 10, 2013, 9:38am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/3 "2013-05-10T09:38:30Z")

</div>

Thank you Alexander for your suggestion, but it is not working ☹

I have used script:  
"script":"for(element : doc["Availability"].values){return element.Period  
+"="+ element.Price}"

And I get error:  
_ElasticSearchIllegalArgumentException[No field found for  
[AvailabilityPeriodV2] in mapping_

Any other suggestions?

Thanks,  
Gzom

On Friday, 10 May 2013 10:06:13 UTC+2, Alexander Reelsen wrote:

> Hey,
> 
> The MVEL functions you are searching for is doc['yourField'].values and a  
> for loop in MVEL. Use it like this:
> 
> for (i : doc['a'].values) { do\_what\_you\_want\_here }
> 
> see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
> and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)
> 
> --Alex
> 
> On Thu, May 9, 2013 at 2:09 PM, \<[wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com)\<javascript:\>
> 
> > wrote:
> 
> > HI All,
> > 
> > I have index with mapping:  
> > "properties":{ "Availability":{"dynamic":"true","properties":{"Period":{  
> > "type":"string"},"Price":{"type":"double"}}} }
> > 
> > _I want to iterate through array of objects  
> > how can I do it in ES script using MVEL language?_
> > 
> > _What's important I particularly_\* want to access Period and Price for  
> > the same (\*\*concurrent) index/id/object (don't known what is proper term  
> > in ES). \*  
> > For every Period there is assigned Price, so for example I want to access  
> > _Availability[j].Period_ and _Availability[j].Price_ (for the same  
> > j=0,1,2,3,... and so on).
> > 
> > Thanks in advance and Best for All,  
> > Gzom
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [May 10, 2013, 9:45am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/4 "2013-05-10T09:45:21Z")

</div>

doc["Availability"]

Should be

doc["Availability"]

My 2 cents

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

Le 10 mai 2013 à 11:38, [waldemar@pureholidayhomes.com](mailto:waldemar@pureholidayhomes.com) a écrit :

Thank you Alexander for your suggestion, but it is not working ☹

I have used script:  
"script":"for(element : doc["Availability"].values){return element.Period +"="+ element.Price}"

And I get error:  
ElasticSearchIllegalArgumentException[No field found for [AvailabilityPeriodV2] in mapping

Any other suggestions?

Thanks,  
Gzom

On Friday, 10 May 2013 10:06:13 UTC+2, Alexander Reelsen wrote:

> Hey,
> 
> The MVEL functions you are searching for is doc['yourField'].values and a for loop in MVEL. Use it like this:
> 
> for (i : doc['a'].values) { do\_what\_you\_want\_here }
> 
> see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
> and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)
> 
> --Alex
> 
> On Thu, May 9, 2013 at 2:09 PM, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) wrote:
> 
> > HI All,
> > 
> > I have index with mapping:  
> > "properties":{ "Availability":{"dynamic":"true","properties":{"Period":{"type":"string"},"Price":{"type":"double"}}} }
> > 
> > I want to iterate through array of objects  
> > how can I do it in ES script using MVEL language?
> > 
> > What's important I particularly want to access Period and Price for the same (concurrent) index/id/object (don't known what is proper term in ES).  
> > For every Period there is assigned Price, so for example I want to access Availability[j].Period and Availability[j].Price (for the same j=0,1,2,3,... and so on).
> > 
> > Thanks in advance and Best for All,  
> > Gzom
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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).  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 10, 2013, 10:19am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/5 "2013-05-10T10:19:45Z")

</div>

Yes, that is misspeling for the copy paste.

I have used script:  
"script":"for(element : doc["Availability"].values){return element.Period  
+"="+ element.Price}"

And I get error:  
_ElasticSearchIllegalArgumentException[No field found for [Availability] in  
mapping_

Any other suggestions?

Thanks,  
Gzom

On Friday, 10 May 2013 11:45:21 UTC+2, David Pilato wrote:

> doc["Availability"]
> 
> Should be
> 
> doc["Availability"]
> 
> My 2 cents
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 10 mai 2013 à 11:38, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) \<javascript:\> a  
> écrit :
> 
> Thank you Alexander for your suggestion, but it is not working ☹
> 
> I have used script:  
> "script":"for(element : doc["Availability"].values){return element.Period  
> +"="+ element.Price}"
> 
> And I get error:  
> _ElasticSearchIllegalArgumentException[No field found for  
> [AvailabilityPeriodV2] in mapping_
> 
> Any other suggestions?
> 
> Thanks,  
> Gzom
> 
> On Friday, 10 May 2013 10:06:13 UTC+2, Alexander Reelsen wrote:
> 
> > Hey,
> > 
> > The MVEL functions you are searching for is doc['yourField'].values and a  
> > for loop in MVEL. Use it like this:
> > 
> > for (i : doc['a'].values) { do\_what\_you\_want\_here }
> > 
> > see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
> > and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)
> > 
> > --Alex
> > 
> > On Thu, May 9, 2013 at 2:09 PM, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) wrote:
> > 
> > > HI All,
> > > 
> > > I have index with mapping:  
> > > "properties":{ "Availability":{"dynamic":"true","properties":{"Period":{  
> > > "type":"string"},"Price":{"type":"double"}}} }
> > > 
> > > _I want to iterate through array of objects  
> > > how can I do it in ES script using MVEL language?_
> > > 
> > > _What's important I particularly_\* want to access Period and Price for  
> > > the same (\*\*concurrent) index/id/object (don't known what is proper  
> > > term in ES). \*  
> > > For every Period there is assigned Price, so for example I want to  
> > > access _Availability[j].Period_ and _Availability[j].Price_ (for the  
> > > same j=0,1,2,3,... and so on).
> > > 
> > > Thanks in advance and Best for All,  
> > > Gzom
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [May 10, 2013, 10:27am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/6 "2013-05-10T10:27:31Z")

</div>

Because Avaibility is not an array but an object with two nested properties.  
Can you post a sample doc here?

BTW, I think you should use nested docs for your use case.  
Othewise, your properties will be flatten and you won't be able to find a relation between Avaibility and price.

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

Le 10 mai 2013 à 12:19, [waldemar@pureholidayhomes.com](mailto:waldemar@pureholidayhomes.com) a écrit :

Yes, that is misspeling for the copy paste.

I have used script:  
"script":"for(element : doc["Availability"].values){return element.Period +"="+ element.Price}"

And I get error:  
ElasticSearchIllegalArgumentException[No field found for [Availability] in mapping

Any other suggestions?

Thanks,  
Gzom

On Friday, 10 May 2013 11:45:21 UTC+2, David Pilato wrote:

> doc["Availability"]
> 
> Should be
> 
> doc["Availability"]
> 
> My 2 cents
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 10 mai 2013 à 11:38, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) a écrit :
> 
> Thank you Alexander for your suggestion, but it is not working ☹
> 
> I have used script:  
> "script":"for(element : doc["Availability"].values){return element.Period +"="+ element.Price}"
> 
> And I get error:  
> ElasticSearchIllegalArgumentException[No field found for [AvailabilityPeriodV2] in mapping
> 
> Any other suggestions?
> 
> Thanks,  
> Gzom
> 
> On Friday, 10 May 2013 10:06:13 UTC+2, Alexander Reelsen wrote:
> 
> > Hey,
> > 
> > The MVEL functions you are searching for is doc['yourField'].values and a for loop in MVEL. Use it like this:
> > 
> > for (i : doc['a'].values) { do\_what\_you\_want\_here }
> > 
> > see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
> > and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)
> > 
> > --Alex
> > 
> > On Thu, May 9, 2013 at 2:09 PM, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) wrote:
> > 
> > > HI All,
> > > 
> > > I have index with mapping:  
> > > "properties":{ "Availability":{"dynamic":"true","properties":{"Period":{"type":"string"},"Price":{"type":"double"}}} }
> > > 
> > > I want to iterate through array of objects  
> > > how can I do it in ES script using MVEL language?
> > > 
> > > What's important I particularly want to access Period and Price for the same (concurrent) index/id/object (don't known what is proper term in ES).  
> > > For every Period there is assigned Price, so for example I want to access Availability[j].Period and Availability[j].Price (for the same j=0,1,2,3,... and so on).
> > > 
> > > Thanks in advance and Best for All,  
> > > Gzom
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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).  
> > > 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 10, 2013, 10:39am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/7 "2013-05-10T10:39:22Z")

</div>

Hi David,

My sample doc:

\*"Availability":[

```
{
    "Period":"2013-06-05|2013-05-09",
    "Price": 53.37
},
{
    "Period":"2013-06-07|2013-05-10",
    "Price": 106.75
}

```

]  
\*  
\*And I can't use nested docs for the performance reason. \*

Thnaks in advance,  
Gzom

On Friday, 10 May 2013 12:27:31 UTC+2, David Pilato wrote:

> Because Avaibility is not an array but an object with two nested  
> properties.  
> Can you post a sample doc here?
> 
> BTW, I think you should use nested docs for your use case.  
> Othewise, your properties will be flatten and you won't be able to find a  
> relation between Avaibility and price.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 10 mai 2013 à 12:19, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) \<javascript:\> a  
> écrit :
> 
> Yes, that is misspeling for the copy paste.
> 
> I have used script:  
> "script":"for(element : doc["Availability"].values){return element.  
> Period +"="+ element.Price}"
> 
> And I get error:  
> _ElasticSearchIllegalArgumentException[No field found for [Availability]  
> in mapping_
> 
> Any other suggestions?
> 
> Thanks,  
> Gzom
> 
> On Friday, 10 May 2013 11:45:21 UTC+2, David Pilato wrote:
> 
> > doc["Availability"]
> > 
> > Should be
> > 
> > doc["Availability"]
> > 
> > My 2 cents
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 10 mai 2013 à 11:38, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) a écrit :
> > 
> > Thank you Alexander for your suggestion, but it is not working ☹
> > 
> > I have used script:  
> > "script":"for(element : doc["Availability"].values){return element.  
> > Period +"="+ element.Price}"
> > 
> > And I get error:  
> > _ElasticSearchIllegalArgumentException[No field found for  
> > [AvailabilityPeriodV2] in mapping_
> > 
> > Any other suggestions?
> > 
> > Thanks,  
> > Gzom
> > 
> > On Friday, 10 May 2013 10:06:13 UTC+2, Alexander Reelsen wrote:
> > 
> > > Hey,
> > > 
> > > The MVEL functions you are searching for is doc['yourField'].values and  
> > > a for loop in MVEL. Use it like this:
> > > 
> > > for (i : doc['a'].values) { do\_what\_you\_want\_here }
> > > 
> > > see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
> > > and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)
> > > 
> > > --Alex
> > > 
> > > On Thu, May 9, 2013 at 2:09 PM, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) wrote:
> > > 
> > > > HI All,
> > > > 
> > > > I have index with mapping:  
> > > > "properties":{ "Availability":{"dynamic":"true","properties":{"Period"  
> > > > :{"type":"string"},"Price":{"type":"double"}}} }
> > > > 
> > > > _I want to iterate through array of objects  
> > > > how can I do it in ES script using MVEL language?_
> > > > 
> > > > _What's important I particularly_\* want to access Period and Price for  
> > > > the same (\*\*concurrent) index/id/object (don't known what is proper  
> > > > term in ES). \*  
> > > > For every Period there is assigned Price, so for example I want to  
> > > > access _Availability[j].Period_ and _Availability[j].Price_ (for the  
> > > > same j=0,1,2,3,... and so on).
> > > > 
> > > > Thanks in advance and Best for All,  
> > > > Gzom
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 10, 2013, 10:50am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/8 "2013-05-10T10:50:37Z")

</div>

Is it possible not to use nested docs and still have array with relation  
between Period an Price ?

Thanks,  
Gzom

On Friday, 10 May 2013 12:27:31 UTC+2, David Pilato wrote:

> Because Avaibility is not an array but an object with two nested  
> properties.  
> Can you post a sample doc here?
> 
> BTW, I think you should use nested docs for your use case.  
> Othewise, your properties will be flatten and you won't be able to find a  
> relation between Avaibility and price.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 10 mai 2013 à 12:19, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) \<javascript:\> a  
> écrit :
> 
> Yes, that is misspeling for the copy paste.
> 
> I have used script:  
> "script":"for(element : doc["Availability"].values){return element.  
> Period +"="+ element.Price}"
> 
> And I get error:  
> _ElasticSearchIllegalArgumentException[No field found for [Availability]  
> in mapping_
> 
> Any other suggestions?
> 
> Thanks,  
> Gzom
> 
> On Friday, 10 May 2013 11:45:21 UTC+2, David Pilato wrote:
> 
> > doc["Availability"]
> > 
> > Should be
> > 
> > doc["Availability"]
> > 
> > My 2 cents
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 10 mai 2013 à 11:38, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) a écrit :
> > 
> > Thank you Alexander for your suggestion, but it is not working ☹
> > 
> > I have used script:  
> > "script":"for(element : doc["Availability"].values){return element.  
> > Period +"="+ element.Price}"
> > 
> > And I get error:  
> > _ElasticSearchIllegalArgumentException[No field found for  
> > [AvailabilityPeriodV2] in mapping_
> > 
> > Any other suggestions?
> > 
> > Thanks,  
> > Gzom
> > 
> > On Friday, 10 May 2013 10:06:13 UTC+2, Alexander Reelsen wrote:
> > 
> > > Hey,
> > > 
> > > The MVEL functions you are searching for is doc['yourField'].values and  
> > > a for loop in MVEL. Use it like this:
> > > 
> > > for (i : doc['a'].values) { do\_what\_you\_want\_here }
> > > 
> > > see [http://mvel.codehaus.org/MVEL+2.0+Control+Flow](http://mvel.codehaus.org/MVEL+2.0+Control+Flow)  
> > > and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting/)
> > > 
> > > --Alex
> > > 
> > > On Thu, May 9, 2013 at 2:09 PM, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com) wrote:
> > > 
> > > > HI All,
> > > > 
> > > > I have index with mapping:  
> > > > "properties":{ "Availability":{"dynamic":"true","properties":{"Period"  
> > > > :{"type":"string"},"Price":{"type":"double"}}} }
> > > > 
> > > > _I want to iterate through array of objects  
> > > > how can I do it in ES script using MVEL language?_
> > > > 
> > > > _What's important I particularly_\* want to access Period and Price for  
> > > > the same (\*\*concurrent) index/id/object (don't known what is proper  
> > > > term in ES). \*  
> > > > For every Period there is assigned Price, so for example I want to  
> > > > access _Availability[j].Period_ and _Availability[j].Price_ (for the  
> > > > same j=0,1,2,3,... and so on).
> > > > 
> > > > Thanks in advance and Best for All,  
> > > > Gzom
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [May 10, 2013, 4:59pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/9 "2013-05-10T16:59:36Z")

</div>

Hi Gzom,

Just a thought.... it's worked well for me in a past life when creating  
arrays of [lat, lon] pairs but the database only supported multivalued  
fields of basic types but not complex types.

Perhaps:

"Availability\_Period" : ["2013-06-05|2013-05-09", "2013-06-05|2013-05-09"]

"Availability\_Price" : [53.37, 106.75]

The two arrays are related by position. For example, Availability\_Period[_n_  
] corresponds to Availability\_Price[_n_].

Might this help? Just a thought...

Regards,  
Brian

On Friday, May 10, 2013 6:39:22 AM UTC-4, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com)  
wrote:

> Hi David,
> 
> My sample doc:
> 
> \*"Availability":[
> 
> ```
> {
> "Period":"2013-06-05|2013-05-09",
> "Price": 53.37
> },
> {
> "Period":"2013-06-07|2013-05-10",
> "Price": 106.75
> }
> 
> ```
> 
> ]  
> \*  
> \*And I can't use nested docs for the performance reason. \*
> 
> Thnaks in advance,  
> Gzom

--  
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:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 13, 2013, 7:30am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/10 "2013-05-13T07:30:11Z")

</div>

Thanks Brian for your idea, I will check that in next few day (now too many  
responsibilities), and come back with results .  
Best,  
Gzom

On Friday, 10 May 2013 18:59:36 UTC+2, InquiringMind wrote:

> Hi Gzom,
> 
> Just a thought.... it's worked well for me in a past life when creating  
> arrays of [lat, lon] pairs but the database only supported multivalued  
> fields of basic types but not complex types.
> 
> Perhaps:
> 
> "Availability\_Period" : [ "2013-06-05|2013-05-09", "2013-06-05|2013-05-09"  
> ]
> 
> "Availability\_Price" : [53.37, 106.75]
> 
> The two arrays are related by position. For example, Availability\_Period[\*  
> n\*] corresponds to Availability\_Price[_n_].
> 
> Might this help? Just a thought...
> 
> Regards,  
> Brian
> 
> On Friday, May 10, 2013 6:39:22 AM UTC-4, wald...@pureholidayhomes.comwrote:
> 
> > Hi David,
> > 
> > My sample doc:
> > 
> > \*"Availability":[
> > 
> > ```
> > {
> > "Period":"2013-06-05|2013-05-09",
> > "Price": 53.37
> > },
> > {
> > "Period":"2013-06-07|2013-05-10",
> > "Price": 106.75
> > }
> > 
> > ```
> > 
> > ]  
> > \*  
> > \*And I can't use nested docs for the performance reason. \*
> > 
> > Thnaks in advance,  
> > Gzom

--  
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:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 13, 2013, 7:37am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/11 "2013-05-13T07:37:25Z")

</div>

Hi Brian,

Could you provide some code example?  
Because I have tested everything what's on internet, and without success.  
Maybe I'm doing something wrong?

Best,  
Gzom

PS. Thanks for your idea, I will check that in next few day (now too  
many responsibilities), and come back with results .

W dniu 2013-05-10 18:59, InquiringMind pisze:

> Hi Gzom,
> 
> Just a thought.... it's worked well for me in a past life when  
> creating arrays of [lat, lon] pairs but the database only supported  
> multivalued fields of basic types but not complex types.
> 
> Perhaps:
> 
> "Availability\_Period" : [ "2013-06-05|2013-05-09",  
> "2013-06-05|2013-05-09" ]
> 
> "Availability\_Price" : [53.37, 106.75]
> 
> The two arrays are related by position. For example,  
> Availability\_Period[/n/] corresponds to Availability\_Price[/n/].
> 
> Might this help? Just a thought...
> 
> Regards,  
> Brian
> 
> On Friday, May 10, 2013 6:39:22 AM UTC-4, [wald...@pureholidayhomes.com](mailto:wald...@pureholidayhomes.com)  
> wrote:
> 
> ```
> Hi David,
> 
> My sample doc:
> 
> /"Availability":[
> 
> {
> "Period":"2013-06-05|2013-05-09",
> "Price": 53.37
> },
> {
> "Period":"2013-06-07|2013-05-10",
> "Price": 106.75
> }
> ]
> /
> _And I can't use nested docs for the performance reason. _
> 
> Thnaks in advance,
> Gzom
> 
> ```
> 
> --  
> 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/8Z2KwuPlyas/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/8Z2KwuPlyas/unsubscribe?hl=en-US).  
> To unsubscribe from this group and all its topics, 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).

--  
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:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [May 13, 2013, 4:34pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/12 "2013-05-13T16:34:14Z")

</div>

Hi Gzom,

My suggestion is pretty much language-independent. My previous experience  
was based on a C++ database engine I wrote (Elasticsearch blows it away in  
the area of updates and smooth cluster replication; I blew away Lucene in  
the areas of performance, phrases with wildcards, tock-solid bug-free geo  
shapes, and hierarchy support. I would love to marry Elasticsearch with my  
non-Lucene indexing magic, but that's a discussion for another day!) and  
that C++ code is no longer available to me (but the ideas are!).

But the code (in whatever language you wish) should be pretty simple. The  
important concept is that your documents contain two multi-valued fields,  
and that the relationship between the values in each of those fields is  
handled by your application (load, and query) and not internal to  
Elasticsearch. For some index N, the value of field1[N] corresponds to the  
value of field2[N].

In my previous experience, I had one lat and lon pair for each document,  
but across the hierarchy there could be many, many documents that matched  
the query. So I collected the lat and lon values from the related documents  
for a hierarchy. I was left with two arrays of the same size (lat and  
lon). I would apply each pair of lat[N], lon[N] to the geo index and geo  
filter (either circle or generic polygon) and if any of the pairs was  
within the range the documents from that hierarchy were kept (in general,  
of course). And as I was determining the inclusion, I also updated the  
document (in the response, not on disk) with the calculated distance; this  
enabled a sort by (smallest) distance and also enabled the client to  
highlight the smallest distance so it was easy to see which distance caused  
the sorting placement of the hierarchy's returned documents.

Regards,  
Brian

On Monday, May 13, 2013 3:37:25 AM UTC-4, Waldemar Ch. wrote:

> Hi Brian,
> 
> Could you provide some code example?  
> Because I have tested everything what's on internet, and without success.  
> Maybe I'm doing something wrong?
> 
> Best,  
> Gzom
> 
> PS. Thanks for your idea, I will check that in next few day (now too many  
> responsibilities), and come back with results .
> 
> W dniu 2013-05-10 18:59, InquiringMind pisze:
> 
> Hi Gzom,
> 
> Just a thought.... it's worked well for me in a past life when creating  
> arrays of [lat, lon] pairs but the database only supported multivalued  
> fields of basic types but not complex types.
> 
> Perhaps:
> 
> "Availability\_Period" : [ "2013-06-05|2013-05-09",  
> "2013-06-05|2013-05-09" ]
> 
> "Availability\_Price" : [53.37, 106.75]
> 
> The two arrays are related by position. For example, Availability\_Period[  
> _n_] corresponds to Availability\_Price[_n_].
> 
> Might this help? Just a thought...
> 
> Regards,  
> Brian

--  
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:** ![waldemar](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@waldemar](https://discuss.elastic.co/u/waldemar)\
**Post date:** [May 20, 2013, 10:38am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/13 "2013-05-20T10:38:07Z")

</div>

Hi to All,

For now my solution to this problem is to add (force) counter to the data  
field name.  
For example, my sample doc is:  
"Availability":{

```
0: {
    Period: 2013-06-15|2013-06-22
    Price: 931
}
1: {
    Period: 2013-09-07|2013-09-14
    Price: 931
}

```

And then I can iterate through data and associate Period with Price.  
So my script will look something like this:  
"script\_fields":{  
"test1":{  
"script":"i=0; return  
doc["AvailabilityPeriodV2."+i+".Period"].value+"="+doc["AvailabilityPeriodV2."+i+".Price"].value"  
}  
}

I know that this is not the most sophisticated solution, but it works.

If some one have better idea, then please share.

Thanks, and Best to all of you,  
Gzom

On Monday, 13 May 2013 18:34:14 UTC+2, InquiringMind wrote:

> Hi Gzom,
> 
> My suggestion is pretty much language-independent. My previous experience  
> was based on a C++ database engine I wrote (Elasticsearch blows it away in  
> the area of updates and smooth cluster replication; I blew away Lucene in  
> the areas of performance, phrases with wildcards, tock-solid bug-free geo  
> shapes, and hierarchy support. I would love to marry Elasticsearch with my  
> non-Lucene indexing magic, but that's a discussion for another day!) and  
> that C++ code is no longer available to me (but the ideas are!).
> 
> But the code (in whatever language you wish) should be pretty simple. The  
> important concept is that your documents contain two multi-valued fields,  
> and that the relationship between the values in each of those fields is  
> handled by your application (load, and query) and not internal to  
> Elasticsearch. For some index N, the value of field1[N] corresponds to the  
> value of field2[N].
> 
> In my previous experience, I had one lat and lon pair for each document,  
> but across the hierarchy there could be many, many documents that matched  
> the query. So I collected the lat and lon values from the related documents  
> for a hierarchy. I was left with two arrays of the same size (lat and  
> lon). I would apply each pair of lat[N], lon[N] to the geo index and geo  
> filter (either circle or generic polygon) and if any of the pairs was  
> within the range the documents from that hierarchy were kept (in general,  
> of course). And as I was determining the inclusion, I also updated the  
> document (in the response, not on disk) with the calculated distance; this  
> enabled a sort by (smallest) distance and also enabled the client to  
> highlight the smallest distance so it was easy to see which distance caused  
> the sorting placement of the hierarchy's returned documents.
> 
> Regards,  
> Brian
> 
> On Monday, May 13, 2013 3:37:25 AM UTC-4, Waldemar Ch. wrote:
> 
> > Hi Brian,
> > 
> > Could you provide some code example?  
> > Because I have tested everything what's on internet, and without success.  
> > Maybe I'm doing something wrong?
> > 
> > Best,  
> > Gzom
> > 
> > PS. Thanks for your idea, I will check that in next few day (now too many  
> > responsibilities), and come back with results .
> > 
> > W dniu 2013-05-10 18:59, InquiringMind pisze:
> > 
> > Hi Gzom,
> > 
> > Just a thought.... it's worked well for me in a past life when creating  
> > arrays of [lat, lon] pairs but the database only supported multivalued  
> > fields of basic types but not complex types.
> > 
> > Perhaps:
> > 
> > "Availability\_Period" : [ "2013-06-05|2013-05-09",  
> > "2013-06-05|2013-05-09" ]
> > 
> > "Availability\_Price" : [53.37, 106.75]
> > 
> > The two arrays are related by position. For example,  
> > Availability\_Period[_n_] corresponds to Availability\_Price[_n_].
> > 
> > Might this help? Just a thought...
> > 
> > Regards,  
> > Brian

--  
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:35am UTC](https://discuss.elastic.co/t/how-to-iterate-through-array-of-objects/11898/14 "2017-07-06T02:35:45Z")

</div>


