悟空,拿我的打狗棒来

求解elasticsearch 5.X版本如何基于json进行查询

Elasticsearch | 作者 x291379613 | 发布于2017年05月22日 | 阅读数:5161

在elasticsearch 2.X版本中,我们可以这样:
String str = "{"query":{"bool":{"must":[{"terms":{"id":[1,2,3]}}]}},"aggs":{"group":{"terms":{"field":"id"}}}}";
SearchResponse response = client.prepareSearch("esIndex").setTypes("esType").setSource(dsl).execute().actionGet();
可以直接传入json串进行查询。
在5.X中setSource被弃用了,他的替代方法是什么,怎么样才能基于json来进行查询。
求解各位大大
已邀请:

arvin

赞同来自:

直接使用RestAPI。

RestClient client = RestClient.builder( new HttpHost("127.0.0.1", 9200, "http") ).build(); 
 
HttpEntity entity = new NStringEntity(mapping, ContentType.APPLICATION_JSON); 
 
Response response = client.performRequest("POST", "/bid/info/_search", Collections.singletonMap("pretty", "true"), 
entity); 
 
Result result = EntityUtils.toString(response.getEntity());
 
 System.out.println(result);

要回复问题请先登录注册