# Terms facet

**URL:** <https://discuss.elastic.co/t/terms-facet/7310>\
**Category:** Elasticsearch\
**Created:** [April 12, 2012, 2:38pm UTC](https://discuss.elastic.co/t/terms-facet/7310 "2012-04-12T14:38:48Z")\
**Posts on this page:** 5\
**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:38pm UTC](https://discuss.elastic.co/t/terms-facet/7310/1 "2012-04-12T14:38:48Z")

</div>

Hi,

I have the following document structure:  
"simple\_array":{  
"dynamic":"true",  
"properties":{  
"field\_name":{  
"type":"string",  
"index":"not\_analyzed"  
},  
"last\_field":{  
"type":"boolean"  
}  
}  
}

Now I want to get terms facet for simple\_array.field\_name and it would be  
easy:  
{  
"facets":{  
"test\_facet":{  
"terms":{  
"field":"simple\_array.field\_name",  
}  
}  
}  
}

But now I want to get counted only that 'field\_name' fields that have  
'last\_field' set fo 'true'. I know that it is simple using nested type with  
facet filter. But I have a situation where simple\_array is only one of  
document fields and I have to use in request filters for other document  
fields - here nested type for 'simple\_array' won't work.

Thanks  
Best regards.

---

<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, 9:37pm UTC](https://discuss.elastic.co/t/terms-facet/7310/2 "2012-04-12T21:37:41Z")

</div>

As I can see, the best way would be:  
{  
"facets":{  
"test\_facet":{  
"terms":{  
"field":"simple\_array.field\_name",  
"script":"\_source.simple\_array[i].last\_field"  
}  
}  
}  
}

But how to get 'i' for given term ? 🙂 \_source.simple\_array.indexOf(term)  
does not work because simple\_array keeps documents containing 'term'...

I would really appreciate your help here 🙂

Best regards.  
Marcin.

2012/4/12 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)

> Hi,
> 
> I have the following document structure:  
> "simple\_array":{  
> "dynamic":"true",  
> "properties":{  
> "field\_name":{  
> "type":"string",  
> "index":"not\_analyzed"  
> },  
> "last\_field":{  
> "type":"boolean"  
> }  
> }  
> }
> 
> Now I want to get terms facet for simple\_array.field\_name and it would be  
> easy:  
> {  
> "facets":{  
> "test\_facet":{  
> "terms":{  
> "field":"simple\_array.field\_name",  
> }  
> }  
> }  
> }
> 
> But now I want to get counted only that 'field\_name' fields that have  
> 'last\_field' set fo 'true'. I know that it is simple using nested type with  
> facet filter. But I have a situation where simple\_array is only one of  
> document fields and I have to use in request filters for other document  
> fields - here nested type for 'simple\_array' won't work.
> 
> Thanks  
> Best regards.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 12, 2012, 10:13pm UTC](https://discuss.elastic.co/t/terms-facet/7310/3 "2012-04-12T22:13:06Z")

</div>

If all your field names are unique within sample\_array and you store  
source, you could do something like this:

```
"facets": {
    "test_facet":{
        "terms":{
            "field":"simple_array.field_name",
            "script" : "foreach(item : _source.simple_array) { 

```

if(item.field\_name.equals(term)) return item.last\_field }"  
}  
}  
}

However, when I find that I need to do something as complicated as this I,  
typically, consider it as an indication that I should refactor my schema.

On Thursday, April 12, 2012 5:37:41 PM UTC-4, Marcin Dojwa wrote:

