# How can I get the results of a terms facet bucketed into ranges by the counts?

**URL:** <https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846>\
**Category:** Elasticsearch\
**Created:** [August 24, 2012, 5:48pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846 "2012-08-24T17:48:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)\
**Post date:** [August 24, 2012, 5:48pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846/1 "2012-08-24T17:48:35Z")

</div>

Assume I have a couple books like this:

{ title:"One", author: "John" }  
{ title:"Two", author: "John" }  
{ title:"Three", author: "Alex" }  
{ title:"Four", author: "Alex" }  
{ title:"Five", author: "Mike" }  
{ title:"Six", author: "Mike" }  
{ title:"Seven", author: "Mike" }  
{ title:"Eight", author: "Dave" }

How can I get a grouping of authors by the number of books they wrote?

I can make a terms facet on author to get the top 4 most published authors  
and get:  
Mike : 3  
John : 2  
Alex : 2  
Dave : 1

But what if I want something like this:  
Written 6+ books : 0  
Written 2-5 books : 3  
Written 1 book : 1

--

---

<div class="post-metadata">

**Author:** ![Nick\_Dunn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_dunn/32/2315_2.png) [@Nick\_Dunn](https://discuss.elastic.co/u/Nick_Dunn)\
**Post date:** [August 25, 2012, 6:20am UTC](https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846/2 "2012-08-25T06:20:54Z")

</div>

As far as I know "group by" isn't supported. I think you'd need to convert your facet result to your desired range output in your own application logic.

--

---

<div class="post-metadata">

**Author:** ![Sawyer\_Zhu](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sawyer\_Zhu](https://discuss.elastic.co/u/Sawyer_Zhu)\
**Post date:** [August 26, 2012, 2:59pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846/3 "2012-08-26T14:59:58Z")

</div>

> You can use TermFacet to do this.

--

---

<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:** [August 26, 2012, 3:15pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846/4 "2012-08-26T15:15:10Z")

</div>

IMHO you are doing a count on Authors (books per author). You have to index Authors (and their books if needed).

Then, you can apply a RangeFacet on Authors.

My 2 cents.

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

Le 24 août 2012 à 19:48, Mike [mnilsson2323@gmail.com](mailto:mnilsson2323@gmail.com) a écrit :

> Assume I have a couple books like this:
> 
> { title:"One", author: "John" }  
> { title:"Two", author: "John" }  
> { title:"Three", author: "Alex" }  
> { title:"Four", author: "Alex" }  
> { title:"Five", author: "Mike" }  
> { title:"Six", author: "Mike" }  
> { title:"Seven", author: "Mike" }  
> { title:"Eight", author: "Dave" }
> 
> How can I get a grouping of authors by the number of books they wrote?
> 
> I can make a terms facet on author to get the top 4 most published authors and get:  
> Mike : 3  
> John : 2  
> Alex : 2  
> Dave : 1
> 
> But what if I want something like this:  
> Written 6+ books : 0  
> Written 2-5 books : 3  
> Written 1 book : 1
> 
> --

--

---

<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:15am UTC](https://discuss.elastic.co/t/how-can-i-get-the-results-of-a-terms-facet-bucketed-into-ranges-by-the-counts/8846/5 "2017-07-06T03:15:01Z")

</div>

Closed on July 6, 2017, 3:15am UTC


