# Multiple Count queries

**URL:** <https://discuss.elastic.co/t/multiple-count-queries/6589>\
**Category:** Elasticsearch\
**Created:** [February 3, 2012, 9:00pm UTC](https://discuss.elastic.co/t/multiple-count-queries/6589 "2012-02-03T21:00:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)\
**Post date:** [February 3, 2012, 9:00pm UTC](https://discuss.elastic.co/t/multiple-count-queries/6589/1 "2012-02-03T21:00:11Z")

</div>

If I need to find counts of user's posts, comments, etc... and each  
such item is a separate type with userId field, can I get such  
information using just one query?  
Because currently, I need to run several requests for each type  
(posts, comments, etc.).  
Something like this:

public long countPostsByUserId(String userId)throws IOException{  
FilteredQueryBuilder q = QueryBuilders.filteredQuery(  
QueryBuilders.matchAllQuery(),  
FilterBuilders.termFilter("userId", userId)  
);  
SearchResponse response = clientProvider.getClient()  
.prepareSearch("myIndex")  
.setSearchType(SearchType.COUNT).setQuery(q).setTypes("post")  
.execute().actionGet();  
return response.getHits().getTotalHits();  
}

And if I have many such types, this become a bottleneck. Specially if  
I need to find such counts for many users at once.

Please advice. Any info is appreciated.

Thank you,  
Eugene S.

---

<div class="post-metadata">

**Author:** ![Greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Greg](https://discuss.elastic.co/u/Greg)\
**Post date:** [February 4, 2012, 11:57am UTC](https://discuss.elastic.co/t/multiple-count-queries/6589/2 "2012-02-04T11:57:21Z")

</div>

You can do this with the Count API: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count.html)

I'm not familiar with the Java API, but with the REST API you could do  
this:

$ curl -XGET '[http://localhost:9200/user/post,comment/\_count](http://localhost:9200/user/post,comment/_count)' -d '  
{  
"term" : { "userId" : "foo" }  
}'

On Feb 3, 9:00 pm, Eugene Strokin [eug...@strokin.info](mailto:eug...@strokin.info) wrote:

> If I need to find counts of user's posts, comments, etc... and each  
> such item is a separate type with userId field, can I get such  
> information using just one query?  
> Because currently, I need to run several requests for each type  
> (posts, comments, etc.).  
> Something like this:
> 
> public long countPostsByUserId(String userId)throws IOException{  
> FilteredQueryBuilder q = QueryBuilders.filteredQuery(  
> QueryBuilders.matchAllQuery(),  
> FilterBuilders.termFilter("userId", userId)  
> );  
> SearchResponse response = clientProvider.getClient()  
> .prepareSearch("myIndex")  
> .setSearchType(SearchType.COUNT).setQuery(q).setTypes("post")  
> .execute().actionGet();  
> return response.getHits().getTotalHits();
> 
> }
> 
> And if I have many such types, this become a bottleneck. Specially if  
> I need to find such counts for many users at once.
> 
> Please advice. Any info is appreciated.
> 
> Thank you,  
> Eugene S.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 5, 2012, 4:35pm UTC](https://discuss.elastic.co/t/multiple-count-queries/6589/3 "2012-02-05T16:35:08Z")

</div>

What are you after? Counts of type per userId? You can use a match all query, and have global scoped facets of type filter that would return counts per filter you have (which can use term filter \_type, term filter on userId, or a bool filter on both).

On Friday, February 3, 2012 at 11:00 PM, Eugene Strokin wrote:

> If I need to find counts of user's posts, comments, etc... and each  
> such item is a separate type with userId field, can I get such  
> information using just one query?  
> Because currently, I need to run several requests for each type  
> (posts, comments, etc.).  
> Something like this:
> 
> public long countPostsByUserId(String userId)throws IOException{  
> FilteredQueryBuilder q = QueryBuilders.filteredQuery(  
> QueryBuilders.matchAllQuery(),  
> FilterBuilders.termFilter("userId", userId)  
> );  
> SearchResponse response = clientProvider.getClient()  
> .prepareSearch("myIndex")  
> .setSearchType(SearchType.COUNT).setQuery(q).setTypes("post")  
> .execute().actionGet();  
> return response.getHits().getTotalHits();  
> }
> 
> And if I have many such types, this become a bottleneck. Specially if  
> I need to find such counts for many users at once.
> 
> Please advice. Any info is appreciated.
> 
> Thank you,  
> Eugene S.

---

<div class="post-metadata">

**Author:** ![Vyasa](https://avatars.discourse-cdn.com/v4/letter/v/4af34b/32.png) [@Vyasa](https://discuss.elastic.co/u/Vyasa)\
**Post date:** [November 6, 2013, 6:15pm UTC](https://discuss.elastic.co/t/multiple-count-queries/6589/4 "2013-11-06T18:15:13Z")

</div>

@kimchy : I have similar problem, in one of my projects, I have a field  
called 'userId' in three index types, I need to write a facet query for  
getting counts of a particular userId in specific index type. Can you give  
an example  
for the query you are suggesting?

On Sunday, February 5, 2012 10:05:08 PM UTC+5:30, kimchy wrote:

> What are you after? Counts of type per userId? You can use a match all  
> query, and have global scoped facets of type filter that would return  
> counts per filter you have (which can use term filter \_type, term filter on  
> userId, or a bool filter on both).
> 
> On Friday, February 3, 2012 at 11:00 PM, Eugene Strokin wrote:
> 
> If I need to find counts of user's posts, comments, etc... and each  
> such item is a separate type with userId field, can I get such  
> information using just one query?  
> Because currently, I need to run several requests for each type  
> (posts, comments, etc.).  
> Something like this:
> 
> public long countPostsByUserId(String userId)throws IOException{  
> FilteredQueryBuilder q = QueryBuilders.filteredQuery(  
> QueryBuilders.matchAllQuery(),  
> FilterBuilders.termFilter("userId", userId)  
> );  
> SearchResponse response = clientProvider.getClient()  
> .prepareSearch("myIndex")  
> .setSearchType(SearchType.COUNT).setQuery(q).setTypes("post")  
> .execute().actionGet();  
> return response.getHits().getTotalHits();  
> }
> 
> And if I have many such types, this become a bottleneck. Specially if  
> I need to find such counts for many users at once.
> 
> Please advice. Any info is appreciated.
> 
> Thank you,  
> Eugene S.

--  
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:08am UTC](https://discuss.elastic.co/t/multiple-count-queries/6589/5 "2017-07-06T02:08:30Z")

</div>


