# Multiple key\_fields for Statistical Facet

**URL:** <https://discuss.elastic.co/t/multiple-key-fields-for-statistical-facet/12014>\
**Category:** Elasticsearch\
**Created:** [May 17, 2013, 2:37pm UTC](https://discuss.elastic.co/t/multiple-key-fields-for-statistical-facet/12014 "2013-05-17T14:37:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OffensivelyBad\_2](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@OffensivelyBad\_2](https://discuss.elastic.co/u/OffensivelyBad_2)\
**Post date:** [May 17, 2013, 2:37pm UTC](https://discuss.elastic.co/t/multiple-key-fields-for-statistical-facet/12014/1 "2013-05-17T14:37:43Z")

</div>

I have a need to use two key\_fields for a statistical facet, but I'm not  
sure if this is possible.

I have several fields that have a name, a date, and a number associated  
with them. I'd like to do the SQL equivalent of:

SELECT name, date, SUM(number) FROM es GROUP BY name, date

I'm trying to do something like this is ES but its not working:

"facets": {  
"name\_date\_stats": {  
"terms\_stats": {  
"key\_field": ["name","date"],  
"value\_field": "number",  
"order": "term"  
}  
}  
}

I've tried several variations and haven't gotten the result I'm looking  
for. Any help would be greatly appreciated!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Boaz\_Leskes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boaz_leskes/32/723_2.png) [@Boaz\_Leskes](https://discuss.elastic.co/u/Boaz_Leskes)\
**Post date:** [May 22, 2013, 1:23pm UTC](https://discuss.elastic.co/t/multiple-key-fields-for-statistical-facet/12014/2 "2013-05-22T13:23:53Z")

</div>

Hi,

With the currently released version this is not directly possible. There is  
an open issue requesting the ability to script the key field. This would  
allow you to combine the two fields into one key as you want. You can watch  
the issue here: [key\_script for term stats facets · Issue #3004 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/3004)

Cheers,  
Boaz

On Friday, May 17, 2013 4:37:43 PM UTC+2, OffensivelyBad wrote:

> I have a need to use two key\_fields for a statistical facet, but I'm not  
> sure if this is possible.
> 
> I have several fields that have a name, a date, and a number associated  
> with them. I'd like to do the SQL equivalent of:
> 
> SELECT name, date, SUM(number) FROM es GROUP BY name, date
> 
> I'm trying to do something like this is ES but its not working:
> 
> "facets": {  
> "name\_date\_stats": {  
> "terms\_stats": {  
> "key\_field": ["name","date"],  
> "value\_field": "number",  
> "order": "term"  
> }  
> }  
> }
> 
> I've tried several variations and haven't gotten the result I'm looking  
> for. Any help would be greatly appreciated!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:35am UTC](https://discuss.elastic.co/t/multiple-key-fields-for-statistical-facet/12014/3 "2017-07-06T02:35:25Z")

</div>


