# Termstats facet for multiple string fields

**URL:** <https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151>\
**Category:** Elasticsearch\
**Created:** [March 23, 2011, 10:59pm UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151 "2011-03-23T22:59:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://discuss.elastic.co/u/jason)\
**Post date:** [March 23, 2011, 10:59pm UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/1 "2011-03-23T22:59:11Z")

</div>

Hi,

I got termsstats facet code working in Java for a single non-numeric  
key field. The way I did this is I introduced a new numeric field  
called "counter" that always has a value 1. Here is an example of curl  
version of my code:

```
"query" : {
	"match_all": {}
 },
"facets" : {
	"facet1" : {
		"terms_stats" : {
			"key_field" : "field1",
			"value_field" : "counter"
		}
	}
}

```

How do I specify more than one key field? Does ES support this  
feature today, or it is currently under development?

Thank you,  
Eugene.

---

<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:** [March 23, 2011, 11:05pm UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/2 "2011-03-23T23:05:12Z")

</div>

Regarding the count, you can simply use a value script that is simply "1".

Regarding the terms stats on multiple key fields, you can simply have several terms stats facets, each on different field. Or are you looking for something different?  
On Thursday, March 24, 2011 at 12:59 AM, eugene wrote:

> Hi,
> 
> I got termsstats facet code working in Java for a single non-numeric  
> key field. The way I did this is I introduced a new numeric field  
> called "counter" that always has a value 1. Here is an example of curl  
> version of my code:
> 
> "query" : {  
> "match\_all": {}  
> },  
> "facets" : {  
> "facet1" : {  
> "terms\_stats" : {  
> "key\_field" : "field1",  
> "value\_field" : "counter"  
> }  
> }  
> }
> 
> How do I specify more than one key field? Does ES support this  
> feature today, or it is currently under development?
> 
> Thank you,  
> Eugene.

---

<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:** [March 23, 2011, 11:06pm UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/3 "2011-03-23T23:06:55Z")

</div>

Wait, now I am a bit confused :), why use term stats to get how many times a term occurs, you can just use the terms facet, no?  
On Thursday, March 24, 2011 at 1:05 AM, Shay Banon wrote:

> Regarding the count, you can simply use a value script that is simply "1".
> 
> Regarding the terms stats on multiple key fields, you can simply have several terms stats facets, each on different field. Or are you looking for something different?  
> On Thursday, March 24, 2011 at 12:59 AM, eugene wrote:
> 
> > Hi,
> > 
> > I got termsstats facet code working in Java for a single non-numeric  
> > key field. The way I did this is I introduced a new numeric field  
> > called "counter" that always has a value 1. Here is an example of curl  
> > version of my code:
> > 
> > "query" : {  
> > "match\_all": {}  
> > },  
> > "facets" : {  
> > "facet1" : {  
> > "terms\_stats" : {  
> > "key\_field" : "field1",  
> > "value\_field" : "counter"  
> > }  
> > }  
> > }
> > 
> > How do I specify more than one key field? Does ES support this  
> > feature today, or it is currently under development?
> > 
> > Thank you,  
> > Eugene.

---

<div class="post-metadata">

**Author:** ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://discuss.elastic.co/u/jason)\
**Post date:** [March 23, 2011, 11:08pm UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/4 "2011-03-23T23:08:32Z")

</div>

I am looking for grouping fields, equivalent to sql query "select  
field1, field2, sum(counter) from my\_table group by field1, field2",  
which implies that I need to have 2 key fields under the same facet.

On Mar 23, 4:05 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Regarding the count, you can simply use a value script that is simply "1".
> 
> Regarding the terms stats on multiple key fields, you can simply have several terms stats facets, each on different field. Or are you looking for something different?
> 
> On Thursday, March 24, 2011 at 12:59 AM, eugene wrote:
> 
> > Hi,
> 
> > I got termsstats facet code working in Java for a single non-numeric  
> > key field. The way I did this is I introduced a new numeric field  
> > called "counter" that always has a value 1. Here is an example of curl  
> > version of my code:
> 
> > "query" : {  
> > "match\_all": {}  
> > },  
> > "facets" : {  
> > "facet1" : {  
> > "terms\_stats" : {  
> > "key\_field" : "field1",  
> > "value\_field" : "counter"  
> > }  
> > }  
> > }
> 
> > How do I specify more than one key field? Does ES support this  
> > feature today, or it is currently under development?
> 
> > Thank you,  
> > Eugene.- Hide quoted text -
> 
> - Show quoted text -

