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

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" : {
"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
}
}

resolved .. thanks

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": {
"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())

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