# How to convert/wrap a BooleanQuery to/in a BoolQueryBuilder object?

**URL:** <https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619>\
**Category:** Elasticsearch\
**Created:** [August 3, 2012, 2:33am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619 "2012-08-03T02:33:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bilash](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bilash](https://discuss.elastic.co/u/bilash)\
**Post date:** [August 3, 2012, 2:33am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619/1 "2012-08-03T02:33:51Z")

</div>

My application creates BoolQueryBuilder objects using Java API. Now I need  
to cache these in Jedis for reuse. Jedis only allows String type objects to  
be stored in it. So I convert the BoolQueryBuilder objects to String  
objects and store it. But how do I get BoolQueryBuilder objects back from  
these String objects? An example String representation is:

{  
"bool" : {  
"must" : [ {  
"text" : {  
"levelCount" : {  
"query" : 1,  
"type" : "phrase"  
}  
}  
}, {  
"query\_string" : {  
"query" : ""I love New York"",  
"fields" : ["level\_0"],  
"default\_operator" : "and",  
"analyzer" : "defaultAnalyzer",  
"phrase\_slop" : 0  
}  
} ]  
}  
}

I can convert this String to a BooleanQuery using QueryParser parse method.  
But then how do I convert this BooleanQuery back to a BoolQueryBuilder? I  
need a BoolQueryBuilder object because my SearchRequestBuilder expects a  
builder object, not the query itself.

Any ideas?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 4, 2012, 1:32am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619/2 "2012-08-04T01:32:16Z")

</div>

Are you using some custom version  
of SearchRequestBuilder? The standard SearchRequestBuilder object actually  
accepts String as a query.

On Thursday, August 2, 2012 10:33:51 PM UTC-4, bilash wrote:

> My application creates BoolQueryBuilder objects using Java API. Now I need  
> to cache these in Jedis for reuse. Jedis only allows String type objects to  
> be stored in it. So I convert the BoolQueryBuilder objects to String  
> objects and store it. But how do I get BoolQueryBuilder objects back from  
> these String objects? An example String representation is:
> 
> {  
> "bool" : {  
> "must" : [ {  
> "text" : {  
> "levelCount" : {  
> "query" : 1,  
> "type" : "phrase"  
> }  
> }  
> }, {  
> "query\_string" : {  
> "query" : ""I love New York"",  
> "fields" : ["level\_0"],  
> "default\_operator" : "and",  
> "analyzer" : "defaultAnalyzer",  
> "phrase\_slop" : 0  
> }  
> } ]  
> }  
> }
> 
> I can convert this String to a BooleanQuery using QueryParser parse  
> method. But then how do I convert this BooleanQuery back to a  
> BoolQueryBuilder? I need a BoolQueryBuilder object because my  
> SearchRequestBuilder expects a builder object, not the query itself.
> 
> Any ideas?
> 
> Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bilash](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bilash](https://discuss.elastic.co/u/bilash)\
**Post date:** [August 4, 2012, 1:52am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619/3 "2012-08-04T01:52:05Z")

</div>

Actually I am using prepareSearch which seems to only accept a QueryBuilder  
objects as arguments. Is there a way I could pass a String query to  
prepareSearch?

Here is how I am using it:

SearchRequestBuilder search = prepareSearch(query.getBoolQueryBuilder(),  
query.getBoolFilterBuilder(), query.getSortBuilder());  
SearchResponse response = search.execute().actionGet();

Here query is a custom query object that basically encapsulates a  
BoolQueryBuilder. Is there any other way I could execute the above search  
with a String query?

Thanks a lot for helping out.

On Friday, August 3, 2012 9:32:16 PM UTC-4, Igor Motov wrote:

> Are you using some custom version  
> of SearchRequestBuilder? The standard SearchRequestBuilder object actually  
> accepts String as a query.
> 
> On Thursday, August 2, 2012 10:33:51 PM UTC-4, bilash wrote:
> 
> > My application creates BoolQueryBuilder objects using Java API. Now I  
> > need to cache these in Jedis for reuse. Jedis only allows String type  
> > objects to be stored in it. So I convert the BoolQueryBuilder objects to  
> > String objects and store it. But how do I get BoolQueryBuilder objects back  
> > from these String objects? An example String representation is:
> > 
> > {  
> > "bool" : {  
> > "must" : [ {  
> > "text" : {  
> > "levelCount" : {  
> > "query" : 1,  
> > "type" : "phrase"  
> > }  
> > }  
> > }, {  
> > "query\_string" : {  
> > "query" : ""I love New York"",  
> > "fields" : ["level\_0"],  
> > "default\_operator" : "and",  
> > "analyzer" : "defaultAnalyzer",  
> > "phrase\_slop" : 0  
> > }  
> > } ]  
> > }  
> > }
> > 
> > I can convert this String to a BooleanQuery using QueryParser parse  
> > method. But then how do I convert this BooleanQuery back to a  
> > BoolQueryBuilder? I need a BoolQueryBuilder object because my  
> > SearchRequestBuilder expects a builder object, not the query itself.
> > 
> > Any ideas?
> > 
> > Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 4, 2012, 2:23am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619/4 "2012-08-04T02:23:26Z")

</div>

The prepareSearch function that you are referring to is probably your  
internal function, correct? I am not sure how it's implemented and what  
restrictions it has.

I meant something like this:

SearchResponse response  
= client.prepareSearch("my-index").setQuery(query).setFilter(filter).addSort(sortBuilder).execute.actionGet();

Here query and filter can be Builders as well as Strings.

Igor

On Friday, August 3, 2012 9:52:05 PM UTC-4, bilash wrote:

