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


(bilash) #1

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!


(Igor Motov) #2

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!


(bilash) #3

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!


(Igor Motov) #4

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!


(bilash) #5

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!


(system) #6