# Getting hit count before post-filter

**URL:** <https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791>\
**Category:** Elasticsearch\
**Created:** [January 9, 2016, 5:58pm UTC](https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791 "2016-01-09T17:58:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cosmin\_Marginean](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cosmin_marginean/32/1735_2.png) [@Cosmin\_Marginean](https://discuss.elastic.co/u/Cosmin_Marginean)\
**Post date:** [January 9, 2016, 5:58pm UTC](https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791/1 "2016-01-09T17:58:05Z")

</div>

Hi

I'm applying a post-filter (in order to get aggregations only on the original query scope). There is however a need to find/display the hit count _before_ this post-filter was applied (otherwise we have to cache hits/hits/total which is not entirely portable for what we do)

I can obviously do something silly like

```auto
ValueCountBuilder agg = AggregationBuilders.count("hitsPrePostFilter").field("_uid");
request = request.addAggregation(agg);

```

but that's not ideal as it incurs unnecessary processing.

So I was wondering if there's a mechanism to get, in the same request, the hit count for the core/original query scope, before the post-filter was applied.

Thank you  
Cos

---

<div class="post-metadata">

**Author:** ![Cosmin\_Marginean](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cosmin_marginean/32/1735_2.png) [@Cosmin\_Marginean](https://discuss.elastic.co/u/Cosmin_Marginean)\
**Post date:** [January 19, 2016, 2:19pm UTC](https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791/2 "2016-01-19T14:19:34Z")

</div>

Judging by the lack of activity on this one, it seems that a "fake aggregation" is the way to go here. Has anyone else encountered this use case?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [January 19, 2016, 2:57pm UTC](https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791/3 "2016-01-19T14:57:22Z")

</div>

A fake aggregation would work indeed, but I would recommend running a filter aggregation on a `match_all` query rather than a value\_count aggregation.

---

<div class="post-metadata">

**Author:** ![Cosmin\_Marginean](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cosmin_marginean/32/1735_2.png) [@Cosmin\_Marginean](https://discuss.elastic.co/u/Cosmin_Marginean)\
**Post date:** [January 19, 2016, 3:32pm UTC](https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791/4 "2016-01-19T15:32:44Z")

</div>

Good point, 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:23pm UTC](https://discuss.elastic.co/t/getting-hit-count-before-post-filter/38791/5 "2017-07-05T23:23:25Z")

</div>


