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

此生再无相见时 提交于 2019-11-30 04:58:15

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!