# Stop the search request once it finds first 10 hits

**URL:** <https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783>\
**Category:** Elasticsearch\
**Created:** [April 21, 2021, 4:55am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783 "2021-04-21T04:55:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DEEPTHI\_SONA\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepthi_sona_g/32/85930_2.png) [@DEEPTHI\_SONA\_G](https://discuss.elastic.co/u/DEEPTHI_SONA_G)\
**Post date:** [April 21, 2021, 4:55am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/1 "2021-04-21T04:55:32Z")

</div>

I am doing some performance testing on Elasticsearch. So I am comparing Oracle db with Elasticsearch.  
The Oracle query is able to stop it's search once it finds the first 10matches by using the code

```auto
select * from mtd_sec_data
WHERE current_sighting_status= 'ACT'
fetch first 10 rows only;

```

I want to do similar action in Elasticsearch too. Any help is appreciable.

---

<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:** [April 21, 2021, 7:17am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/2 "2021-04-21T07:17:25Z")

</div>

See the [terminate\_after](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-search.html#search-search-api-query-params) parameter.

---

<div class="post-metadata">

**Author:** ![DEEPTHI\_SONA\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepthi_sona_g/32/85930_2.png) [@DEEPTHI\_SONA\_G](https://discuss.elastic.co/u/DEEPTHI_SONA_G)\
**Post date:** [April 21, 2021, 8:24am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/3 "2021-04-21T08:24:32Z")

</div>

i have tried this query

```auto
GET /mtd_sec_data/_search?pretty&terminate_after=10
{ "query":{
      "match" : {
         "CURRENT_SIGHTING_STATUS": "ACT"
      }
   }
}

```

Even after testing the script it is having a similar throughput as in without using terminate\_after. Does this really stops searching once it finds the first 10hits or is it searching the entire index?

---

<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:** [April 21, 2021, 8:34am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/4 "2021-04-21T08:34:59Z")

</div>

How many total docs does the non-constrained search match?

It's also worth noting that queries now have [optimisations](https://www.elastic.co/blog/faster-retrieval-of-top-hits-in-elasticsearch-with-block-max-wand) for short-cutting non-competitive matches

---

<div class="post-metadata">

**Author:** ![DEEPTHI\_SONA\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepthi_sona_g/32/85930_2.png) [@DEEPTHI\_SONA\_G](https://discuss.elastic.co/u/DEEPTHI_SONA_G)\
**Post date:** [April 21, 2021, 8:39am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/5 "2021-04-21T08:39:22Z")

</div>

It has around 536 hits and the index contains 1000docs.

---

<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:** [April 21, 2021, 8:44am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/6 "2021-04-21T08:44:21Z")

</div>

> It has around 536 hits and the index contains 1000docs.

Probably not a great benchmark on that sort of volume. Most of the time spent will be in JSON parsing/rendering, returning those 10 docs source. The matching part which is searching for a single search term with only ~500 uses will be a small part of the overall cost.

---

<div class="post-metadata">

**Author:** ![DEEPTHI\_SONA\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepthi_sona_g/32/85930_2.png) [@DEEPTHI\_SONA\_G](https://discuss.elastic.co/u/DEEPTHI_SONA_G)\
**Post date:** [April 21, 2021, 8:53am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/7 "2021-04-21T08:53:43Z")

</div>

can we observe a clear difference by benchmarking on 1million data and above?

---

<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:** [April 21, 2021, 8:59am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/8 "2021-04-21T08:59:14Z")

</div>

> can we observe a clear difference by benchmarking on 1million data and above?

Perhaps clearer.

Benchmarking is a notoriously tricky thing what with things like caches playing a part (caches built into elasticsearch and file system caches).

We have a whole [benchmarking framework](https://github.com/elastic/rally) and related [forum topic](https://discuss.elastic.co/tags/c/elastic-stack/elasticsearch/rally)

---

<div class="post-metadata">

**Author:** ![DEEPTHI\_SONA\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepthi_sona_g/32/85930_2.png) [@DEEPTHI\_SONA\_G](https://discuss.elastic.co/u/DEEPTHI_SONA_G)\
**Post date:** [April 21, 2021, 9:02am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/9 "2021-04-21T09:02:35Z")

</div>

Thank you for giving an idea. It really gives some useful thoughts 🙂

---

<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:** [May 19, 2021, 9:02am UTC](https://discuss.elastic.co/t/stop-the-search-request-once-it-finds-first-10-hits/270783/10 "2021-05-19T09:02:41Z")

</div>

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