# How to find the number of authors who have written between 2-3 books?

**URL:** <https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222>\
**Category:** Elasticsearch\
**Created:** [June 19, 2014, 10:34pm UTC](https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222 "2014-06-19T22:34:53Z")\
**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:** [June 19, 2014, 10:34pm UTC](https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222/1 "2014-06-19T22:34:53Z")

</div>

Assume each document is a book:  
{ title: "A", author: "Mike" }  
{ title: "B", author: "Mike" }  
{ title: "C", author: "Mike" }  
{ title: "D", author: "Mike" }

{ title: "E", author: "John" }  
{ title: "F", author: "John" }  
{ title: "G", author: "John" }

{ title: "H", author: "Joe" }  
{ title: "I", author: "Joe" }

{ title: "J", author: "Jack" }

What is the best way to fin the number of authors who have written between  
2-3 books? In this case it would be 2, John and Joe.

I know I can do a terms aggregation on author, set size to be very very  
large, and then on the client side traverse through the thousands of  
authors and count how many had between 2-3. Is there a more efficient way  
to do this? The cardinality aggregation is almost what I want, if only I  
could specify a min and max term count.

--  
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/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [June 19, 2014, 10:43pm UTC](https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222/2 "2014-06-19T22:43:34Z")

</div>

This is a Map/Reduce operation, you'll be better off maintaining a  
ref-count document IMO then trying to hack the aggregations framework to  
support this

Another reason for doing it that way is in a distributed environment some  
aggregations can't be computed to an exact value - the Terms bucketing is  
one example. So if you need exact values, I'd go for a model that does it.

--

Itamar Syn-Hershko  
[http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
Freelance Developer & Consultant  
Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)

On Fri, Jun 20, 2014 at 1:34 AM, Mike [mnilsson2323@gmail.com](mailto:mnilsson2323@gmail.com) wrote:

> Assume each document is a book:  
> { title: "A", author: "Mike" }  
> { title: "B", author: "Mike" }  
> { title: "C", author: "Mike" }  
> { title: "D", author: "Mike" }
> 
> { title: "E", author: "John" }  
> { title: "F", author: "John" }  
> { title: "G", author: "John" }
> 
> { title: "H", author: "Joe" }  
> { title: "I", author: "Joe" }
> 
> { title: "J", author: "Jack" }
> 
> What is the best way to fin the number of authors who have written between  
> 2-3 books? In this case it would be 2, John and Joe.
> 
> I know I can do a terms aggregation on author, set size to be very very  
> large, and then on the client side traverse through the thousands of  
> authors and count how many had between 2-3. Is there a more efficient way  
> to do this? The cardinality aggregation is almost what I want, if only I  
> could specify a min and max term count.
> 
> --  
> 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/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAHTr4Zv5%3DmuahwGVbGobX5SgMHYzC\_bD4udiZ3XTiAdU1v8YCg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4Zv5%3DmuahwGVbGobX5SgMHYzC_bD4udiZ3XTiAdU1v8YCg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [June 20, 2014, 3:15pm UTC](https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222/3 "2014-06-20T15:15:36Z")

</div>

I'm ok with the count returned being some estimate. Say in this simple  
example if it returned 1 for just Joe, or 3 for John, Joe, and Jack that  
would be ok too. I am also ok with restructuring my data in any way to  
more efficiently get this number.

You mentioned creating a reference count document. How would that look? 1  
doc per unique author, with a count of the total number of books he wrote  
so then I can do a range aggregation on that number? What if I wanted to  
find "the number of authors who have written between 2-3 books that have a  
title containing E, F, H, or I" (still 2 in this case, John and Joe) ?

On Thursday, June 19, 2014 6:43:41 PM UTC-4, Itamar Syn-Hershko wrote:

