# Terms facets : problem when using script\_field on an object containing an array

**URL:** <https://discuss.elastic.co/t/terms-facets-problem-when-using-script-field-on-an-object-containing-an-array/9160>\
**Category:** Elasticsearch\
**Created:** [September 26, 2012, 4:39pm UTC](https://discuss.elastic.co/t/terms-facets-problem-when-using-script-field-on-an-object-containing-an-array/9160 "2012-09-26T16:39:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephane\_Raux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephane_raux/32/2349_2.png) [@Stephane\_Raux](https://discuss.elastic.co/u/Stephane_Raux)\
**Post date:** [September 26, 2012, 4:39pm UTC](https://discuss.elastic.co/t/terms-facets-problem-when-using-script-field-on-an-object-containing-an-array/9160/1 "2012-09-26T16:39:53Z")

</div>

Hi,

I am trying to use a term facet with a script\_field parameter.  
It works well when I use it on a simple field, or when I use it whith  
a simple object (like '{"object":{"key":"value"}}'), but it seems to  
fail when I try to use it on an object containing an array.

Here is a gist example (with facets output and errors) :

> <https://gist.github.com/straux/3788994>

The first query also yielded a strange result, with a missing value.

Does anyone have any insight about what is going on ?

Thanks,

Stéphane

--

---

<div class="post-metadata">

**Author:** ![Stephane\_Raux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephane_raux/32/2349_2.png) [@Stephane\_Raux](https://discuss.elastic.co/u/Stephane_Raux)\
**Post date:** [September 27, 2012, 9:05am UTC](https://discuss.elastic.co/t/terms-facets-problem-when-using-script-field-on-an-object-containing-an-array/9160/2 "2012-09-27T09:05:10Z")

</div>

I finally found the answer : you have to use the mvel projection  
operator to iterate over the collection :  
[http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds](http://mvel.codehaus.org/MVEL+2.0+Projections+and+Folds)

I my example, you can do :  
{  
"terms" : {  
"script\_field" : "(field in \_source.array)",  
"size" : 10  
}  
instead of "script\_field" : "\_source.array.field"

This does not explains why the "a" element is not taken in account in  
the first facets performed on the index, though.

Best,

Stéphane

2012/9/26 Stéphane Raux [stephane.raux@gmail.com](mailto:stephane.raux@gmail.com):

> Hi,
> 
> I am trying to use a term facet with a script\_field parameter.  
> It works well when I use it on a simple field, or when I use it whith  
> a simple object (like '{"object":{"key":"value"}}'), but it seems to  
> fail when I try to use it on an object containing an array.
> 
> Here is a gist example (with facets output and errors) :  
> [Elastic search terms facets : problems when performing script\_field on an object with array · GitHub](https://gist.github.com/3788994)
> 
> The first query also yielded a strange result, with a missing value.
> 
> Does anyone have any insight about what is going on ?
> 
> Thanks,
> 
> Stéphane

--

---

<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:11am UTC](https://discuss.elastic.co/t/terms-facets-problem-when-using-script-field-on-an-object-containing-an-array/9160/3 "2017-07-06T03:11:11Z")

</div>


