How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch? How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch? json json

How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?


You can use QueryBuilders.wrapperQuery(jsonQueryString);


You can use setQuery, which can receive a json format string.

/** * Constructs a new search source builder with a raw search query. */public SearchRequestBuilder setQuery(String query) {    sourceBuilder().query(query);    return this;}

Note this: only part of the DSL is needed, the {"query": } part is omitted, like this:

SearchResponse searchResponse = client.prepareSearch(indices).setQuery("{\"term\": {\"id\": 1}}").execute().actionGet();


It might be worth investigating low level rest client. With this you can do:

RestClient esClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();Request request = new Request("POST", "/INDEX_NAME/_doc/_search");request.setJsonEntity(yourJsonQueryString);Response response = esClient.performRequest(request);String jsonResponse = EntityUtils.toString(response.getEntity());