# Count API with multiple fields in Java

**URL:** <https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695>\
**Category:** Elasticsearch\
**Created:** [March 16, 2015, 6:03pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695 "2015-03-16T18:03:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![satheesh\_kumar](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@satheesh\_kumar](https://discuss.elastic.co/u/satheesh_kumar)\
**Post date:** [March 16, 2015, 6:03pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/1 "2015-03-16T18:03:21Z")

</div>

Hi,

I'm trying to use Count API to see whether a search query returns data  
count 1 or not.

[http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html)

The above example uses single (fieldname, fieldvalue) which works as  
expected.

If I use TermQueryBuilder with single (fieldname, fieldvalue) like the  
below code it works.  
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user.\_id",  
"5506dc497179fa070d31ce37");

CountResponse response =  
elasticSearchConnectionFactory.getConnection().prepareCount("test")  
.setQuery(termQueryBuilder )  
.execute()  
.actionGet();

I've a condition with multiple fields and I expect to return count = 1  
("user.\_id", "5506dc497179fa070d31(ce37")  
("user.parentId", "5506dc497179fa070d31ce36")  
("user.type", "user")  
("user.\_deleted", "false")

When I construct a boolean query for the above fields and try to pass them  
to the below code, it doesn't work.  
CountResponse response =  
elasticSearchConnectionFactory.getConnection().prepareCount("test")  
.setQuery(boolQuery)  
.execute()  
.actionGet();

Can anyone suggest me how to achieve this?

Thanks,  
Sat

--  
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/2fb348d7-7e9c-4a59-812a-cd77df400e1d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2fb348d7-7e9c-4a59-812a-cd77df400e1d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jaroslaw\_Zawila](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jaroslaw\_Zawila](https://discuss.elastic.co/u/Jaroslaw_Zawila)\
**Post date:** [March 16, 2015, 9:32pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/2 "2015-03-16T21:32:00Z")

</div>

Could you post how do you construct second query ??

On Monday, March 16, 2015 at 6:03:21 PM UTC, satheesh kumar wrote:

> Hi,
> 
> I'm trying to use Count API to see whether a search query returns data  
> count 1 or not.
> 
> [http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html)  
> The above example uses single (fieldname, fieldvalue) which works as  
> expected.
> 
> If I use TermQueryBuilder with single (fieldname, fieldvalue) like the  
> below code it works.  
> TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user.\_id",  
> "5506dc497179fa070d31ce37");
> 
> CountResponse response =  
> elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> .setQuery(termQueryBuilder )  
> .execute()  
> .actionGet();
> 
> I've a condition with multiple fields and I expect to return count = 1  
> ("user.\_id", "5506dc497179fa070d31(ce37")  
> ("user.parentId", "5506dc497179fa070d31ce36")  
> ("user.type", "user")  
> ("user.\_deleted", "false")
> 
> When I construct a boolean query for the above fields and try to pass them  
> to the below code, it doesn't work.  
> CountResponse response =  
> elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> .setQuery(boolQuery)  
> .execute()  
> .actionGet();
> 
> Can anyone suggest me how to achieve this?
> 
> Thanks,  
> Sat

--  
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/8f35eff0-c9f4-4537-b9fa-3102549af1e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8f35eff0-c9f4-4537-b9fa-3102549af1e7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jaroslaw\_Zawila](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jaroslaw\_Zawila](https://discuss.elastic.co/u/Jaroslaw_Zawila)\
**Post date:** [March 16, 2015, 10:00pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/3 "2015-03-16T22:00:03Z")

</div>

Hey,

First and for most I am not 100% sure what do you try to achieve.

Could you explain what do you mean it does not work ??? Do you mean the  
results is not as expected ??

If you looking for unique fields (I believe "user.\_id" is unique) I would  
suggest to use filter instead of query. Filters performs quicker as they  
omit score bit and which is more important are cache-able.

Here is example.

```
    ConstantScoreQueryBuilder query = QueryBuilders.constantScoreQuery(

```

> ```
> FilterBuilders
> .andFilter(FilterBuilders.termFilter("hometeam", 
> 
> ```
> 
> "Toronto"))  
> .add(FilterBuilders.termFilter("awayteam",  
> "Boston")));  
> CountResponse response =  
> client.prepareCount("sport").setQuery(query).get();

I hope it helps.

Kind Regards

Jarek

On Monday, March 16, 2015 at 6:03:21 PM UTC, satheesh kumar wrote:

