# Terminate after not working for elasticsearch count ? Count optimization issue

**URL:** <https://discuss.elastic.co/t/terminate-after-not-working-for-elasticsearch-count-count-optimization-issue/310016>\
**Category:** Elasticsearch\
**Created:** [July 19, 2022, 12:04pm UTC](https://discuss.elastic.co/t/terminate-after-not-working-for-elasticsearch-count-count-optimization-issue/310016 "2022-07-19T12:04:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehdiz](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mehdiz](https://discuss.elastic.co/u/mehdiz)\
**Post date:** [July 19, 2022, 12:04pm UTC](https://discuss.elastic.co/t/terminate-after-not-working-for-elasticsearch-count-count-optimization-issue/310016/1 "2022-07-19T12:04:02Z")

</div>

I want to have the total count of documents matching a specific query using the count query api of Elasticsearch on Python.

I'm using the count to have a check of maximum documents that we can fetch for one query, sometimes the query is big and it takes up to 1 min, which is too long, because it impacts the user experience.

I used `terminate_after`, thinking that it would stop the count it it hits the maximum, but it's not working that way, the count still returns the full count, which explains why it is taking so long.

I'm asking my self if the `terminate_after` is really efficient ? is there a way to tell ES to stock counting if we reach the maximum defined on terminate after, I checked the code behind the count on ES and seems using the same as search (at least on the pyhton api) but didn't go further.

---

<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:** [August 16, 2022, 12:04pm UTC](https://discuss.elastic.co/t/terminate-after-not-working-for-elasticsearch-count-count-optimization-issue/310016/2 "2022-08-16T12:04:46Z")

</div>

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