---

<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:** [March 23, 2011, 11:26pm UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/5 "2011-03-23T23:26:26Z")

</div>

So, you want to get counts for tuples of field1 and field2? You can do that using terms stats and script field, by creating a script that combined the two: doc['field1'].value + "\_" + doc['field2'].value. Not amazing in terms of perf because of the string generation, but, might be good for you.  
On Thursday, March 24, 2011 at 1:08 AM, eugene wrote:

> I am looking for grouping fields, equivalent to sql query "select  
> field1, field2, sum(counter) from my\_table group by field1, field2",  
> which implies that I need to have 2 key fields under the same facet.
> 
> On Mar 23, 4:05 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Regarding the count, you can simply use a value script that is simply "1".
> > 
> > Regarding the terms stats on multiple key fields, you can simply have several terms stats facets, each on different field. Or are you looking for something different?
> > 
> > On Thursday, March 24, 2011 at 12:59 AM, eugene wrote:
> > 
> > > Hi,
> > 
> > > I got termsstats facet code working in Java for a single non-numeric  
> > > key field. The way I did this is I introduced a new numeric field  
> > > called "counter" that always has a value 1. Here is an example of curl  
> > > version of my code:
> > 
> > > "query" : {  
> > > "match\_all": {}  
> > > },  
> > > "facets" : {  
> > > "facet1" : {  
> > > "terms\_stats" : {  
> > > "key\_field" : "field1",  
> > > "value\_field" : "counter"  
> > > }  
> > > }  
> > > }
> > 
> > > How do I specify more than one key field? Does ES support this  
> > > feature today, or it is currently under development?
> > 
> > > Thank you,  
> > > Eugene.- Hide quoted text -
> > 
> > - Show quoted text -

---

<div class="post-metadata">

**Author:** ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://discuss.elastic.co/u/jason)\
**Post date:** [March 31, 2011, 1:57am UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/6 "2011-03-31T01:57:40Z")

</div>

Does that mean I should create a new script\_field, and then use it as  
a key\_field in the terms\_stats facet?

On Mar 23, 4:26 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> So, you want to get counts for tuples of field1 and field2? You can do that using terms stats and script field, by creating a script that combined the two: doc['field1'].value + "\_" + doc['field2'].value. Not amazing in terms of perf because of the string generation, but, might be good for you.
> 
> On Thursday, March 24, 2011 at 1:08 AM, eugene wrote:
> 
> > I am looking for grouping fields, equivalent to sql query "select  
> > field1, field2, sum(counter) from my\_table group by field1, field2",  
> > which implies that I need to have 2 key fields under the same facet.
> 
> > On Mar 23, 4:05 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > Regarding the count, you can simply use a value script that is simply "1".
> 
> > > Regarding the terms stats on multiple key fields, you can simply have several terms stats facets, each on different field. Or are you looking for something different?
> 
> > > On Thursday, March 24, 2011 at 12:59 AM, eugene wrote:
> > > 
> > > > Hi,
> 
> > > > I got termsstats facet code working in Java for a single non-numeric  
> > > > key field. The way I did this is I introduced a new numeric field  
> > > > called "counter" that always has a value 1. Here is an example of curl  
> > > > version of my code:
> 
> > > > "query" : {  
> > > > "match\_all": {}  
> > > > },  
> > > > "facets" : {  
> > > > "facet1" : {  
> > > > "terms\_stats" : {  
> > > > "key\_field" : "field1",  
> > > > "value\_field" : "counter"  
> > > > }  
> > > > }  
> > > > }
> 
> > > > How do I specify more than one key field? Does ES support this  
> > > > feature today, or it is currently under development?
> 
> > > > Thank you,  
> > > > Eugene.- Hide quoted text -
> 
> > > - Show quoted text -- Hide quoted text -
> 
> - Show quoted text -

---

<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, 4:09am UTC](https://discuss.elastic.co/t/termstats-facet-for-multiple-string-fields/4151/7 "2017-07-06T04:09:05Z")

</div>