> Hi,
> 
> I'm trying to use Count API to see whether a search query returns data  
> count 1 or not.
> 
> [http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html)  
> The above example uses single (fieldname, fieldvalue) which works as  
> expected.
> 
> If I use TermQueryBuilder with single (fieldname, fieldvalue) like the  
> below code it works.  
> TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user.\_id",  
> "5506dc497179fa070d31ce37");
> 
> CountResponse response =  
> elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> .setQuery(termQueryBuilder )  
> .execute()  
> .actionGet();
> 
> I've a condition with multiple fields and I expect to return count = 1  
> ("user.\_id", "5506dc497179fa070d31(ce37")  
> ("user.parentId", "5506dc497179fa070d31ce36")  
> ("user.type", "user")  
> ("user.\_deleted", "false")
> 
> When I construct a boolean query for the above fields and try to pass them  
> to the below code, it doesn't work.  
> CountResponse response =  
> elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> .setQuery(boolQuery)  
> .execute()  
> .actionGet();
> 
> Can anyone suggest me how to achieve this?
> 
> Thanks,  
> Sat

--  
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/e8e4d10e-e6cb-43d4-874c-06c271402680%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e8e4d10e-e6cb-43d4-874c-06c271402680%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![satheesh\_kumar](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@satheesh\_kumar](https://discuss.elastic.co/u/satheesh_kumar)\
**Post date:** [March 16, 2015, 10:03pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/4 "2015-03-16T22:03:43Z")

</div>

Hi Jaroslaw,  
This is the code for second query.  
BoolQueryBuilder boolQuery = QueryBuilders  
.boolQuery()  
.must(QueryBuilders.matchQuery("user.\_id",  
id).operator(MatchQueryBuilder.Operator.AND))  
.must(QueryBuilders.matchQuery(  
"user.\_parentId", parentId)  
.operator(MatchQueryBuilder.Operator.AND))  
.must(QueryBuilders.matchQuery(  
"user.\_type",  
"user").operator(  
MatchQueryBuilder.Operator.AND))  
.must(QueryBuilders.matchQuery(  
"user.\_deleted", false)  
.operator(MatchQueryBuilder.Operator.AND));

On Monday, March 16, 2015 at 5:32:00 PM UTC-4, Jaroslaw Zawila wrote:

> Could you post how do you construct second query ??
> 
> On Monday, March 16, 2015 at 6:03:21 PM UTC, satheesh kumar wrote:
> 
> > Hi,
> > 
> > I'm trying to use Count API to see whether a search query returns data  
> > count 1 or not.
> > 
> > [http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html)  
> > The above example uses single (fieldname, fieldvalue) which works as  
> > expected.
> > 
> > If I use TermQueryBuilder with single (fieldname, fieldvalue) like the  
> > below code it works.  
> > TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user.\_id",  
> > "5506dc497179fa070d31ce37");
> > 
> > CountResponse response =  
> > elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> > .setQuery(termQueryBuilder )  
> > .execute()  
> > .actionGet();
> > 
> > I've a condition with multiple fields and I expect to return count = 1  
> > ("user.\_id", "5506dc497179fa070d31(ce37")  
> > ("user.parentId", "5506dc497179fa070d31ce36")  
> > ("user.type", "user")  
> > ("user.\_deleted", "false")
> > 
> > When I construct a boolean query for the above fields and try to pass  
> > them to the below code, it doesn't work.  
> > CountResponse response =  
> > elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> > .setQuery(boolQuery)  
> > .execute()  
> > .actionGet();
> > 
> > Can anyone suggest me how to achieve this?
> > 
> > Thanks,  
> > Sat

--  
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/f12f4ddc-6445-4d7a-a1b1-f1a6bb0c8417%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f12f4ddc-6445-4d7a-a1b1-f1a6bb0c8417%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jaroslaw\_Zawila](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jaroslaw\_Zawila](https://discuss.elastic.co/u/Jaroslaw_Zawila)\
**Post date:** [March 16, 2015, 10:26pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/5 "2015-03-16T22:26:52Z")

</div>

What result do you get ??

Could you explain what do you try to achieve please ??

On Monday, March 16, 2015 at 10:03:43 PM UTC, satheesh kumar wrote:

> Hi Jaroslaw,  
> This is the code for second query.  
> BoolQueryBuilder boolQuery = QueryBuilders  
> .boolQuery()  
> .must(QueryBuilders.matchQuery("user.\_id",  
> id).operator(MatchQueryBuilder.Operator.AND))  
> .must(QueryBuilders.matchQuery(  
> "user.\_parentId", parentId)  
> .operator(MatchQueryBuilder.Operator.AND))  
> .must(QueryBuilders.matchQuery(  
> "user.\_type",  
> "user").operator(  
> MatchQueryBuilder.Operator.AND))  
> .must(QueryBuilders.matchQuery(  
> "user.\_deleted", false)  
> .operator(MatchQueryBuilder.Operator.AND));
> 
> On Monday, March 16, 2015 at 5:32:00 PM UTC-4, Jaroslaw Zawila wrote:
> 
> > Could you post how do you construct second query ??
> > 
> > On Monday, March 16, 2015 at 6:03:21 PM UTC, satheesh kumar wrote:
> > 
> > > Hi,
> > > 
> > > I'm trying to use Count API to see whether a search query returns data  
> > > count 1 or not.
> > > 
> > > [http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html)  
> > > The above example uses single (fieldname, fieldvalue) which works as  
> > > expected.
> > > 
> > > If I use TermQueryBuilder with single (fieldname, fieldvalue) like the  
> > > below code it works.  
> > > TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user.\_id",  
> > > "5506dc497179fa070d31ce37");
> > > 
> > > CountResponse response =  
> > > elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> > > .setQuery(termQueryBuilder )  
> > > .execute()  
> > > .actionGet();
> > > 
> > > I've a condition with multiple fields and I expect to return count = 1  
> > > ("user.\_id", "5506dc497179fa070d31(ce37")  
> > > ("user.parentId", "5506dc497179fa070d31ce36")  
> > > ("user.type", "user")  
> > > ("user.\_deleted", "false")
> > > 
> > > When I construct a boolean query for the above fields and try to pass  
> > > them to the below code, it doesn't work.  
> > > CountResponse response =  
> > > elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> > > .setQuery(boolQuery)  
> > > .execute()  
> > > .actionGet();
> > > 
> > > Can anyone suggest me how to achieve this?
> > > 
> > > Thanks,  
> > > Sat

