# Can I execute Java query using JSON string

**URL:** <https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366>\
**Category:** Elasticsearch\
**Created:** [January 16, 2013, 5:06am UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366 "2013-01-16T05:06:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janusz\_Dalecki](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Janusz\_Dalecki](https://discuss.elastic.co/u/Janusz_Dalecki)\
**Post date:** [January 16, 2013, 5:06am UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366/1 "2013-01-16T05:06:59Z")

</div>

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

--

---

<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:** [January 16, 2013, 2:11pm UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366/2 "2013-01-16T14:11:23Z")

</div>

This not a query. A query has to start with query type and follow Query DSL[http://www.elasticsearch.org/guide/reference/query-dsl/index.html](http://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

--

---

<div class="post-metadata">

**Author:** ![egaumer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egaumer/32/2365_2.png) [@egaumer](https://discuss.elastic.co/u/egaumer)\
**Post date:** [January 16, 2013, 2:45pm UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366/3 "2013-01-16T14:45:48Z")

</div>

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

--

---

<div class="post-metadata">

**Author:** ![Janusz\_Dalecki](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Janusz\_Dalecki](https://discuss.elastic.co/u/Janusz_Dalecki)\
**Post date:** [January 16, 2013, 11:25pm UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366/4 "2013-01-16T23:25:38Z")

</div>

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

--

---

<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:** [January 16, 2013, 11:39pm UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366/5 "2013-01-16T23:39:05Z")

</div>

A list of all possible query types can be found on the right side of this  
page: [Elasticsearch Platform — Find real-time answers at scale | Elastic](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

--

---

<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, 2:56am UTC](https://discuss.elastic.co/t/can-i-execute-java-query-using-json-string/10366/6 "2017-07-06T02:56:01Z")

</div>


