# Just Pushed: Terms Stats Facets

**URL:** <https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952>\
**Category:** Elasticsearch\
**Created:** [February 19, 2011, 9:38pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952 "2011-02-19T21:38:02Z")\
**Posts on this page:** 14\
**Page:** 1

<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:** [February 19, 2011, 9:38pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/1 "2011-02-19T21:38:02Z")

</div>

Heya,

Just pushed the (long requested) terms stats facet: [https://github.com/elasticsearch/elasticsearch/issues/705](https://github.com/elasticsearch/elasticsearch/issues/705).

-shay.banon

---

<div class="post-metadata">

**Author:** ![IvanBrusic](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IvanBrusic](https://discuss.elastic.co/u/IvanBrusic)\
**Post date:** [February 19, 2011, 11:08pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/2 "2011-02-19T23:08:59Z")

</div>

Excellent news. I have never officially requested it, but it is  
feature that I have needed. I have been using term facet\_filters with  
the stats facet to achieve the same effect (thankfully the terms are a  
small predetermined set). Will test it out soon.

Thanks!

Ivan

On Feb 19, 4:38 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Heya,
> 
> Just pushed the (long requested) terms stats facet:[Search: Terms Stats Facet · Issue #705 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/705).
> 
> -shay.banon

---

<div class="post-metadata">

**Author:** ![Wojciech\_Durczynski](https://avatars.discourse-cdn.com/v4/letter/w/fbc32d/32.png) [@Wojciech\_Durczynski](https://discuss.elastic.co/u/Wojciech_Durczynski)\
**Post date:** [February 23, 2011, 2:51pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/3 "2011-02-23T14:51:57Z")

</div>

I'm very glad to see this functionality. Thank you very much for it.

As described in guide about statistical facet:  
The statistical data include count, total, sum of squares, mean  
(average), minimum, maximum, variance, and standard deviation.  
Will terms stats facet include them also? I see total and count only  
now. And my usecase described here:  
[http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/6e524daeb77d7060/d518c70310eb0405](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/6e524daeb77d7060/d518c70310eb0405)  
will be easily resolved with min values in terms stats facets.

Regards  
Wojciech Durczyński

---

<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:** [February 23, 2011, 5:29pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/4 "2011-02-23T17:29:34Z")

</div>

Right, it does not include the min/max, open an issue and I will add it 🙂  
On Wednesday, February 23, 2011 at 4:51 PM, Wojciech DurczyÅski wrote:

> I'm very glad to see this functionality. Thank you very much for it.
> 
> As described in guide about statistical facet:  
> The statistical data include count, total, sum of squares, mean  
> (average), minimum, maximum, variance, and standard deviation.  
> Will terms stats facet include them also? I see total and count only  
> now. And my usecase described here:  
> [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/6e524daeb77d7060/d518c70310eb0405](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/6e524daeb77d7060/d518c70310eb0405)  
> will be easily resolved with min values in terms stats facets.
> 
> Regards  
> Wojciech DurczyÅski

---

<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:** [February 23, 2011, 6:37pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/5 "2011-02-23T18:37:35Z")

</div>

ok, pushed support for it. Min and max are in. if sum of squares is also required, it can be added as well (though every addition does come at a cost).  
On Wednesday, February 23, 2011 at 7:29 PM, Shay Banon wrote:

> Right, it does not include the min/max, open an issue and I will add it 🙂  
> On Wednesday, February 23, 2011 at 4:51 PM, Wojciech DurczyÅski wrote:
> 
> > I'm very glad to see this functionality. Thank you very much for it.
> > 
> > As described in guide about statistical facet:  
> > The statistical data include count, total, sum of squares, mean  
> > (average), minimum, maximum, variance, and standard deviation.  
> > Will terms stats facet include them also? I see total and count only  
> > now. And my usecase described here:  
> > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_thread/thread/6e524daeb77d7060/d518c70310eb0405](http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/6e524daeb77d7060/d518c70310eb0405)  
> > will be easily resolved with min values in terms stats facets.
> > 
> > Regards  
> > Wojciech DurczyÅski

---

<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 8, 2011, 6:43pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/6 "2011-03-08T18:43:10Z")

</div>

Could you please post an example how to query min/max value of a  
particular field? I was not able to find it in the current  
documentation. How do I use Java to do this task?

Thanks,  
Eugene.

On Feb 23, 10:37 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> ok, pushed support for it. Min and max are in. if sum of squares is also required, it can be added as well (though every addition does come at a cost).
> 
> On Wednesday, February 23, 2011 at 7:29 PM, Shay Banon wrote:
> 
> > Right, it does not include the min/max, open an issue and I will add it 🙂  
> > On Wednesday, February 23, 2011 at 4:51 PM, Wojciech Durczyński wrote:
> > 
> > > I'm very glad to see this functionality. Thank you very much for it.
> 
> > > As described in guide about statistical facet:  
> > > The statistical data include count, total, sum of squares, mean  
> > > (average), minimum, maximum, variance, and standard deviation.  
> > > Will terms stats facet include them also? I see total and count only  
> > > now. And my usecase described here:  
> > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_threa](http://groups.google.com/a/elasticsearch.com/group/users/browse_threa)...  
> > > will be easily resolved with min values in terms stats facets.
> 
> > > Regards  
> > > Wojciech Durczyński- 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 8, 2011, 7:08pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/7 "2011-03-08T19:08:06Z")

</div>

For min max of a field, use the statistical facet. In the Java API, call addFacet on the SearchRequestBuilder you get from prepareSearch, and add a FacetBuilders#statisticalFacet.  
On Tuesday, March 8, 2011 at 8:43 PM, eugene wrote:

> Could you please post an example how to query min/max value of a  
> particular field? I was not able to find it in the current  
> documentation. How do I use Java to do this task?
> 
> Thanks,  
> Eugene.
> 
> On Feb 23, 10:37 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > ok, pushed support for it. Min and max are in. if sum of squares is also required, it can be added as well (though every addition does come at a cost).
> > 
> > On Wednesday, February 23, 2011 at 7:29 PM, Shay Banon wrote:
> > 
> > > Right, it does not include the min/max, open an issue and I will add it 🙂  
> > > On Wednesday, February 23, 2011 at 4:51 PM, Wojciech DurczyÅski wrote:
> > > 
> > > > I'm very glad to see this functionality. Thank you very much for it.
> > 
> > > > As described in guide about statistical facet:  
> > > > The statistical data include count, total, sum of squares, mean  
> > > > (average), minimum, maximum, variance, and standard deviation.  
> > > > Will terms stats facet include them also? I see total and count only  
> > > > now. And my usecase described here:  
> > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_threa](http://groups.google.com/a/elasticsearch.com/group/users/browse_threa)...  
> > > > will be easily resolved with min values in terms stats facets.
> > 
> > > > Regards  
> > > > Wojciech DurczyÅski- 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 9, 2011, 8:22am UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/8 "2011-03-09T08:22:52Z")