> Actually I am using prepareSearch which seems to only accept a  
> QueryBuilder objects as arguments. Is there a way I could pass a String  
> query to prepareSearch?
> 
> Here is how I am using it:
> 
> SearchRequestBuilder search = prepareSearch(query.getBoolQueryBuilder(),  
> query.getBoolFilterBuilder(), query.getSortBuilder());  
> SearchResponse response = search.execute().actionGet();
> 
> Here query is a custom query object that basically encapsulates a  
> BoolQueryBuilder. Is there any other way I could execute the above search  
> with a String query?
> 
> Thanks a lot for helping out.
> 
> On Friday, August 3, 2012 9:32:16 PM UTC-4, Igor Motov wrote:
> 
> > Are you using some custom version  
> > of SearchRequestBuilder? The standard SearchRequestBuilder object actually  
> > accepts String as a query.
> > 
> > On Thursday, August 2, 2012 10:33:51 PM UTC-4, bilash wrote:
> > 
> > > My application creates BoolQueryBuilder objects using Java API. Now I  
> > > need to cache these in Jedis for reuse. Jedis only allows String type  
> > > objects to be stored in it. So I convert the BoolQueryBuilder objects to  
> > > String objects and store it. But how do I get BoolQueryBuilder objects back  
> > > from these String objects? An example String representation is:
> > > 
> > > {  
> > > "bool" : {  
> > > "must" : [ {  
> > > "text" : {  
> > > "levelCount" : {  
> > > "query" : 1,  
> > > "type" : "phrase"  
> > > }  
> > > }  
> > > }, {  
> > > "query\_string" : {  
> > > "query" : ""I love New York"",  
> > > "fields" : ["level\_0"],  
> > > "default\_operator" : "and",  
> > > "analyzer" : "defaultAnalyzer",  
> > > "phrase\_slop" : 0  
> > > }  
> > > } ]  
> > > }  
> > > }
> > > 
> > > I can convert this String to a BooleanQuery using QueryParser parse  
> > > method. But then how do I convert this BooleanQuery back to a  
> > > BoolQueryBuilder? I need a BoolQueryBuilder object because my  
> > > SearchRequestBuilder expects a builder object, not the query itself.
> > > 
> > > Any ideas?
> > > 
> > > Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bilash](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@bilash](https://discuss.elastic.co/u/bilash)\
**Post date:** [August 4, 2012, 2:26am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619/5 "2012-08-04T02:26:20Z")

</div>

Yes, it is my own wrapper around Client.prepareSearch.

Thanks a lot for the help. I will try the String version/

On Friday, August 3, 2012 10:23:26 PM UTC-4, Igor Motov wrote:

> The prepareSearch function that you are referring to is probably your  
> internal function, correct? I am not sure how it's implemented and what  
> restrictions it has.
> 
> I meant something like this:
> 
> SearchResponse response  
> = client.prepareSearch("my-index").setQuery(query).setFilter(filter).addSort(sortBuilder).execute.actionGet();
> 
> Here query and filter can be Builders as well as Strings.
> 
> Igor
> 
> On Friday, August 3, 2012 9:52:05 PM UTC-4, bilash wrote:
> 
> > Actually I am using prepareSearch which seems to only accept a  
> > QueryBuilder objects as arguments. Is there a way I could pass a String  
> > query to prepareSearch?
> > 
> > Here is how I am using it:
> > 
> > SearchRequestBuilder search = prepareSearch(query.getBoolQueryBuilder(),  
> > query.getBoolFilterBuilder(), query.getSortBuilder());  
> > SearchResponse response = search.execute().actionGet();
> > 
> > Here query is a custom query object that basically encapsulates a  
> > BoolQueryBuilder. Is there any other way I could execute the above search  
> > with a String query?
> > 
> > Thanks a lot for helping out.
> > 
> > On Friday, August 3, 2012 9:32:16 PM UTC-4, Igor Motov wrote:
> > 
> > > Are you using some custom version  
> > > of SearchRequestBuilder? The standard SearchRequestBuilder object actually  
> > > accepts String as a query.
> > > 
> > > On Thursday, August 2, 2012 10:33:51 PM UTC-4, bilash wrote:
> > > 
> > > > My application creates BoolQueryBuilder objects using Java API. Now I  
> > > > need to cache these in Jedis for reuse. Jedis only allows String type  
> > > > objects to be stored in it. So I convert the BoolQueryBuilder objects to  
> > > > String objects and store it. But how do I get BoolQueryBuilder objects back  
> > > > from these String objects? An example String representation is:
> > > > 
> > > > {  
> > > > "bool" : {  
> > > > "must" : [ {  
> > > > "text" : {  
> > > > "levelCount" : {  
> > > > "query" : 1,  
> > > > "type" : "phrase"  
> > > > }  
> > > > }  
> > > > }, {  
> > > > "query\_string" : {  
> > > > "query" : ""I love New York"",  
> > > > "fields" : ["level\_0"],  
> > > > "default\_operator" : "and",  
> > > > "analyzer" : "defaultAnalyzer",  
> > > > "phrase\_slop" : 0  
> > > > }  
> > > > } ]  
> > > > }  
> > > > }
> > > > 
> > > > I can convert this String to a BooleanQuery using QueryParser parse  
> > > > method. But then how do I convert this BooleanQuery back to a  
> > > > BoolQueryBuilder? I need a BoolQueryBuilder object because my  
> > > > SearchRequestBuilder expects a builder object, not the query itself.
> > > > 
> > > > Any ideas?
> > > > 
> > > > Thanks in advance!

---

<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, 3:17am UTC](https://discuss.elastic.co/t/how-to-convert-wrap-a-booleanquery-to-in-a-boolquerybuilder-object/8619/6 "2017-07-06T03:17:44Z")

</div>


