# Term Scripts in terms facet

**URL:** <https://discuss.elastic.co/t/term-scripts-in-terms-facet/7309>\
**Category:** Elasticsearch\
**Created:** [April 12, 2012, 2:30pm UTC](https://discuss.elastic.co/t/term-scripts-in-terms-facet/7309 "2012-04-12T14:30:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)\
**Post date:** [April 12, 2012, 2:30pm UTC](https://discuss.elastic.co/t/term-scripts-in-terms-facet/7309/1 "2012-04-12T14:30:27Z")

</div>

Hi,

Lets assume that test\_facet is an array with two fields: field\_name and  
field\_value. The following request:  
{  
"facets":{  
"test\_facet":{  
"terms":{  
"field":"test\_array.field\_name",  
}  
}  
}  
}

will return facet grouping by field\_name.

Now I want to get grouping by field\_name + ":" + field\_value.  
Using something like this:  
{  
"facets":{  
"test\_facet":{  
"terms":{  
"field":"test\_array.field\_name",  
"script":"term + ":" + field\_value"  
}  
}  
}  
}

unfortunatelly does not work.

Do you know if it is possible to get such grouping? If it is possible then  
how ? 🙂

Thanks  
Best regards  
Marcin

---

<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:** [April 13, 2012, 12:24pm UTC](https://discuss.elastic.co/t/term-scripts-in-terms-facet/7309/2 "2012-04-13T12:24:01Z")

</div>

You want to access field\_value from the doc? In this case, you need to do  
doc.field\_value.value. But note, if both are multi valued, then there won't  
be any ordering guaranteed between them (the field\_name and field\_value).

On Thu, Apr 12, 2012 at 5:30 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:

> Hi,
> 
> Lets assume that test\_facet is an array with two fields: field\_name and  
> field\_value. The following request:  
> {  
> "facets":{  
> "test\_facet":{  
> "terms":{  
> "field":"test\_array.field\_name",  
> }  
> }  
> }  
> }
> 
> will return facet grouping by field\_name.
> 
> Now I want to get grouping by field\_name + ":" + field\_value.  
> Using something like this:  
> {  
> "facets":{  
> "test\_facet":{  
> "terms":{  
> "field":"test\_array.field\_name",  
> "script":"term + ":" + field\_value"  
> }  
> }  
> }  
> }
> 
> unfortunatelly does not work.
> 
> Do you know if it is possible to get such grouping? If it is possible then  
> how ? 🙂
> 
> Thanks  
> Best regards  
> Marcin

---

<div class="post-metadata">

**Author:** ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)\
**Post date:** [April 14, 2012, 10:45am UTC](https://discuss.elastic.co/t/term-scripts-in-terms-facet/7309/3 "2012-04-14T10:45:23Z")

</div>

Thank you Shay, I found the answer using complicated scripts (not so  
efficient) in other posts. I guess the best way here is to reorganize  
document structure to do it other way 🙂

Thanks again.  
Best regards.

2012/4/13 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)

> You want to access field\_value from the doc? In this case, you need to do  
> doc.field\_value.value. But note, if both are multi valued, then there won't  
> be any ordering guaranteed between them (the field\_name and field\_value).
> 
> On Thu, Apr 12, 2012 at 5:30 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:
> 
> > Hi,
> > 
> > Lets assume that test\_facet is an array with two fields: field\_name and  
> > field\_value. The following request:  
> > {  
> > "facets":{  
> > "test\_facet":{  
> > "terms":{  
> > "field":"test\_array.field\_name",  
> > }  
> > }  
> > }  
> > }
> > 
> > will return facet grouping by field\_name.
> > 
> > Now I want to get grouping by field\_name + ":" + field\_value.  
> > Using something like this:  
> > {  
> > "facets":{  
> > "test\_facet":{  
> > "terms":{  
> > "field":"test\_array.field\_name",  
> > "script":"term + ":" + field\_value"  
> > }  
> > }  
> > }  
> > }
> > 
> > unfortunatelly does not work.
> > 
> > Do you know if it is possible to get such grouping? If it is possible  
> > then how ? 🙂
> > 
> > Thanks  
> > Best regards  
> > Marcin

---

<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:32am UTC](https://discuss.elastic.co/t/term-scripts-in-terms-facet/7309/4 "2017-07-06T03:32:35Z")

</div>


