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


(Matt Friedman) #1

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


(Matt Friedman) #2

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.


(Jason Wee) #3

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


(system) #4