# Aggregation only on hits returned

**URL:** <https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131>\
**Category:** Elasticsearch\
**Created:** [March 23, 2021, 6:38pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131 "2021-03-23T18:38:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidnei\_de\_souza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sidnei_de_souza/32/85981_2.png) [@sidnei\_de\_souza](https://discuss.elastic.co/u/sidnei_de_souza)\
**Post date:** [March 23, 2021, 6:38pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131/1 "2021-03-23T18:38:58Z")

</div>

First question here, new on Elasticsearch.  
Is it possible to do an aggregation on the hits returned on a search? I mean, if there are 8000 docs that match my query but I limit the size to 200, is it possible to do an aggregation using only the 200?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [March 23, 2021, 7:04pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131/2 "2021-03-23T19:04:07Z")

</div>

Hi Sidnei. See the [sampler](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html) aggregation

---

<div class="post-metadata">

**Author:** ![sidnei\_de\_souza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sidnei_de_souza/32/85981_2.png) [@sidnei\_de\_souza](https://discuss.elastic.co/u/sidnei_de_souza)\
**Post date:** [March 23, 2021, 7:19pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131/3 "2021-03-23T19:19:24Z")

</div>

Thanks a lot Mark. It worked perfectly. 😃

---

<div class="post-metadata">

**Author:** ![sidnei\_de\_souza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sidnei_de_souza/32/85981_2.png) [@sidnei\_de\_souza](https://discuss.elastic.co/u/sidnei_de_souza)\
**Post date:** [March 23, 2021, 8:25pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131/4 "2021-03-23T20:25:11Z")

</div>

Humm I think I was too hurried... in fact it seems that the sampler aggregation has nothing to do with the returned hits. If you use a fixed "size": 200 and change the "from": 0, 200, 400, the hits returned are differente sets, but the agg looks the same and does not reflect the returned hits.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [March 23, 2021, 8:53pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131/5 "2021-03-23T20:53:00Z")

</div>

Generally aggregations don’t follow the current page of results (1-10, 11-20 etc) because they are used to help searchers narrow matches using choices of prices, colours, brands etc from _all_ matches rather than just those visible on the current page.  
If you have a fuzzy query or one that allows partial matches with a common term then there can be a very long tail of low quality matches, with the worst case being a match on every doc in the index. In these cases it makes sense to use a sampler aggregation to focus the aggregations on only the most relevant results.

---

<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:** [April 20, 2021, 8:53pm UTC](https://discuss.elastic.co/t/aggregation-only-on-hits-returned/268131/6 "2021-04-20T20:53:47Z")

</div>

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