# Java Bool Query not adding "query"{ at the top ( ES version 5.4.1 )

**URL:** <https://discuss.elastic.co/t/java-bool-query-not-adding-query-at-the-top-es-version-5-4-1/88877>\
**Category:** Elasticsearch\
**Created:** [June 9, 2017, 6:45pm UTC](https://discuss.elastic.co/t/java-bool-query-not-adding-query-at-the-top-es-version-5-4-1/88877 "2017-06-09T18:45:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samdash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samdash/32/21628_2.png) [@samdash](https://discuss.elastic.co/u/samdash)\
**Post date:** [June 9, 2017, 6:45pm UTC](https://discuss.elastic.co/t/java-bool-query-not-adding-query-at-the-top-es-version-5-4-1/88877/1 "2017-06-09T18:45:49Z")

</div>

`BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); if(null != product && !"".equals(product.trim())){ boolQueryBuilder.must(QueryBuilders.termQuery("community.name",product.toLowerCase())); } if(null != locale && !"".equals(locale.trim())){ boolQueryBuilder.must(QueryBuilders.termQuery("locale",locale.toLowerCase())); } if(null != textSearch && !"".equals(textSearch.trim())){ boolQueryBuilder.must(QueryBuilders.queryStringQuery(textSearch.toLowerCase()).field("subject").defaultOperator(Operator.AND)); System.out.println(queryBuilder.toString()); }`

My BoolQueryBuilder in java results in below , which is wrong, it is missing "query"{ at the top , please let me know the fix , it should result in , however **query** part is missing  
{  
" **query**" : {  
" **bool**" : {

below is the toString of the query builder , which is missing "query" at the top.

{  
"bool" : {  
"must" : [  
{  
"term" : {  
"[community.name](http://community.name)" : {  
"value" : "XXXX",  
"boost" : 1.0  
}  
}  
},  
{  
"term" : {  
"locale" : {  
"value" : "en\_us",  
"boost" : 1.0  
}  
}  
},  
{  
"query\_string" : {  
"query" : "XXX",  
"fields" : [  
"subject^1.0"  
],  
"use\_dis\_max" : true,  
"tie\_breaker" : 0.0,  
"default\_operator" : "and",  
"auto\_generate\_phrase\_queries" : false,  
"max\_determinized\_states" : 10000,  
"enable\_position\_increments" : true,  
"fuzziness" : "AUTO",  
"fuzzy\_prefix\_length" : 0,  
"fuzzy\_max\_expansions" : 50,  
"phrase\_slop" : 0,  
"escape" : false,  
"split\_on\_whitespace" : true,  
"boost" : 1.0  
}  
}  
],  
"disable\_coord" : false,  
"adjust\_pure\_negative" : true,  
"boost" : 1.0  
}  
}

---

<div class="post-metadata">

**Author:** ![samdash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samdash/32/21628_2.png) [@samdash](https://discuss.elastic.co/u/samdash)\
**Post date:** [June 9, 2017, 6:56pm UTC](https://discuss.elastic.co/t/java-bool-query-not-adding-query-at-the-top-es-version-5-4-1/88877/2 "2017-06-09T18:56:29Z")

</div>

resolved .. thanks

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 9, 2017, 7:39pm UTC](https://discuss.elastic.co/t/java-bool-query-not-adding-query-at-the-top-es-version-5-4-1/88877/3 "2017-06-09T19:39:30Z")

</div>

Queries can be nested, so they should have both no knowledge of their  
enclosing context and/or add it themselves. If every query type adding the  
query context, your query would end up looking something like:

{  
"query": {  
"bool": {  
"must": [  
{  
"query": {  
"term": {  
"[community.name](http://community.name)": {  
"value": "XXXX",  
"boost": 1.0  
}  
}  
}  
},

Note the query context surrounding the term sub-query. Obviously not what  
you want

When using the Java API, the enclosing query context is added when setting  
the query as part of the search request:  
client.prepareSearch("someindex").setQuery(boolQueryBuilder.build())

---

<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 7, 2017, 7:39pm UTC](https://discuss.elastic.co/t/java-bool-query-not-adding-query-at-the-top-es-version-5-4-1/88877/4 "2017-07-07T19:39:38Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
