# Problem when looping over an array with objects

**URL:** <https://discuss.elastic.co/t/problem-when-looping-over-an-array-with-objects/6037>\
**Category:** Elasticsearch\
**Created:** [December 1, 2011, 8:51am UTC](https://discuss.elastic.co/t/problem-when-looping-over-an-array-with-objects/6037 "2011-12-01T08:51:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrik\_Johansson](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@Patrik\_Johansson](https://discuss.elastic.co/u/Patrik_Johansson)\
**Post date:** [December 1, 2011, 8:51am UTC](https://discuss.elastic.co/t/problem-when-looping-over-an-array-with-objects/6037/1 "2011-12-01T08:51:32Z")

</div>

Hi!

I'm trying to create a script\_field by looping over an array of  
values. This works  
great with arrays with only ordinary items (e.g. integers or strings)  
but I can't figure out how to loop over an array  
of objects with properties.

When I try to loop over an array of objects I get this error back:  
CompileException[[Error: No field found for [weights]]\n[Near : {...  
tmp = ''; for(item : doc['weig ....}]\n  
^\n[Line: 1, Column: 1]]; nested:  
ElasticSearchIllegalArgumentException[No field found for [weights]];

My example code, requests and return values can be found here:

> <https://gist.github.com/patrikjohansson/1415070>

  
Stack trace from Elastic: [https://gist.github.com/1414981](https://gist.github.com/1414981)

Tested in both ES 0.17.7 and 0.18.5  
I'm using mvel as scripting language.

Is this a bug or i'm using the foreach-loop wrong?

//Patrik

---

<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, 4:15pm UTC](https://discuss.elastic.co/t/problem-when-looping-over-an-array-with-objects/6037/2 "2011-12-01T16:15:28Z")

</div>

If you are using doc[...] notation, then it loads up the indexed terms from  
the Lucene index, in this case, you need to point to actual field elements  
indexed in Lucene, for example: weights.lang. Note, if you use  
script\_fields, it makes little sense to use the doc notation cause it will  
cause all values to be loaded, use \_source.weights which will load the  
\_source of the relevant hit and parse it.

On Thu, Dec 1, 2011 at 10:51 AM, Patrik Johansson \<  
patrik.j.johansson@kentor.se\> wrote:

> Hi!
> 
> I'm trying to create a script\_field by looping over an array of  
> values. This works  
> great with arrays with only ordinary items (e.g. integers or strings)  
> but I can't figure out how to loop over an array  
> of objects with properties.
> 
> When I try to loop over an array of objects I get this error back:  
> CompileException[[Error: No field found for [weights]]\n[Near : {...  
> tmp = ''; for(item : doc['weig ....}]\n  
> ^\n[Line: 1, Column: 1]]; nested:  
> ElasticSearchIllegalArgumentException[No field found for [weights]];
> 
> My example code, requests and return values can be found here:  
> [Problem when looping over arrays with objects · GitHub](https://gist.github.com/1415070)  
> Stack trace from Elastic: [https://gist.github.com/1414981](https://gist.github.com/1414981)
> 
> Tested in both ES 0.17.7 and 0.18.5  
> I'm using mvel as scripting language.
> 
> Is this a bug or i'm using the foreach-loop wrong?
> 
> //Patrik

---

<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/problem-when-looping-over-an-array-with-objects/6037/3 "2017-07-06T03:46:43Z")

</div>


