# Limit number of results for each value of one field

**URL:** https://discuss.elastic.co/t/limit-number-of-results-for-each-value-of-one-field/29146
**Category:** Elasticsearch
**Created:** [September 11, 2015, 3:23pm UTC](https://discuss.elastic.co/t/limit-number-of-results-for-each-value-of-one-field/29146 "2015-09-11T15:23:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Christophe\_Rosko](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Christophe\_Rosko](https://discuss.elastic.co/u/Christophe_Rosko)
#### Post date: [September 11, 2015, 3:23pm UTC](https://discuss.elastic.co/t/limit-number-of-results-for-each-value-of-one-field/29146/1 "2015-09-11T15:23:45Z")

</div>

Hi !

I'm currently using filtered queries, and I need to get only the best 2 results for each value of field "type". For example, if my documents are :  
{id: 1; type: 1; value:1}  
{id: 2; type: 1; value:2}  
{id: 3; type: 2; value:3}  
{id: 4; type: 2; value:4}  
{id: 5; type: 2; value:5}  
{id: 6; type: 3; value:6}  
{id: 7; type: 1; value:7}

When I'm querying and sorting by value desc, I want the results to be :  
{id: 7; type: 1; value:7}  
{id: 6; type: 3; value:6}  
{id: 5; type: 2; value:5}  
{id: 4; type: 2; value:4}  
{id: 2; type: 1; value:2}

I tried to use top\_hits aggregation but I couldn't get what I want.  
Is there a way to do something like that ?

Thanks for your help 😄

---

<div class="post-metadata">

### Author: ![Christophe\_Rosko](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Christophe\_Rosko](https://discuss.elastic.co/u/Christophe_Rosko)
#### Post date: [September 21, 2015, 10:54am UTC](https://discuss.elastic.co/t/limit-number-of-results-for-each-value-of-one-field/29146/2 "2015-09-21T10:54:14Z")

</div>

Up ! 🙂

---

<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:49pm UTC](https://discuss.elastic.co/t/limit-number-of-results-for-each-value-of-one-field/29146/3 "2017-07-05T23:49:05Z")

</div>


