# Terms stats facet using script on multiple fields

**URL:** <https://discuss.elastic.co/t/terms-stats-facet-using-script-on-multiple-fields/9126>\
**Category:** Elasticsearch\
**Created:** [September 25, 2012, 6:23am UTC](https://discuss.elastic.co/t/terms-stats-facet-using-script-on-multiple-fields/9126 "2012-09-25T06:23:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sujoysett](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@sujoysett](https://discuss.elastic.co/u/sujoysett)\
**Post date:** [September 25, 2012, 6:23am UTC](https://discuss.elastic.co/t/terms-stats-facet-using-script-on-multiple-fields/9126/1 "2012-09-25T06:23:12Z")

</div>

I often use terms facet script to do grouping on multiple fields, as  
following:

{  
"facets": {  
"my\_terms\_facet": {  
"_terms_": {  
"size": 5,  
"_script_": "doc['field1'].value + "~~~" +  
doc['field2'].value + "~~~" + doc['field3'].value"  
}  
}  
}  
}

The result is parsed on the response using client side scripts for  
necessary display formatting.

The above query gives only the count, whereas now I am looking for summing  
up based on a fourth field.

Is something possible on combining script and terms-stats facet? Something  
like following?

{  
"facets": {  
"my\_terms\_stats\_facet": {  
"_terms\_stats_": {  
"size": 5,  
"_key\_script_": "doc['field1'].value + "~~~" +  
doc['field2'].value + "~~~" + doc['field3'].value",  
"value\_field": "field4"  
}  
}  
}  
}

Thanks in advance,

- Sujoy.

--

---

<div class="post-metadata">

**Author:** ![sujoysett](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@sujoysett](https://discuss.elastic.co/u/sujoysett)\
**Post date:** [October 1, 2012, 5:20am UTC](https://discuss.elastic.co/t/terms-stats-facet-using-script-on-multiple-fields/9126/2 "2012-10-01T05:20:12Z")

</div>

Hi,

Any hint/hope on the following?

Thanks,

- Sujoy.

On Tuesday, September 25, 2012 11:53:12 AM UTC+5:30, Sujoy Sett wrote:

> I often use terms facet script to do grouping on multiple fields, as  
> following:
> 
> {  
> "facets": {  
> "my\_terms\_facet": {  
> "_terms_": {  
> "size": 5,  
> "_script_": "doc['field1'].value + "~~~" +  
> doc['field2'].value + "~~~" + doc['field3'].value"  
> }  
> }  
> }  
> }
> 
> The result is parsed on the response using client side scripts for  
> necessary display formatting.
> 
> The above query gives only the count, whereas now I am looking for summing  
> up based on a fourth field.
> 
> Is something possible on combining script and terms-stats facet? Something  
> like following?
> 
> {  
> "facets": {  
> "my\_terms\_stats\_facet": {  
> "_terms\_stats_": {  
> "size": 5,  
> "_key\_script_": "doc['field1'].value + "~~~" +  
> doc['field2'].value + "~~~" + doc['field3'].value",  
> "value\_field": "field4"  
> }  
> }  
> }  
> }
> 
> Thanks in advance,
> 
> - Sujoy.

--

---

<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:10am UTC](https://discuss.elastic.co/t/terms-stats-facet-using-script-on-multiple-fields/9126/3 "2017-07-06T03:10:42Z")

</div>


