RestClient for Elastic Search

时间:2017-11-13 06:23:53

标签: java elasticsearch spring-data-elasticsearch elasticsearch-5

I am trying to fetch data from Elastic search cluster. Index Name : testIndex Type : testType host : localhost port : 8080

I want to use Elastic Search 5.4 version and using RestClient I would like to fetch data using multimatch query.

Sample Query would look something like this.

GET testIndex/testType/_search
{
  "query": {
    "multi_match": {
      "query":"slabs",
      "fields": ["field1","field2"]
    }
  }
}

What would be the best way to do that ? Is it possible that I can use QueryBuilder to build queries and use that with rest client.

2 个答案:

答案 0 :(得分:0)

发布版本6时,RestClient的QueryBuilder将可用。将有一个低级别和一个高级别的REST客户端。高级REST客户端将支持QueryBuilder。你可以在这里玩它:

QueryBuilder with High Level REST Client

在此之前,QueryBuilder不适用于REST客户端,因此在版本5.4中,您需要自己构建查询。有很多方法可以做到这一点。

一种方法是使用jTwig之类的语言模板。 Jettro Coenradie here解释了如何做到这一点的一个非常好的例子。

答案 1 :(得分:0)

RestClient client = new RestClient(serverhost, port,username, password);
SearchSourceBuilder query = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest(IndexName);
searchRequest.source(query);
SearchResponse searchResponse = restClient.search(IndexName , query);

您应该使用Rest客户端创建与elasticsearch的连接,对于查询请求和搜索响应,应该使用SearchSourceBuilder,SearchRespons。对于查询,您可以使用弹性搜索的QueryBuilders。