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

**URL:** <https://discuss.elastic.co/t/how-to-search-with-only-raw-query-json-with-es-java-api/22194>\
**Category:** Elasticsearch\
**Created:** [February 16, 2015, 5:17pm UTC](https://discuss.elastic.co/t/how-to-search-with-only-raw-query-json-with-es-java-api/22194 "2015-02-16T17:17:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Molter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_molter/32/871_2.png) [@Tim\_Molter](https://discuss.elastic.co/u/Tim_Molter)\
**Post date:** [February 16, 2015, 5:17pm UTC](https://discuss.elastic.co/t/how-to-search-with-only-raw-query-json-with-es-java-api/22194/1 "2015-02-16T17:17:11Z")

</div>

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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/09b1d9ec-18ad-4507-a382-9aabff817fcb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Tim\_Molter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_molter/32/871_2.png) [@Tim\_Molter](https://discuss.elastic.co/u/Tim_Molter)\
**Post date:** [February 16, 2015, 6:15pm UTC](https://discuss.elastic.co/t/how-to-search-with-only-raw-query-json-with-es-java-api/22194/2 "2015-02-16T18:15:11Z")

</div>

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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/e43f8230-832a-4516-8684-95407afee167%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:32am UTC](https://discuss.elastic.co/t/how-to-search-with-only-raw-query-json-with-es-java-api/22194/3 "2017-07-06T00:32:24Z")

</div>


