# Is there an exists API or something like that?

**URL:** <https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724>\
**Category:** Elasticsearch\
**Created:** [September 23, 2013, 1:22pm UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724 "2013-09-23T13:22:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_van\_der\_Weerd](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@Peter\_van\_der\_Weerd](https://discuss.elastic.co/u/Peter_van_der_Weerd)\
**Post date:** [September 23, 2013, 1:22pm UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/1 "2013-09-23T13:22:20Z")

</div>

Hi All,

I noticed that there is a count API, that is basically a query without  
scoring, but only count the #hits.

Now I need to know whether a query has results or not.  
Of course I can do that by doing a count and then test of 0 results, but  
that is probably not the most efficient approach.

Also the exists filter seems no to solve this problem.

Any idea's?

/P

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 23, 2013, 11:28pm UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/2 "2013-09-23T23:28:57Z")

</div>

Executing a query, plus the network latency, will be far greater than any  
time used on the client side to see the count is 0 or not. The query needs  
to be executed regardless to determine if it will return a result. I would  
simply use the count API.

--  
Ivan

On Mon, Sep 23, 2013 at 6:22 AM, Peter van der Weerd [pw2@bitmanager.nl](mailto:pw2@bitmanager.nl)wrote:

> Hi All,
> 
> I noticed that there is a count API, that is basically a query without  
> scoring, but only count the #hits.
> 
> Now I need to know whether a query has results or not.  
> Of course I can do that by doing a count and then test of 0 results, but  
> that is probably not the most efficient approach.
> 
> Also the exists filter seems no to solve this problem.
> 
> Any idea's?
> 
> /P
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [September 23, 2013, 11:44pm UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/3 "2013-09-23T23:44:23Z")

</div>

There is a search\_type "count" (not count API!) which is the fastest method  
to check for search results.

[http://www.elasticsearch.org/guide/reference/api/search/search-type/](http://www.elasticsearch.org/guide/reference/api/search/search-type/)

Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [September 24, 2013, 12:39am UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/4 "2013-09-24T00:39:36Z")

</div>

What about asking for one result and no fields? Is it still faster to count all the results?

Sent from my iPhone

> On Sep 23, 2013, at 7:44 PM, "[joergprante@gmail.com](mailto:joergprante@gmail.com)" [joergprante@gmail.com](mailto:joergprante@gmail.com) wrote:
> 
> There is a search\_type "count" (not count API!) which is the fastest method to check for search results.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/search-type/)
> 
> Jörg
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Boaz\_Leskes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boaz_leskes/32/723_2.png) [@Boaz\_Leskes](https://discuss.elastic.co/u/Boaz_Leskes)\
**Post date:** [September 24, 2013, 8:17am UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/5 "2013-09-24T08:17:32Z")

</div>

Hi Peter,

At the moment, there is no option to just check if query matches any  
documents other then using the count API or the count search type and  
checking #hits \> 0. In Lucene you could use early termination  
(CollectionTerminatedException) to stop enumerating docs when the first  
document is found - so we can do something there.

If the #hits\>0 is not fast enough for you, I suggest you open an issue,  
including and example of your use case, the timing involved and why they  
are not good enough for you.

Of course, you can also have some fun writing a plugin/a pull request with  
an extra endpoint (modelled after the Count API) and see how much that  
helps. Shouldn't be too hard and is a nice exercise 🙂

Cheers,  
Boaz

On Monday, September 23, 2013 3:22:20 PM UTC+2, Peter van der Weerd wrote:

> Hi All,
> 
> I noticed that there is a count API, that is basically a query without  
> scoring, but only count the #hits.
> 
> Now I need to know whether a query has results or not.  
> Of course I can do that by doing a count and then test of 0 results, but  
> that is probably not the most efficient approach.
> 
> Also the exists filter seems no to solve this problem.
> 
> Any idea's?
> 
> /P

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [September 24, 2013, 8:30am UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/6 "2013-09-24T08:30:13Z")

</div>

Even when a single document is requested, all matching documents will be  
collected in order to count the number of matches. The count API can be  
expected to be slightly faster than collecting a single document since  
Elasticsearch will be able to use a specialized collector that only counts  
matches and doesn't care about scoring or field values.

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:14am UTC](https://discuss.elastic.co/t/is-there-an-exists-api-or-something-like-that/13724/7 "2017-07-06T02:14:58Z")

</div>


