# Terms Facet with Histograms

**URL:** <https://discuss.elastic.co/t/terms-facet-with-histograms/14427>\
**Category:** Elasticsearch\
**Created:** [November 15, 2013, 9:56am UTC](https://discuss.elastic.co/t/terms-facet-with-histograms/14427 "2013-11-15T09:56:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaidik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vaidik/32/1134_2.png) [@vaidik](https://discuss.elastic.co/u/vaidik)\
**Post date:** [November 15, 2013, 9:56am UTC](https://discuss.elastic.co/t/terms-facet-with-histograms/14427/1 "2013-11-15T09:56:35Z")

</div>

Hi,

I have a requirement according to which I need the count of terms but in  
range buckets. Consider this scenario:

I have inserted the following documents in ES in an index:

{ "timestamp": 120000000, "combination": "1", "goal": "1" }  
{ "timestamp": 120000002, "combination": "1", "goal": "2" }  
{ "timestamp": 120000003, "combination": "2", "goal": "1" }  
{ "timestamp": 120000015, "combination": "1", "goal": "2" }  
{ "timestamp": 120000040, "combination": "1", "goal": "3" }  
{ "timestamp": 120000043, "combination": "2", "goal": "3" }  
{ "timestamp": 120000057, "combination": "1", "goal": "2" }

The following terms facet on the above data:  
{  
"query": {  
"match\_all": {}  
},  
"facets": {  
"comb\_goal": {  
{  
"terms": {  
"script\_field": "\_source.combination + '-' + \_source.goal",  
}  
}  
}  
}  
}

would give me the following result:  
{  
"terms": [  
{  
"term": "1-1",  
"count": 1,  
},  
{  
"term": "1-2",  
"count": 3,  
},  
{  
"term": "1-3",  
"count": 1,  
},  
{  
"term": "2-1",  
"count": 1,  
},  
{  
"term": "2-3",  
"count": 1,  
},  
]  
}

My exact requirement is to get the above term counts but broken down into  
buckets based on timestamp intervals. In a way, I want a histogram that  
does terms counts instead of acting on numeric fields and doing  
mathematical aggregation on it.

Is this possible at all using facets? The only way I see this possible is  
by using multiple terms facets as I am using with range queries to handle  
the timestamp buckets. But then I will have to form the buckets by myself  
and this will mean a large number of queries if my date range is large. One  
way to reduce the number of queries would be to use multi-search queries.  
But isn't there a simpler way?

Looking forward to some valuable inputs on this problem.

Thanks,  
Vaidik

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 19, 2013, 5:48am UTC](https://discuss.elastic.co/t/terms-facet-with-histograms/14427/2 "2013-11-19T05:48:01Z")

</div>

I guess you will be able to do it with elasticsearch 1.0 and the new aggregation feature.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 15 nov. 2013 à 10:56, Vaidik Kapoor [kapoor.vaidik@gmail.com](mailto:kapoor.vaidik@gmail.com) a écrit :

Hi,

I have a requirement according to which I need the count of terms but in range buckets. Consider this scenario:

I have inserted the following documents in ES in an index:

{ "timestamp": 120000000, "combination": "1", "goal": "1" }  
{ "timestamp": 120000002, "combination": "1", "goal": "2" }  
{ "timestamp": 120000003, "combination": "2", "goal": "1" }  
{ "timestamp": 120000015, "combination": "1", "goal": "2" }  
{ "timestamp": 120000040, "combination": "1", "goal": "3" }  
{ "timestamp": 120000043, "combination": "2", "goal": "3" }  
{ "timestamp": 120000057, "combination": "1", "goal": "2" }

The following terms facet on the above data:  
{  
"query": {  
"match\_all": {}  
},  
"facets": {  
"comb\_goal": {  
{  
"terms": {  
"script\_field": "\_source.combination + '-' + \_source.goal",  
}  
}  
}  
}  
}

would give me the following result:  
{  
"terms": [  
{  
"term": "1-1",  
"count": 1,  
},  
{  
"term": "1-2",  
"count": 3,  
},  
{  
"term": "1-3",  
"count": 1,  
},  
{  
"term": "2-1",  
"count": 1,  
},  
{  
"term": "2-3",  
"count": 1,  
},  
]  
}

My exact requirement is to get the above term counts but broken down into buckets based on timestamp intervals. In a way, I want a histogram that does terms counts instead of acting on numeric fields and doing mathematical aggregation on it.

Is this possible at all using facets? The only way I see this possible is by using multiple terms facets as I am using with range queries to handle the timestamp buckets. But then I will have to form the buckets by myself and this will mean a large number of queries if my date range is large. One way to reduce the number of queries would be to use multi-search queries. But isn't there a simpler way?

Looking forward to some valuable inputs on this problem.

## Thanks, Vaidik

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).

--  
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:06am UTC](https://discuss.elastic.co/t/terms-facet-with-histograms/14427/3 "2017-07-06T02:06:19Z")

</div>