> As I can see, the best way would be:  
> {  
> "facets":{  
> "test\_facet":{  
> "terms":{  
> "field":"simple\_array.field\_name",  
> "script":"\_source.simple\_array[i].last\_field"  
> }  
> }  
> }  
> }
> 
> But how to get 'i' for given term ? 🙂 \_source.simple\_array.indexOf(term)  
> does not work because simple\_array keeps documents containing 'term'...
> 
> I would really appreciate your help here 🙂
> 
> Best regards.  
> Marcin.
> 
> 2012/4/12 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)
> 
> > Hi,
> > 
> > I have the following document structure:  
> > "simple\_array":{  
> > "dynamic":"true",  
> > "properties":{  
> > "field\_name":{  
> > "type":"string",  
> > "index":"not\_analyzed"  
> > },  
> > "last\_field":{  
> > "type":"boolean"  
> > }  
> > }  
> > }
> > 
> > Now I want to get terms facet for simple\_array.field\_name and it would be  
> > easy:  
> > {  
> > "facets":{  
> > "test\_facet":{  
> > "terms":{  
> > "field":"simple\_array.field\_name",  
> > }  
> > }  
> > }  
> > }
> > 
> > But now I want to get counted only that 'field\_name' fields that have  
> > 'last\_field' set fo 'true'. I know that it is simple using nested type with  
> > facet filter. But I have a situation where simple\_array is only one of  
> > document fields and I have to use in request filters for other document  
> > fields - here nested type for 'simple\_array' won't work.
> > 
> > Thanks  
> > Best regards.

---

<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 13, 2012, 6:03am UTC](https://discuss.elastic.co/t/terms-facet/7310/4 "2012-04-13T06:03:11Z")

</div>

Thanks 🙂 it's good to know that scripts like this are allowed. But you are  
right, I will modify document structure in that case.

Thanks for help  
Marcin

W dniu piątek, 13 kwietnia 2012 użytkownik Igor Motov napisał:

> If all your field names are unique within sample\_array and you store  
> source, you could do something like this:
> 
> ```
> "facets": {
> "test_facet":{
> "terms":{
> "field":"simple_array.field_name",
> "script" : "foreach(item : _source.simple_array) {
> 
> ```
> 
> if(item.field\_name.equals(term)) return item.last\_field }"  
> }  
> }  
> }
> 
> However, when I find that I need to do something as complicated as this I,  
> typically, consider it as an indication that I should refactor my schema.
> 
> On Thursday, April 12, 2012 5:37:41 PM UTC-4, Marcin Dojwa wrote:
> 
> > As I can see, the best way would be:  
> > {  
> > "facets":{  
> > "test\_facet":{  
> > "terms":{  
> > "field":"simple\_array.field\_na\*\*me",  
> > "script":"_source.simple_\*\*array[i].last\_field"  
> > }  
> > }  
> > }  
> > }
> > 
> > But how to get 'i' for given term ? 🙂 \_source.simple\_array.\*\*indexOf(term)  
> > does not work because simple\_array keeps documents containing 'term'...
> > 
> > I would really appreciate your help here 🙂
> > 
> > Best regards.  
> > Marcin.
> > 
> > 2012/4/12 Marcin Dojwa \<[m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com) \<javascript:\_e({},  
> > 'cvml', 'm.dojwa@livechatinc.com');\>\>
> > 
> > > Hi,
> > > 
> > > I have the following document structure:  
> > > "simple\_array":{  
> > > "dynamic":"true",  
> > > "properties":{  
> > > "field\_name":{  
> > > "type":"string",  
> > > "index":"not\_analyzed"  
> > > },  
> > > "last\_field":{  
> > > "type":"boolean"  
> > > }  
> > > }  
> > > }
> > > 
> > > Now I want to get terms facet for simple\_array.field\_name and it would  
> > > be easy:  
> > > {  
> > > "facets":{  
> > > "test\_facet":{  
> > > "terms":{  
> > > "field":"simple\_array.field\_\*\*name",  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > But now I want to get counted only that 'field\_name' fields that have  
> > > 'last\_field' set fo 'true'. I know that it is simple using nested type with  
> > > facet filter. But I have a situation where simple\_array is only one of  
> > > document fields and I have to use in request filters for other document  
> > > fields - here nested type for 'simple\_array' won't work.
> > > 
> > > Thanks  
> > > Best regards.

---

<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/terms-facet/7310/5 "2017-07-06T03:32:46Z")

</div>


