# How can I refine the query to return only number of hits

**URL:** <https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306>\
**Category:** Elasticsearch\
**Created:** [July 4, 2012, 5:08am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306 "2012-07-04T05:08:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curt\_Hu](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@Curt\_Hu](https://discuss.elastic.co/u/Curt_Hu)\
**Post date:** [July 4, 2012, 5:08am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/1 "2012-07-04T05:08:23Z")

</div>

I want to write a query to return only number of hits for a specific query,  
seemed that \_count API doesn't work. is there any solution for this?

---

<div class="post-metadata">

**Author:** ![Zhibin](https://avatars.discourse-cdn.com/v4/letter/z/6a8cbe/32.png) [@Zhibin](https://discuss.elastic.co/u/Zhibin)\
**Post date:** [July 4, 2012, 5:35am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/2 "2012-07-04T05:35:28Z")

</div>

Are you querying through REST API or JAVA API?

If using Java, On the SearchRequestBuilder  
-setSize(0)  
-setSearchType(SearchType.COUNT)

To get the number of hits, use response.hits().getTotalHits(), assuming  
response is your SearchResponse

Regards,  
Zhibin

On Wednesday, 4 July 2012 13:08:23 UTC+8, Curt wrote:

> I want to write a query to return only number of hits for a specific  
> query, seemed that \_count API doesn't work. is there any solution for this?

---

<div class="post-metadata">

**Author:** ![Curt\_Hu](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@Curt\_Hu](https://discuss.elastic.co/u/Curt_Hu)\
**Post date:** [July 4, 2012, 6:21am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/3 "2012-07-04T06:21:59Z")

</div>

Hi, Thanks for reply. How about using REST API?

On Wednesday, July 4, 2012 1:35:28 PM UTC+8, Zhibin wrote:

> Are you querying through REST API or JAVA API?
> 
> If using Java, On the SearchRequestBuilder  
> -setSize(0)  
> -setSearchType(SearchType.COUNT)
> 
> To get the number of hits, use response.hits().getTotalHits(), assuming  
> response is your SearchResponse
> 
> Regards,  
> Zhibin
> 
> On Wednesday, 4 July 2012 13:08:23 UTC+8, Curt wrote:
> 
> > I want to write a query to return only number of hits for a specific  
> > query, seemed that \_count API doesn't work. is there any solution for this?

---

<div class="post-metadata">

**Author:** ![Radu\_Gheorghe1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe1/32/2688_2.png) [@Radu\_Gheorghe1](https://discuss.elastic.co/u/Radu_Gheorghe1)\
**Post date:** [July 5, 2012, 7:55am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/4 "2012-07-05T07:55:04Z")

</div>

Hi Curt,

You need to add "size" : 0.

For example, this should work:  
$ curl -XPOST localhost:9200/\_search?pretty=true -d '{"query" : {  
"match\_all" : {}}, "size" : 0}'

On Wednesday, July 4, 2012 9:21:59 AM UTC+3, Curt wrote:

> Hi, Thanks for reply. How about using REST API?
> 
> On Wednesday, July 4, 2012 1:35:28 PM UTC+8, Zhibin wrote:
> 
> > Are you querying through REST API or JAVA API?
> > 
> > If using Java, On the SearchRequestBuilder  
> > -setSize(0)  
> > -setSearchType(SearchType.COUNT)
> > 
> > To get the number of hits, use response.hits().getTotalHits(), assuming  
> > response is your SearchResponse
> > 
> > Regards,  
> > Zhibin
> > 
> > On Wednesday, 4 July 2012 13:08:23 UTC+8, Curt wrote:
> > 
> > > I want to write a query to return only number of hits for a specific  
> > > query, seemed that \_count API doesn't work. is there any solution for this?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 5, 2012, 8:03am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/5 "2012-07-05T08:03:35Z")

</div>

Or with search\_type:

$ curl -XPOST 'localhost:9200/\_search?search\_type=count&pretty=true' -d '  
{"query" : { "match\_all" : {}}}  
'

clint

---

<div class="post-metadata">

**Author:** ![Shaun\_Farrell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaun_farrell/32/2827_2.png) [@Shaun\_Farrell](https://discuss.elastic.co/u/Shaun_Farrell)\
**Post date:** [July 5, 2012, 12:24pm UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/6 "2012-07-05T12:24:45Z")

</div>

You can do the Match\_All and then set the size to 0. They you will  
just have to grab the "total hits" variable from the result set.

Shaun Farrell

On Thu, Jul 5, 2012 at 4:03 AM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> Or with search\_type:
> 
> $ curl -XPOST 'localhost:9200/\_search?search\_type=count&pretty=true' -d '  
> {"query" : { "match\_all" : {}}}  
> '
> 
> clint

---

<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, 3:21am UTC](https://discuss.elastic.co/t/how-can-i-refine-the-query-to-return-only-number-of-hits/8306/7 "2017-07-06T03:21:24Z")

</div>


