Can I execute Java query using JSON string

Hi,
I have a search string that is already JSON string like this:
{
description:"Cisco Network Data Switch"
}
When I pass it to the code below :
SearchRequestBuilder srb =
client.prepareSearch("asset_index").setTypes("Asset").setSearchType(
SearchType.DEFAULT).setQuery(search);
SearchResponse response = srb.execute().actionGet();
... I get :
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to
execute phase [query], total failure; shardFailures
{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][0]:
SearchParseException[[asset_index][0]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][4]:
SearchParseException[[asset_index][4]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][1]:
SearchParseException[[asset_index][1]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][3]:
SearchParseException[[asset_index][3]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }

Regards,
Janusz

--

This not a query. A query has to start with query type and follow Query DSLhttp://www.elasticsearch.org/guide/reference/query-dsl/index.html.
You can make a query out of it by making this json a part of match query
for example:

{
"match_phrase": {
description:"Cisco Network Data Switch"
}
}

But it will not be very flexible solution in the long run. I would advise
parsing this json in your application and building an appropriate query out
of it.

On Wednesday, January 16, 2013 12:06:59 AM UTC-5, JD wrote:

Hi,
I have a search string that is already JSON string like this:
{
description:"Cisco Network Data Switch"
}
When I pass it to the code below :
SearchRequestBuilder srb =
client.prepareSearch("asset_index").setTypes("Asset").setSearchType(
SearchType.DEFAULT).setQuery(search);
SearchResponse response = srb.execute().actionGet();
... I get :
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to
execute phase [query], total failure; shardFailures
{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][0]:
SearchParseException[[asset_index][0]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][4]:
SearchParseException[[asset_index][4]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][1]:
SearchParseException[[asset_index][1]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][3]:
SearchParseException[[asset_index][3]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }

Regards,
Janusz

--

Something like this should work providing you're JSON is a valid query...

client.search(Requests.searchRequest(index).source(jsonQry)).actionGet()

On Wednesday, January 16, 2013 12:06:59 AM UTC-5, JD wrote:

Hi,
I have a search string that is already JSON string like this:
{
description:"Cisco Network Data Switch"
}
When I pass it to the code below :
SearchRequestBuilder srb =
client.prepareSearch("asset_index").setTypes("Asset").setSearchType(
SearchType.DEFAULT).setQuery(search);
SearchResponse response = srb.execute().actionGet();
... I get :
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to
execute phase [query], total failure; shardFailures
{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][0]:
SearchParseException[[asset_index][0]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][4]:
SearchParseException[[asset_index][4]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][1]:
SearchParseException[[asset_index][1]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][3]:
SearchParseException[[asset_index][3]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }

Regards,
Janusz

--

Hi,

Thanks for both replies.

Yep. I agree it was not a query. Where can I find what are possible values
in Query DSL - like the one you mentioned “match_phrase”.

While we are at it I have another question - In production probably it is
better to get from user a search string and pass it as is (like “(Cisco AND
Network) OR Firewall”) to the code like this:

            String search = “(Cisco AND Network)

SearchRequestBuilder srb = client.prepareSearch("asset_index").setTypes(
"Asset").setSearchType(SearchType.DEFAULT);

srb.setQuery(QueryBuilders.queryString(search));

… and let the elasticsearch to do the job for you? Would you agree?

Regards,

Janusz

On Wednesday, 16 January 2013 16:06:59 UTC+11, JD wrote:

Hi,
I have a search string that is already JSON string like this:
{
description:"Cisco Network Data Switch"
}
When I pass it to the code below :
SearchRequestBuilder srb =
client.prepareSearch("asset_index").setTypes("Asset").setSearchType(
SearchType.DEFAULT).setQuery(search);
SearchResponse response = srb.execute().actionGet();
... I get :
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to
execute phase [query], total failure; shardFailures
{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][0]:
SearchParseException[[asset_index][0]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][4]:
SearchParseException[[asset_index][4]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][1]:
SearchParseException[[asset_index][1]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][3]:
SearchParseException[[asset_index][3]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }

Regards,
Janusz

--

A list of all possible query types can be found on the right side of this
page: http://www.elasticsearch.org/guide/reference/query-dsl/index.html

Using queryString query sounds like a good idea. I would say what you are
describing is the primary use case for that query.

On Wednesday, January 16, 2013 6:25:38 PM UTC-5, JD wrote:

Hi,

Thanks for both replies.

Yep. I agree it was not a query. Where can I find what are possible values
in Query DSL - like the one you mentioned “match_phrase”.

While we are at it I have another question - In production probably it is
better to get from user a search string and pass it as is (like “(Cisco AND
Network) OR Firewall”) to the code like this:

            String search = “(Cisco AND Network)

SearchRequestBuilder srb = client.prepareSearch("asset_index").setTypes(
"Asset").setSearchType(SearchType.DEFAULT);

srb.setQuery(QueryBuilders.queryString(search));

… and let the elasticsearch to do the job for you? Would you agree?

Regards,

Janusz

On Wednesday, 16 January 2013 16:06:59 UTC+11, JD wrote:

Hi,
I have a search string that is already JSON string like this:
{
description:"Cisco Network Data Switch"
}
When I pass it to the code below :
SearchRequestBuilder srb =
client.prepareSearch("asset_index").setTypes("Asset").setSearchType(
SearchType.DEFAULT).setQuery(search);
SearchResponse response = srb.execute().actionGet();
... I get :
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to
execute phase [query], total failure; shardFailures
{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][0]:
SearchParseException[[asset_index][0]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][4]:
SearchParseException[[asset_index][4]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][1]:
SearchParseException[[asset_index][1]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }{[PdwFM8JUTjqVbZqPEdOT0w][asset_index][3]:
SearchParseException[[asset_index][3]: from[-1],size[-1]: Parse Failure
[Failed to parse source
[{"query_binary":"ewoJZGVzY3JpcHRpb246IkNpc2NvIE5ldHdvcmsgRGF0YSBTd2l0Y2giCn0="}]]];
nested: QueryParsingException[[asset_index] [_na] query malformed, no field
after start_object]; }

Regards,
Janusz

--