--  
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/dd927a9b-6e5a-4b12-9c50-3c28e8210e42%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dd927a9b-6e5a-4b12-9c50-3c28e8210e42%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![satheesh\_kumar](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@satheesh\_kumar](https://discuss.elastic.co/u/satheesh_kumar)\
**Post date:** [March 16, 2015, 10:59pm UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/6 "2015-03-16T22:59:38Z")

</div>

I"m trying to find whether a user document is present or not. Forget the  
\_parentId. Using the below three condition, I want to get a count =1.  
("user.\_id", "5506dc497179fa070d31(ce37")  
("user.type", "user")  
("user.\_deleted", "false")

Using your sample code, I was able to achieve that. Thank you.

On Monday, March 16, 2015 at 6:26:52 PM UTC-4, Jaroslaw Zawila wrote:

> What result do you get ??
> 
> Could you explain what do you try to achieve please ??
> 
> On Monday, March 16, 2015 at 10:03:43 PM UTC, satheesh kumar wrote:
> 
> > Hi Jaroslaw,  
> > This is the code for second query.  
> > BoolQueryBuilder boolQuery = QueryBuilders  
> > .boolQuery()  
> > .must(QueryBuilders.matchQuery("user.\_id",  
> > id).operator(MatchQueryBuilder.Operator.AND))  
> > .must(QueryBuilders.matchQuery(  
> > "user.\_parentId", parentId)  
> > .operator(MatchQueryBuilder.Operator.AND))  
> > .must(QueryBuilders.matchQuery(  
> > "user.\_type",  
> > "user").operator(  
> > MatchQueryBuilder.Operator.AND))  
> > .must(QueryBuilders.matchQuery(  
> > "user.\_deleted", false)  
> > .operator(MatchQueryBuilder.Operator.AND));
> > 
> > On Monday, March 16, 2015 at 5:32:00 PM UTC-4, Jaroslaw Zawila wrote:
> > 
> > > Could you post how do you construct second query ??
> > > 
> > > On Monday, March 16, 2015 at 6:03:21 PM UTC, satheesh kumar wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I'm trying to use Count API to see whether a search query returns data  
> > > > count 1 or not.
> > > > 
> > > > [http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html](http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/count.html)  
> > > > The above example uses single (fieldname, fieldvalue) which works as  
> > > > expected.
> > > > 
> > > > If I use TermQueryBuilder with single (fieldname, fieldvalue) like the  
> > > > below code it works.  
> > > > TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user.\_id",  
> > > > "5506dc497179fa070d31ce37");
> > > > 
> > > > CountResponse response =  
> > > > elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> > > > .setQuery(termQueryBuilder )  
> > > > .execute()  
> > > > .actionGet();
> > > > 
> > > > I've a condition with multiple fields and I expect to return count = 1  
> > > > ("user.\_id", "5506dc497179fa070d31(ce37")  
> > > > ("user.parentId", "5506dc497179fa070d31ce36")  
> > > > ("user.type", "user")  
> > > > ("user.\_deleted", "false")
> > > > 
> > > > When I construct a boolean query for the above fields and try to pass  
> > > > them to the below code, it doesn't work.  
> > > > CountResponse response =  
> > > > elasticSearchConnectionFactory.getConnection().prepareCount("test")  
> > > > .setQuery(boolQuery)  
> > > > .execute()  
> > > > .actionGet();
> > > > 
> > > > Can anyone suggest me how to achieve this?
> > > > 
> > > > Thanks,  
> > > > Sat

--  
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/3957b9cd-b59a-48d9-8ae1-092bff4e7e97%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3957b9cd-b59a-48d9-8ae1-092bff4e7e97%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:26am UTC](https://discuss.elastic.co/t/count-api-with-multiple-fields-in-java/22695/7 "2017-07-06T00:26:23Z")

</div>