> This is a Map/Reduce operation, you'll be better off maintaining a  
> ref-count document IMO then trying to hack the aggregations framework to  
> support this
> 
> Another reason for doing it that way is in a distributed environment some  
> aggregations can't be computed to an exact value - the Terms bucketing is  
> one example. So if you need exact values, I'd go for a model that does it.
> 
> --
> 
> Itamar Syn-Hershko  
> [http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
> Freelance Developer & Consultant  
> Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)
> 
> On Fri, Jun 20, 2014 at 1:34 AM, Mike \<[mnilss...@gmail.com](mailto:mnilss...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Assume each document is a book:  
> > { title: "A", author: "Mike" }  
> > { title: "B", author: "Mike" }  
> > { title: "C", author: "Mike" }  
> > { title: "D", author: "Mike" }
> > 
> > { title: "E", author: "John" }  
> > { title: "F", author: "John" }  
> > { title: "G", author: "John" }
> > 
> > { title: "H", author: "Joe" }  
> > { title: "I", author: "Joe" }
> > 
> > { title: "J", author: "Jack" }
> > 
> > What is the best way to fin the number of authors who have written  
> > between 2-3 books? In this case it would be 2, John and Joe.
> > 
> > I know I can do a terms aggregation on author, set size to be very very  
> > large, and then on the client side traverse through the thousands of  
> > authors and count how many had between 2-3. Is there a more efficient way  
> > to do this? The cardinality aggregation is almost what I want, if only I  
> > could specify a min and max term count.
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/2cab8d84-7c65-4f6e-ab39-3e2a0e859a87%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2cab8d84-7c65-4f6e-ab39-3e2a0e859a87%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 20, 2014, 10:25pm UTC](https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222/4 "2014-06-20T22:25:44Z")

</div>

Alternatively, if you mode this with parent-child, then you can use  
min\_children/max\_children which is available in the next release

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

On 20 June 2014 17:15, Mike [mnilsson2323@gmail.com](mailto:mnilsson2323@gmail.com) wrote:

> I'm ok with the count returned being some estimate. Say in this simple  
> example if it returned 1 for just Joe, or 3 for John, Joe, and Jack that  
> would be ok too. I am also ok with restructuring my data in any way to  
> more efficiently get this number.
> 
> You mentioned creating a reference count document. How would that look?  
> 1 doc per unique author, with a count of the total number of books he  
> wrote so then I can do a range aggregation on that number? What if I  
> wanted to find "the number of authors who have written between 2-3 books  
> that have a title containing E, F, H, or I" (still 2 in this case, John and  
> Joe) ?
> 
> On Thursday, June 19, 2014 6:43:41 PM UTC-4, Itamar Syn-Hershko wrote:
> 
> > This is a Map/Reduce operation, you'll be better off maintaining a  
> > ref-count document IMO then trying to hack the aggregations framework to  
> > support this
> > 
> > Another reason for doing it that way is in a distributed environment some  
> > aggregations can't be computed to an exact value - the Terms bucketing is  
> > one example. So if you need exact values, I'd go for a model that does it.
> > 
> > --
> > 
> > Itamar Syn-Hershko  
> > [http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
> > Freelance Developer & Consultant  
> > Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)
> > 
> > On Fri, Jun 20, 2014 at 1:34 AM, Mike [mnilss...@gmail.com](mailto:mnilss...@gmail.com) wrote:
> > 
> > > Assume each document is a book:  
> > > { title: "A", author: "Mike" }  
> > > { title: "B", author: "Mike" }  
> > > { title: "C", author: "Mike" }  
> > > { title: "D", author: "Mike" }
> > > 
> > > { title: "E", author: "John" }  
> > > { title: "F", author: "John" }  
> > > { title: "G", author: "John" }
> > > 
> > > { title: "H", author: "Joe" }  
> > > { title: "I", author: "Joe" }
> > > 
> > > { title: "J", author: "Jack" }
> > > 
> > > What is the best way to fin the number of authors who have written  
> > > between 2-3 books? In this case it would be 2, John and Joe.
> > > 
> > > I know I can do a terms aggregation on author, set size to be very very  
> > > large, and then on the client side traverse through the thousands of  
> > > authors and count how many had between 2-3. Is there a more efficient way  
> > > to do this? The cardinality aggregation is almost what I want, if only I  
> > > could specify a min and max term count.
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%  
> > > [40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/22fc4e6d-bcac-426c-a343-ff1d36fc25de%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > 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/2cab8d84-7c65-4f6e-ab39-3e2a0e859a87%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2cab8d84-7c65-4f6e-ab39-3e2a0e859a87%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/2cab8d84-7c65-4f6e-ab39-3e2a0e859a87%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/2cab8d84-7c65-4f6e-ab39-3e2a0e859a87%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAPt3XKSyio7izuxr5UL4SD5uiA5J7rwtfyP742W3robxfk7s6A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPt3XKSyio7izuxr5UL4SD5uiA5J7rwtfyP742W3robxfk7s6A%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:20am UTC](https://discuss.elastic.co/t/how-to-find-the-number-of-authors-who-have-written-between-2-3-books/18222/5 "2017-07-06T01:20:49Z")

</div>


