# Getting top\_buckets(size=5) by using max(bucket.doc.\_score) + also getting the top\_document() on each bucket

**URL:** <https://discuss.elastic.co/t/getting-top-buckets-size-5-by-using-max-bucket-doc--score-also-getting-the-top-document-on-each-bucket/40856>\
**Category:** Elasticsearch\
**Created:** [February 3, 2016, 10:07am UTC](https://discuss.elastic.co/t/getting-top-buckets-size-5-by-using-max-bucket-doc--score-also-getting-the-top-document-on-each-bucket/40856 "2016-02-03T10:07:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [February 3, 2016, 10:07am UTC](https://discuss.elastic.co/t/getting-top-buckets-size-5-by-using-max-bucket-doc--score-also-getting-the-top-document-on-each-bucket/40856/1 "2016-02-03T10:07:12Z")

</div>

Hi,

I'm having trouble getting top\_buckets by a field "program\_id" and then getting the top document for each field and also ordering buckets by the document with the highest \_score.

Any ideas ?

So it should return 20 buckets ordered by the bucket's.top\_document.\_score and the 1 document for each bucket ordered by doc.\_score.

Thank You

---

<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 5, 2017, 11:19pm UTC](https://discuss.elastic.co/t/getting-top-buckets-size-5-by-using-max-bucket-doc--score-also-getting-the-top-document-on-each-bucket/40856/2 "2017-07-05T23:19:18Z")

</div>


