# Possible to pass REST JSON query format to the Java API?

**URL:** <https://discuss.elastic.co/t/possible-to-pass-rest-json-query-format-to-the-java-api/2354>\
**Category:** Elasticsearch\
**Created:** [June 10, 2015, 4:24pm UTC](https://discuss.elastic.co/t/possible-to-pass-rest-json-query-format-to-the-java-api/2354 "2015-06-10T16:24:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Friedman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_friedman/32/606_2.png) [@Matt\_Friedman](https://discuss.elastic.co/u/Matt_Friedman)\
**Post date:** [June 10, 2015, 4:24pm UTC](https://discuss.elastic.co/t/possible-to-pass-rest-json-query-format-to-the-java-api/2354/1 "2015-06-10T16:24:56Z")

</div>

I have the following query string:

```
{"query": {"filtered": {
       "query": {"match_all": {}},
       "filter": {"and": {
          "filters": [
             {"term": {
                "targetUsername": "testuser1"
             }}, {"range": {
                "timestamp": {
                   "from": "2015-06-01T11:16:19.203",
                   "to": "2015-06-01T11:16:19.203",
                   "time_zone": "-4:00"
                }
             }}
          ]
       }}
    }}}

```

I would like to pass this query to the Java API, rather than to the REST API.

Is there a way to do this?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![Matt\_Friedman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_friedman/32/606_2.png) [@Matt\_Friedman](https://discuss.elastic.co/u/Matt_Friedman)\
**Post date:** [June 10, 2015, 8:32pm UTC](https://discuss.elastic.co/t/possible-to-pass-rest-json-query-format-to-the-java-api/2354/2 "2015-06-10T20:32:04Z")

</div>

For reference, what I was asking can be accomplished as follows:

```
{"filtered": {
       "query": {"match_all": {}},
       "filter": {"and": {
          "filters": [
             {"term": {
                "targetUsername": "testuser1"
             }}, {"range": {
                "timestamp": {
                   "from": "2015-06-01T11:16:19.203",
                   "to": "now",
                   "time_zone": "-4:00"
                }
             }}
          ]
       }}
    }}

QueryBuilders.wrapperQuery(jsonQueryString)

```

Just leave out the original "query". The wrapper query takes care of wrapping your input query.

---

<div class="post-metadata">

**Author:** ![Jason\_Wee](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jason\_Wee](https://discuss.elastic.co/u/Jason_Wee)\
**Post date:** [June 11, 2015, 12:42pm UTC](https://discuss.elastic.co/t/possible-to-pass-rest-json-query-format-to-the-java-api/2354/3 "2015-06-11T12:42:14Z")

</div>

heh, nice to know QueryBuilders.wrapperQuery.

if you are still interested in java native way, try this

> QueryBuilder query = filteredQuery(  
> QueryBuilders.matchAllQuery(),  
> FilterBuilders.andFilter(  
> FilterBuilders.queryFilter(QueryBuilders  
> .....

you can of cause print out the query with `query.buildAsBytes().toUtf8()` to cross check with the one you build above.

hth

jason

---

<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:08am UTC](https://discuss.elastic.co/t/possible-to-pass-rest-json-query-format-to-the-java-api/2354/4 "2017-07-06T00:08:11Z")

</div>


