# How to get Minimum 4 results of each Term in my Query

**URL:** <https://discuss.elastic.co/t/how-to-get-minimum-4-results-of-each-term-in-my-query/102035>\
**Category:** Elasticsearch\
**Created:** [September 27, 2017, 9:47pm UTC](https://discuss.elastic.co/t/how-to-get-minimum-4-results-of-each-term-in-my-query/102035 "2017-09-27T21:47:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayshay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sayshay/32/22584_2.png) [@sayshay](https://discuss.elastic.co/u/sayshay)\
**Post date:** [September 27, 2017, 9:47pm UTC](https://discuss.elastic.co/t/how-to-get-minimum-4-results-of-each-term-in-my-query/102035/1 "2017-09-27T21:47:04Z")

</div>

Hi,

My mapping looks like this:

> address text  
> author text  
> category text  
> description text  
> type text  
> Where type can be a Collection, Content, Event, Tool.  
> How can I make sure query will return at least 4 of each tools, events, collections and contents.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 27, 2017, 10:00pm UTC](https://discuss.elastic.co/t/how-to-get-minimum-4-results-of-each-term-in-my-query/102035/2 "2017-09-27T22:00:31Z")

</div>

You can use a simple terms aggregation [1], with a filter aggregation [2]  
on the types requested and then select the top 4 inner hits [3].

[1]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)  
[2]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html)  
[3]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html)

---

<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:** [October 25, 2017, 10:00pm UTC](https://discuss.elastic.co/t/how-to-get-minimum-4-results-of-each-term-in-my-query/102035/3 "2017-10-25T22:00:49Z")

</div>

Closed on October 25, 2017, 10:00pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
