Multiple Count queries

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 can do this with the Count API: Elasticsearch Platform — Find real-time answers at scale | Elastic

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' -d '
{
"term" : { "userId" : "foo" }
}'

On Feb 3, 9:00 pm, Eugene Strokin 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.

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.

@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.
For more options, visit https://groups.google.com/groups/opt_out.