# Elasticsearch to recommand book authors: how to limit maximum 3 books per author?

**URL:** <https://discuss.elastic.co/t/elasticsearch-to-recommand-book-authors-how-to-limit-maximum-3-books-per-author/20277>\
**Category:** Elasticsearch\
**Created:** [October 16, 2014, 8:36am UTC](https://discuss.elastic.co/t/elasticsearch-to-recommand-book-authors-how-to-limit-maximum-3-books-per-author/20277 "2014-10-16T08:36:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renaud\_Richardet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/renaud_richardet/32/1199_2.png) [@Renaud\_Richardet](https://discuss.elastic.co/u/Renaud_Richardet)\
**Post date:** [October 16, 2014, 8:36am UTC](https://discuss.elastic.co/t/elasticsearch-to-recommand-book-authors-how-to-limit-maximum-3-books-per-author/20277/1 "2014-10-16T08:36:31Z")

</div>

[http://stackoverflow.com/questions/26360859/elasticsearch-to-recommand-book-authors-how-to-limit-maximum-3-books-per-author#](http://stackoverflow.com/questions/26360859/elasticsearch-to-recommand-book-authors-how-to-limit-maximum-3-books-per-author#)

I use Elasticsearch to recommend authors (my Elasticsearch documents  
represent books, with a title, a summary and a list of author ids).

The user queries my index with some text (e.g. Georgia or Paris) and I need  
to aggregate the score of individual books at the author level (meaning:  
recommand an author that writes about Paris).

I began with a simple aggregation, however, experimentally  
(cross-validation) it is best to stop aggregating the score of a user after  
maximum 4 books. Let me explain in pseudocode:

# the aggregated score of each author

Map\<Author, Double\> author\_scores = new Map()

# the number of books (hits) that contributed to each author

Map\<Author, Integer\> author\_cnt = new Map()

# iterate ES query results

for Document doc in hits:

```
# stop aggregating if more that 4 books from this author have already been found
if (author_cnt.get(doc.author_id) < 4):
    author_scores.increment_by(doc.author_id, doc.score)
    author_cnt.increment_by(doc.author_id, 1)

```

the\_result = author\_scores.sort\_map\_by\_value(reverse=true)

So far, I have implemented the above aggregation in custom application  
code, but I was wondering if it was possible to rewrite it using  
Elasticsearch's query DSL or  
org.elasticsearch.search.aggregations.Aggregator interface.

(crossposted from SO: [http://stackoverflow.com/q/26360859/125617](http://stackoverflow.com/q/26360859/125617))

--  
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/7b6155ee-348b-4b4e-a910-3b30ff5c64bd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7b6155ee-348b-4b4e-a910-3b30ff5c64bd%40googlegroups.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, 12:55am UTC](https://discuss.elastic.co/t/elasticsearch-to-recommand-book-authors-how-to-limit-maximum-3-books-per-author/20277/2 "2017-07-06T00:55:50Z")

</div>