</div>

Thank you, Shay. Your suggestion was very helpful. Let me describe  
the entire problem. I am trying to pull document under some index with  
largest '\_id' value. I can set facet on any field, but it didn't work  
on \_id. Do you know if there is a way to retrieve doc with maximum  
\_id?

On Mar 8, 11:08 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> For min max of a field, use the statistical facet. In the Java API, call addFacet on the SearchRequestBuilder you get from prepareSearch, and add a FacetBuilders#statisticalFacet.
> 
> On Tuesday, March 8, 2011 at 8:43 PM, eugene wrote:
> 
> > Could you please post an example how to query min/max value of a  
> > particular field? I was not able to find it in the current  
> > documentation. How do I use Java to do this task?
> 
> > Thanks,  
> > Eugene.
> 
> > On Feb 23, 10:37 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > ok, pushed support for it. Min and max are in. if sum of squares is also required, it can be added as well (though every addition does come at a cost).
> 
> > > On Wednesday, February 23, 2011 at 7:29 PM, Shay Banon wrote:
> > > 
> > > > Right, it does not include the min/max, open an issue and I will add it 🙂  
> > > > On Wednesday, February 23, 2011 at 4:51 PM, Wojciech Durczyński wrote:
> > > > 
> > > > > I'm very glad to see this functionality. Thank you very much for it.
> 
> > > > > As described in guide about statistical facet:  
> > > > > The statistical data include count, total, sum of squares, mean  
> > > > > (average), minimum, maximum, variance, and standard deviation.  
> > > > > Will terms stats facet include them also? I see total and count only  
> > > > > now. And my usecase described here:  
> > > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_threa](http://groups.google.com/a/elasticsearch.com/group/users/browse_threa)...  
> > > > > will be easily resolved with min values in terms stats facets.
> 
> > > > > Regards  
> > > > > Wojciech Durczyński- Hide quoted text -
> 
> > > - Show quoted text -- Hide quoted text -
> 
> - Show quoted text -

---

<div class="post-metadata">

**Author:** ![K\_B](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@K\_B](https://discuss.elastic.co/u/K_B)\
**Post date:** [March 9, 2011, 10:09am UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/9 "2011-03-09T10:09:05Z")

</div>

Eugene, have you thought about just using the "\_id" field as the field  
you let your search query be sorted with? In case you take this as  
primary sort option the one on the top is either the smallest or the  
largest \_id (Descending/ Ascending sort)

Best

On 9 Mrz., 09:22, eugene [efur...@gmail.com](mailto:efur...@gmail.com) wrote:

> Thank you, Shay. Your suggestion was very helpful. Let me describe  
> the entire problem. I am trying to pull document under some index with  
> largest '\_id' value. I can set facet on any field, but it didn't work  
> on \_id. Do you know if there is a way to retrieve doc with maximum  
> \_id?
> 
> On Mar 8, 11:08 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > For min max of a field, use the statistical facet. In the Java API, call addFacet on the SearchRequestBuilder you get from prepareSearch, and add a FacetBuilders#statisticalFacet.
> 
> > On Tuesday, March 8, 2011 at 8:43 PM, eugene wrote:
> 
> > > Could you please post an example how to query min/max value of a  
> > > particular field? I was not able to find it in the current  
> > > documentation. How do I use Java to do this task?
> 
> > > Thanks,  
> > > Eugene.
> 
> > > On Feb 23, 10:37 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > > 
> > > > ok, pushed support for it. Min and max are in. if sum of squares is also required, it can be added as well (though every addition does come at a cost).
> 
> > > > On Wednesday, February 23, 2011 at 7:29 PM, Shay Banon wrote:
> > > > 
> > > > > Right, it does not include the min/max, open an issue and I will add it 🙂  
> > > > > On Wednesday, February 23, 2011 at 4:51 PM, Wojciech Durczyński wrote:
> > > > > 
> > > > > > I'm very glad to see this functionality. Thank you very much for it.
> 
> > > > > > As described in guide about statistical facet:  
> > > > > > The statistical data include count, total, sum of squares, mean  
> > > > > > (average), minimum, maximum, variance, and standard deviation.  
> > > > > > Will terms stats facet include them also? I see total and count only  
> > > > > > now. And my usecase described here:  
> > > > > > [http://groups.google.com/a/elasticsearch.com/group/users/browse\_threa](http://groups.google.com/a/elasticsearch.com/group/users/browse_threa)...  
> > > > > > will be easily resolved with min values in terms stats facets.
> 
> > > > > > Regards  
> > > > > > Wojciech Durczyński- Hide quoted text -
> 
> > > > - Show quoted text -- Hide quoted text -
> 
> > - Show quoted text -

---

<div class="post-metadata">

**Author:** ![Paul\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_smith/32/1323_2.png) [@Paul\_Smith](https://discuss.elastic.co/u/Paul_Smith)\
**Post date:** [March 9, 2011, 10:45pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/10 "2011-03-09T22:45:14Z")

</div>

On 9 March 2011 21:09, K.B. [korbinian.bachl@googlemail.com](mailto:korbinian.bachl@googlemail.com) wrote:

> Eugene, have you thought about just using the "\_id" field as the field  
> you let your search query be sorted with? In case you take this as  
> primary sort option the one on the top is either the smallest or the  
> largest \_id (Descending/ Ascending sort)

just be awfully careful using sorting on such a unique field like \_id. This  
will scale badly in terms of memory because sorting costs in lucene is a  
function of the # unique terms. This will work fine in a small sample set  
but will be armageddon in a large production setup.

wouldn't recommend that.

Paul

---

<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 10, 2011, 2:55am UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/11 "2011-03-10T02:55:01Z")

</div>

Thanks Paul. I actually ended up not using auto-incremented \_id and  
checking the largest value of \_id each time I index a new document, I  
didn't specify it and the system now generates it automatically.

On Mar 9, 2:45 pm, Paul Smith [tallpsm...@gmail.com](mailto:tallpsm...@gmail.com) wrote:

> On 9 March 2011 21:09, K.B. [korbinian.ba...@googlemail.com](mailto:korbinian.ba...@googlemail.com) wrote:
> 
> > Eugene, have you thought about just using the "\_id" field as the field  
> > you let your search query be sorted with? In case you take this as  
> > primary sort option the one on the top is either the smallest or the  
> > largest \_id (Descending/ Ascending sort)
> 
> just be awfully careful using sorting on such a unique field like \_id. This  
> will scale badly in terms of memory because sorting costs in lucene is a  
> function of the # unique terms. This will work fine in a small sample set  
> but will be armageddon in a large production setup.
> 
> wouldn't recommend that.
> 
> Paul

---

<div class="post-metadata">

**Author:** ![itthuyloi](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@itthuyloi](https://discuss.elastic.co/u/itthuyloi)\
**Post date:** [November 2, 2012, 8:56am UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/12 "2012-11-02T08:56:27Z")

</div>

In page: [http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html)  
  
"Note, the terms stats can work with multi valued key fields, or multi valued value fields, but not when both are multi valued (as ordering is not maintained)."  
  
But how to build the query, I want to statistic of "hoursWorked" of a "Task log" have for {groupId and projectId}, please tell me!

I tried:

```

{
	"query": {
		"match_all": {
			
		}
	},
	"facets": {
		"hoursWorked_stats": {
			"terms_stats": {
				"key_field": ["groupId",
				"projectId"],
				"value_field": "hoursWorked",
				"size": 1000
			}
		}
	}
}

```

But it's not working!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 6, 2012, 10:11pm UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/13 "2012-11-06T22:11:16Z")

</div>

Terms Stats facet can indeed work with multi valued key fields, which means  
that it can work with documents that have more than one value in the the  
key\_field. It doesn't work with multiple key fields though. I would try  
adding a special field groupIdProjectId that would contain  
concatenated group ID and project ID in a single field and use this field  
as key\_field.

On Friday, November 2, 2012 4:56:27 AM UTC-4, Nguyen Van De wrote:

> In page:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html)
> 
> "Note, the terms stats can work with multi valued key fields, or multi  
> valued value fields, but not when both are multi valued (as ordering is not  
> maintained)."
> 
> But how to build the query, I want to statistic of "hoursWorked" of a  
> "Task log" have for {groupId and projectId}, please tell me! I tried:
> 
> {  
> "query": {  
> "match\_all": {
> 
> ```
> }
> 
> ```
> 
> },  
> "facets": {  
> "hoursWorked\_stats": {  
> "terms\_stats": {  
> "key\_field": ["groupId",  
> "projectId"],  
> "value\_field": "hoursWorked",  
> "size": 1000  
> }  
> }  
> }  
> }
> 
> ## But it's not working!
> 
> View this message in context: Re: Just Pushed: Terms Stats Facets[http://elasticsearch-users.115913.n3.nabble.com/Just-Pushed-Terms-Stats-Facets-tp2535898p4024723.html](http://elasticsearch-users.115913.n3.nabble.com/Just-Pushed-Terms-Stats-Facets-tp2535898p4024723.html)  
> Sent from the Elasticsearch Users mailing list archive[http://elasticsearch-users.115913.n3.nabble.com/](http://elasticsearch-users.115913.n3.nabble.com/)at [Nabble.com](http://Nabble.com).

--

---

<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:05am UTC](https://discuss.elastic.co/t/just-pushed-terms-stats-facets/3952/14 "2017-07-06T03:05:51Z")

</div>


