# Limit number of documents returned by each clause in Search query

**URL:** https://discuss.elastic.co/t/limit-number-of-documents-returned-by-each-clause-in-search-query/198801
**Category:** Elasticsearch
**Created:** [September 9, 2019, 10:54pm UTC](https://discuss.elastic.co/t/limit-number-of-documents-returned-by-each-clause-in-search-query/198801 "2019-09-09T22:54:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![indiekid](https://avatars.discourse-cdn.com/v4/letter/i/65b543/32.png) [@indiekid](https://discuss.elastic.co/u/indiekid)
#### Post date: [September 9, 2019, 10:54pm UTC](https://discuss.elastic.co/t/limit-number-of-documents-returned-by-each-clause-in-search-query/198801/1 "2019-09-09T22:54:29Z")

</div>

I have a category field in my documents, which is mapped as a keyword. I am trying to run a search query that fetches the latest _n_ documents for each category specified in the query.

I am able to achieve this using a multi-search, with each term search looking for the latest _n_ documents for a particular category, but I was wondering if it possible to achieve using a single search query.

I have drafted a bool query that illustrates this problem:

```
curl -X GET "http://localhost:9200/my_index_2019-09-08,my_index_2019-09-07/_search?pretty" -H 'Content-Type: application/json' -d '
{
    "_source": ["category", "field1", "field2", "field3"],
    "query": {
        "bool": {
            "should": [
                {"term": {"category": "green"}},
                {"term": {"category": "blue"}}
            ],
            "minimum_should_match" : 1
        }
    },
    "sort": [{"field3": "desc"}],
    "from": 0,
    "size": 10
}
'

```

This query fetches the latest 10 documents that match either category. Ideally, I would like to be able to limit the number of matches for each category, if possible.

Elasticsearch version: 6.5.4

---

<div class="post-metadata">

### Author: ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)
#### Post date: [September 10, 2019, 5:04am UTC](https://discuss.elastic.co/t/limit-number-of-documents-returned-by-each-clause-in-search-query/198801/2 "2019-09-10T05:04:53Z")

</div>

See [Top Hits Aggs](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-metrics-top-hits-aggregation.html)

---

<div class="post-metadata">

### Author: ![indiekid](https://avatars.discourse-cdn.com/v4/letter/i/65b543/32.png) [@indiekid](https://discuss.elastic.co/u/indiekid)
#### Post date: [September 10, 2019, 7:55am UTC](https://discuss.elastic.co/t/limit-number-of-documents-returned-by-each-clause-in-search-query/198801/3 "2019-09-10T07:55:42Z")

</div>

It worked really well, thanks! I was able to rewrite my query in the following manner:

```
curl -X POST "http://localhost:9200/my_index_2019-09-08,my_index_2019-09-07/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
    "aggs": {
        "top_tags": {
            "terms": {
                "field": "category",
                "include": ["green", "blue"]
            },
            "aggs": {
                "category": {
                    "top_hits": {
                        "sort": [{"field3": "desc"}],
                        "_source": {
                            "includes": [
                                "field1", "field2", "field3", "category"
                            ]
                        },
                        "size" : 1
                    }
                }
            }
        }
    }
}
'
```

---

<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 8, 2019, 7:55am UTC](https://discuss.elastic.co/t/limit-number-of-documents-returned-by-each-clause-in-search-query/198801/4 "2019-10-08T07:55:43Z")

</div>

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