# Count not working for Java API, works for REST

**URL:** <https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268>\
**Category:** Elasticsearch\
**Created:** [June 23, 2014, 6:19pm UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268 "2014-06-23T18:19:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Chambers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_chambers/32/607_2.png) [@Matt\_Chambers](https://discuss.elastic.co/u/Matt_Chambers)\
**Post date:** [June 23, 2014, 6:19pm UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268/1 "2014-06-23T18:19:01Z")

</div>

Kind of a strange situation.

I've tried 1.0.3, 1.1.2, and 1.2.1, all have the same problem (upgraded  
from .90.13)

My query is simple, looks like this:

{  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"and" : {  
"filters" : [ {  
"terms" : {  
"tags" : ["blah"]  
}  
} ]  
}  
}  
}

CountRequestBuilder builder = client.prepareCount(indexName)  
.setTypes("product")  
.setQuery(getQuery(req));

return builder.get().getCount();

I get a count of 0 with the Java API. If I take that request and hit the  
server with curl I get the real count.

Any help would be appreciated.

-Matt

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/706501e2-0413-49be-a7b3-7a1795dfdd69%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/706501e2-0413-49be-a7b3-7a1795dfdd69%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [June 23, 2014, 6:35pm UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268/2 "2014-06-23T18:35:32Z")

</div>

Perhaps you need to insert the execute().actionGet() method calls, as below?

CountRequestBuilder builder = client.prepareCount(indexName)  
.setTypes("product")  
.setQuery(getQuery(req));

_builder.execute().actionGet()_

return builder.get().getCount();

I don't use Count, but I have used Query and Update and Delete and they all  
work similarly in this regard. Just a guess.

Brian

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/efaf68b8-90f1-47c7-87c1-d124ae7c4bce%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/efaf68b8-90f1-47c7-87c1-d124ae7c4bce%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Matt\_Chambers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_chambers/32/607_2.png) [@Matt\_Chambers](https://discuss.elastic.co/u/Matt_Chambers)\
**Post date:** [June 23, 2014, 7:41pm UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268/3 "2014-06-23T19:41:42Z")

</div>

I think get() is just a shortcut for execute().actionGet()

Anyway, problem solved, it was a problem with my mapping not applying  
correctly with the Java API. Looking into that now.

I'm doing this at the start of the tests to apply the mapping, but its not  
being applied.

client.admin().indices().prepareCreate(indexName).addMapping("product",  
mappingJson).execute().actionGet();  
-Matt

On Tuesday, June 24, 2014 6:35:32 AM UTC+12, Brian wrote:

> Perhaps you need to insert the execute().actionGet() method calls, as  
> below?
> 
> CountRequestBuilder builder = client.prepareCount(indexName)  
> .setTypes("product")  
> .setQuery(getQuery(req));
> 
> _builder.execute().actionGet()_
> 
> return builder.get().getCount();
> 
> I don't use Count, but I have used Query and Update and Delete and they  
> all work similarly in this regard. Just a guess.
> 
> Brian

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5c121d44-81df-4b1f-bdeb-a2fe6ab9ed5c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c121d44-81df-4b1f-bdeb-a2fe6ab9ed5c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Matt\_Chambers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_chambers/32/607_2.png) [@Matt\_Chambers](https://discuss.elastic.co/u/Matt_Chambers)\
**Post date:** [June 23, 2014, 7:57pm UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268/4 "2014-06-23T19:57:28Z")

</div>

Fixed that one by parsing the json as Map\<String,Object\> rather than a  
string.

On Tuesday, June 24, 2014 7:41:42 AM UTC+12, Matt Chambers wrote:

> I think get() is just a shortcut for execute().actionGet()
> 
> Anyway, problem solved, it was a problem with my mapping not applying  
> correctly with the Java API. Looking into that now.
> 
> I'm doing this at the start of the tests to apply the mapping, but its not  
> being applied.
> 
> client.admin().indices().prepareCreate(indexName).addMapping("product",  
> mappingJson).execute().actionGet();  
> -Matt
> 
> On Tuesday, June 24, 2014 6:35:32 AM UTC+12, Brian wrote:
> 
> > Perhaps you need to insert the execute().actionGet() method calls, as  
> > below?
> > 
> > CountRequestBuilder builder = client.prepareCount(indexName)  
> > .setTypes("product")  
> > .setQuery(getQuery(req));
> > 
> > _builder.execute().actionGet()_
> > 
> > return builder.get().getCount();
> > 
> > I don't use Count, but I have used Query and Update and Delete and they  
> > all work similarly in this regard. Just a guess.
> > 
> > Brian

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/95d8d82e-e154-4483-b005-570f3166f362%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/95d8d82e-e154-4483-b005-570f3166f362%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [September 21, 2016, 8:18am UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268/5 "2016-09-21T08:18:16Z")

</div>

how does your resolve the problem?  
i met the same matter with you,this is my code  
CountResponse cResponse = Es\_Utils.getClient().prepareCount(indexName)  
.setTypes(mappingName)  
.execute()  
.actionGet();  
long count = cResponse.getCount();

---

<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 5, 2017, 10:18pm UTC](https://discuss.elastic.co/t/count-not-working-for-java-api-works-for-rest/18268/6 "2017-07-05T22:18:36Z")

</div>


