How to search with only raw query JSON with ES Java API?

I've been trying to get this to work for many hours, but cannot figure out
how to do this.

First I play around with different queries using the Kopf interface, and I
get a working DSL query such as:

{
"query": {
"match": {
"signal": 10
}
}
}

Via a curl command, I can copy and paste the JSON directly:

curl -XPOST 'so:9200/myIndex/_search' -d '{"query":{"match":{"signal":10}}}'

, and I get the same result!

How can I take the exact JSON String and query via the Java API? It should
work for any complex query that Curl or Kopf would accept. For example:
{
"size": 3,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"range": {
"ts": {
"gte": 1423669625
}
}
}
}
}
}

I need to store these queries in a database and use the Java API to make
the queries. I'd like to avoid parsing the JSON and trying to fudge
together a query using the Java API. So far I've tried with no luck
(SearchParseException):

SearchResponse response = client.prepareSearch("myIndex").setTypes("myType"
).setQuery(jsonString).execute().actionGet();

and
WrapperQueryBuilder w = new WrapperQueryBuilder(
"{"size":3,"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"ts":{"gte":1423669625}}}}}}"
);
SearchResponse response = client.prepareSearch("myIndex").setTypes("myType"
).setQuery(w).execute().actionGet();

and

WrapperQueryBuilder w = QueryBuilders.wrapperQuery(
"{"query":{"match":{"signal":10}}}");
SearchResponse response = client.prepareSearch("myIndex").setTypes("myType"
).setQuery(w).execute().actionGet();

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/09b1d9ec-18ad-4507-a382-9aabff817fcb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

I think I found the solution after poking around ES Java API's unit
tests: https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/search/query/SimpleQueryStringTests.java.

SearchResponse response = client.prepareSearch("myIndex").setTypes(

"myType").setSource(

"{"size":3,"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"ts":{"gte":1423669625}}}}}}"
).get();

So far so good...

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e43f8230-832a-4516-8684-95407afee167%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

1 Like