# Elasticsearch terms\_stats doubt

**URL:** <https://discuss.elastic.co/t/elasticsearch-terms-stats-doubt/15648>\
**Category:** Elasticsearch\
**Created:** [February 6, 2014, 5:32pm UTC](https://discuss.elastic.co/t/elasticsearch-terms-stats-doubt/15648 "2014-02-06T17:32:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raul\_de\_la\_Hoz\_Garri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_de_la_hoz_garri/32/1806_2.png) [@Raul\_de\_la\_Hoz\_Garri](https://discuss.elastic.co/u/Raul_de_la_Hoz_Garri)\
**Post date:** [February 6, 2014, 5:32pm UTC](https://discuss.elastic.co/t/elasticsearch-terms-stats-doubt/15648/1 "2014-02-06T17:32:15Z")

</div>

Hello,

I'm new with ES and I'm trying to retrieve aggreated data by term, I have  
something like:

{"total": 10,"product": 1,"campaign": 1},  
{"total": 10,"product": 1,"campaign": 2},  
{"total": 10,"product": 1,"campaign": 1},  
{"total": 10,"product": 2,"campaign": 1}

So, this is my facet:

```
"x": {
  "terms_stats": {
    "key_field": [
      "product",
      "campaign"
    ],
    "value_field": "total"
  }

```

This is the result:

[  
{  
term: 1  
count: 3  
total\_count: 3  
min: 10  
max: 10  
total: 30  
mean: 10  
}  
{  
term: 2  
count: 1  
total\_count: 1  
min: 10  
max: 10  
total: 10  
mean: 10  
}  
]

This information is refered to campaigns, not both product and campaign  
terms. My expectation was something like:

terms: [  
{  
term: [1,1]  
count: 2  
total\_count: 2  
min: 10  
max: 10  
total: 20  
mean: 10  
}  
{  
term: [1,2]  
count: 1  
total\_count: 1  
min: 10  
max: 10  
total: 10  
mean: 10  
}  
{  
term: [2,1]  
count: 1  
total\_count: 1  
min: 10  
max: 10  
total: 10  
mean: 10  
}  
]

I don't know if ES has a way to do that.

Somebody has an idea?

Thanks in advance.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/04860e26-47be-4062-9fcb-128095022a76%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/04860e26-47be-4062-9fcb-128095022a76%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 7, 2014, 1:41pm UTC](https://discuss.elastic.co/t/elasticsearch-terms-stats-doubt/15648/2 "2014-02-07T13:41:22Z")

</div>

Raul,

Unfortunately, the terms\_stats facet does not support scripting the terms  
key. You can try aggregations (ES 1.0) or if you want to use term\_stats  
facet, you will need to introduce an extra field that is a combination of  
product and campaign to your JSON documents.

{"total": 10,"product": 1,"campaign": 1,"product\_campaign": "11"},  
{"total": 10,"product": 1,"campaign": 2,"product\_campaign": "12"},  
{"total": 10,"product": 1,"campaign": 1,"product\_campaign": "11"},  
{"total": 10,"product": 2,"campaign": 1,"product\_campaign": "21"}

And then

```
"x": {
  "terms_stats": {
    "key_field": [
      "product_campaign"
    ],
    "value_field": "total"
  }

```

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9f6fde1c-c424-4a96-ae72-6142b52018b5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9f6fde1c-c424-4a96-ae72-6142b52018b5%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Raul\_de\_la\_Hoz\_Garri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_de_la_hoz_garri/32/1806_2.png) [@Raul\_de\_la\_Hoz\_Garri](https://discuss.elastic.co/u/Raul_de_la_Hoz_Garri)\
**Post date:** [February 10, 2014, 7:43am UTC](https://discuss.elastic.co/t/elasticsearch-terms-stats-doubt/15648/3 "2014-02-10T07:43:28Z")

</div>

Thanks Binh,

The problem is that these aren't not the only one case which I want to  
aggregating them that way is rather complicated.

So, there is a chance to do that in ES 1.0. I'm going to start its  
installation.

Chers,  
Raúl.

On Friday, February 7, 2014 2:41:22 PM UTC+1, Binh Ly wrote:

> Raul,
> 
> Unfortunately, the terms\_stats facet does not support scripting the terms  
> key. You can try aggregations (ES 1.0) or if you want to use term\_stats  
> facet, you will need to introduce an extra field that is a combination of  
> product and campaign to your JSON documents.
> 
> {"total": 10,"product": 1,"campaign": 1,"product\_campaign": "11"},  
> {"total": 10,"product": 1,"campaign": 2,"product\_campaign": "12"},  
> {"total": 10,"product": 1,"campaign": 1,"product\_campaign": "11"},  
> {"total": 10,"product": 2,"campaign": 1,"product\_campaign": "21"}
> 
> And then
> 
> ```
> "x": {
> "terms_stats": {
> "key_field": [
> "product_campaign"
> ],
> "value_field": "total"
> }
> 
> ```

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1a74ce8e-fc6f-4e2b-98ce-374c37c33f42%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1a74ce8e-fc6f-4e2b-98ce-374c37c33f42%40googlegroups.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, 1:51am UTC](https://discuss.elastic.co/t/elasticsearch-terms-stats-doubt/15648/4 "2017-07-06T01:51:32Z")

</div>


