{"id":1385,"date":"2023-03-19T11:05:34","date_gmt":"2023-03-19T03:05:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1385"},"modified":"2023-04-28T21:11:50","modified_gmt":"2023-04-28T13:11:50","slug":"elasticsearch-7-x-integrate-resthighlevelclient","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/elasticsearch-7-x-integrate-resthighlevelclient\/","title":{"rendered":"ElasticSearch 7.x \u96c6\u6210RestHighLevelClient"},"content":{"rendered":"<p><code>Java High Level REST Client<\/code>\u662f\u76ee\u524d\u5b98\u65b9\u63a8\u8350\u4f7f\u7528\u7684\u5ba2\u6237\u7aef\uff0cElasticsearch \u57287.0\u4e2d\u5bf9TransportClient\u8fdb\u884c\u5f03\u7528\uff0c\u5e76\u57288.0\u4e2d\u5b8c\u5168\u5220\u9664\uff0c\u66ff\u800c\u4ee3\u4e4b\u7684\u662f<code>Java High Level REST Client<\/code>\u3002<code>High Level REST Client<\/code>\u4e0e Elasticsearch \u5177\u6709\u76f8\u540c\u7684\u53d1\u5e03\u5468\u671f\u3002\u6545\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u6700\u65b0\u7248\u7684 Elasticsearch<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/client\/java-rest\/7.x\/java-rest-high-getting-started-maven.html\">https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/client\/java-rest\/7.x\/java-rest-high-getting-started-maven.html<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>Maven\u4f9d\u8d56<\/h2>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.elasticsearch.client&lt;\/groupId&gt;\n    &lt;artifactId&gt;elasticsearch-rest-high-level-client&lt;\/artifactId&gt;\n    &lt;version&gt;7.1.0&lt;\/version&gt;\n    &lt;exclusions&gt;\n        &lt;exclusion&gt;\n            &lt;groupId&gt;org.elasticsearch&lt;\/groupId&gt;\n            &lt;artifactId&gt;elasticsearch&lt;\/artifactId&gt;\n        &lt;\/exclusion&gt;\n        &lt;exclusion&gt;\n            &lt;groupId&gt;org.elasticsearch.client&lt;\/groupId&gt;\n            &lt;artifactId&gt;elasticsearch-rest-client&lt;\/artifactId&gt;\n        &lt;\/exclusion&gt;\n    &lt;\/exclusions&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.elasticsearch&lt;\/groupId&gt;\n    &lt;artifactId&gt;elasticsearch&lt;\/artifactId&gt;\n    &lt;version&gt;7.1.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.elasticsearch.client&lt;\/groupId&gt;\n    &lt;artifactId&gt;elasticsearch-rest-client&lt;\/artifactId&gt;\n    &lt;version&gt;7.1.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>elasticsearch\u5fc5\u987b\u8981\u5355\u72ec\u4f9d\u8d56\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u5982\u4e0b\u9519\u8bef:<\/p>\n<pre><code>An attempt was made to call the method org.elasticsearch.action.search.SearchRequest.isCcsMinimizeRoundtrips()Z but it does not exist.<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/elastic\/elasticsearch\/issues\/41189\">https:\/\/github.com\/elastic\/elasticsearch\/issues\/41189<\/a><\/p>\n<h2>\u96c6\u6210RestHighLevelClient<\/h2>\n<p>\u4e3a\u4e86\u7b80\u5316EsClient\u7684\u521b\u5efa\u6d41\u7a0b\u548c\u591aes\u96c6\u7fa4\u7684\u652f\u6301\uff0c\u7f16\u5199\u4e00\u4e2a\u6784\u9020\u5668<\/p>\n<pre><code class=\"language-java\">import org.apache.http.HttpHost;\nimport org.elasticsearch.client.RestClient;\nimport org.elasticsearch.client.RestClientBuilder;\nimport org.elasticsearch.client.RestHighLevelClient;\nimport org.springframework.stereotype.Component;\n\nimport java.util.List;\n\n\/**\n * @Author: yezhou\n * @Date: 2019\/5\/24 21:04\n * @Version 1.0\n *\/\n\n@Component\npublic class EsClientBuilder {\n    private int connectTimeoutMillis = 1000;\n    private int socketTimeoutMillis = 30000;\n    private int connectionRequestTimeoutMillis = 500;\n    private int maxConnectPerRoute = 10;\n    private int maxConnectTotal = 30;\n\n    private final List&lt;HttpHost&gt; httpHosts;\n\n    private EsClientBuilder(List&lt;HttpHost&gt; httpHosts) {\n        this.httpHosts = httpHosts;\n    }\n\n    public EsClientBuilder setConnectTimeoutMillis(int connectTimeoutMillis) {\n        this.connectTimeoutMillis = connectTimeoutMillis;\n        return this;\n    }\n\n    public EsClientBuilder setSocketTimeoutMillis(int socketTimeoutMillis) {\n        this.socketTimeoutMillis = socketTimeoutMillis;\n        return this;\n    }\n\n    public EsClientBuilder setConnectionRequestTimeoutMillis(int connectionRequestTimeoutMillis) {\n        this.connectionRequestTimeoutMillis = connectionRequestTimeoutMillis;\n        return this;\n    }\n\n    public EsClientBuilder setMaxConnectPerRoute(int maxConnectPerRoute) {\n        this.maxConnectPerRoute = maxConnectPerRoute;\n        return this;\n    }\n\n    public EsClientBuilder setMaxConnectTotal(int maxConnectTotal) {\n        this.maxConnectTotal = maxConnectTotal;\n        return this;\n    }\n\n    public static EsClientBuilder build(List&lt;HttpHost&gt; httpHosts) {\n        return new EsClientBuilder(httpHosts);\n    }\n\n    public RestHighLevelClient create() {\n\n        HttpHost[] httpHostArr = httpHosts.toArray(new HttpHost[0]);\n        RestClientBuilder builder = RestClient.builder(httpHostArr);\n\n        builder.setRequestConfigCallback(requestConfigBuilder -&gt; {\n            requestConfigBuilder.setConnectTimeout(connectTimeoutMillis);\n            requestConfigBuilder.setSocketTimeout(socketTimeoutMillis);\n            requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeoutMillis);\n            return requestConfigBuilder;\n        });\n\n        builder.setHttpClientConfigCallback(httpClientBuilder -&gt; {\n            httpClientBuilder.setMaxConnTotal(maxConnectTotal);\n            httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);\n            return httpClientBuilder;\n        });\n\n        return new RestHighLevelClient(builder);\n    }\n}<\/code><\/pre>\n<p>\u914d\u7f6e\u6587\u4ef6application.yml<\/p>\n<pre><code class=\"language-yml\">elasticsearch:\n  nodes: 192.168.1.10:9200\n  schema: http\n  max-connect-total: 50\n  max-connect-per-route: 10\n  connection-request-timeout-millis: 500\n  socket-timeout-millis: 30000\n  connect-timeout-millis: 1000<\/code><\/pre>\n<p>\u914d\u7f6e\u7c7bConfiguration<\/p>\n<pre><code class=\"language-java\">import org.apache.http.HttpHost;\nimport org.elasticsearch.client.RestHighLevelClient;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.util.Assert;\nimport org.springframework.util.StringUtils;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\n\/**\n * @Author: yezhou\n * @Date: 2019\/5\/24 21:06\n * @Version 1.0\n *\/\n\n@Configuration\npublic class ESConfig {\n    @Value(&quot;${elasticsearch.nodes}&quot;)\n    private List&lt;String&gt; nodes;\n\n    @Value(&quot;${elasticsearch.schema}&quot;)\n    private String schema;\n\n    @Value(&quot;${elasticsearch.max-connect-total}&quot;)\n    private Integer maxConnectTotal;\n\n    @Value(&quot;${elasticsearch.max-connect-per-route}&quot;)\n    private Integer maxConnectPerRoute;\n\n    @Value(&quot;${elasticsearch.connection-request-timeout-millis}&quot;)\n    private Integer connectionRequestTimeoutMillis;\n\n    @Value(&quot;${elasticsearch.socket-timeout-millis}&quot;)\n    private Integer socketTimeoutMillis;\n\n    @Value(&quot;${elasticsearch.connect-timeout-millis}&quot;)\n    private Integer connectTimeoutMillis;\n\n    @Bean\n    public RestHighLevelClient getRestHighLevelClient() {\n        List&lt;HttpHost&gt; httpHosts = new ArrayList&lt;&gt;();\n        for (String node : nodes) {\n            try {\n                String[] parts = StringUtils.split(node, &quot;:&quot;);\n                Assert.notNull(parts,&quot;Must defined&quot;);\n                Assert.state(parts.length == 2, &quot;Must be defined as &#039;host:port&#039;&quot;);\n                httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), schema));\n            } catch (RuntimeException ex) {\n                throw new IllegalStateException(\n                        &quot;Invalid ES nodes &quot; + &quot;property &#039;&quot; + node + &quot;&#039;&quot;, ex);\n            }\n        }\n\n        return EsClientBuilder.build(httpHosts)\n                .setConnectionRequestTimeoutMillis(connectionRequestTimeoutMillis)\n                .setConnectTimeoutMillis(connectTimeoutMillis)\n                .setSocketTimeoutMillis(socketTimeoutMillis)\n                .setMaxConnectTotal(maxConnectTotal)\n                .setMaxConnectPerRoute(maxConnectPerRoute)\n                .create();\n    }\n}<\/code><\/pre>\n<p>\u63a7\u5236\u5668Controller<\/p>\n<pre><code class=\"language-java\">import com.alibaba.fastjson.JSON;\nimport lombok.extern.slf4j.Slf4j;\nimport org.apache.http.HttpHost;\nimport org.elasticsearch.action.search.SearchRequest;\nimport org.elasticsearch.action.search.SearchResponse;\nimport org.elasticsearch.action.search.SearchType;\nimport org.elasticsearch.client.RequestOptions;\nimport org.elasticsearch.client.RestClient;\nimport org.elasticsearch.client.RestHighLevelClient;\nimport org.elasticsearch.index.query.BoolQueryBuilder;\nimport org.elasticsearch.index.query.QueryBuilders;\nimport org.elasticsearch.search.builder.SearchSourceBuilder;\nimport org.elasticsearch.search.sort.SortOrder;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.ResponseBody;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport javax.annotation.Resource;\nimport java.io.IOException;\n\n\/**\n * @Author: yezhou\n * @Date: 2019\/5\/24 20:13\n * @Version 1.0\n *\/\n\n@Slf4j\n@RestController\npublic class ElasticSearchController {\n\n    @Resource\n    private RestHighLevelClient client;\n\n    @GetMapping(&quot;\/search&quot;)\n    @ResponseBody\n    public String search() throws IOException {\n\/\/        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(&quot;192.168.165.239&quot;, 9200, &quot;http&quot;)));\n\n        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();\n\n        boolQueryBuilder.must(QueryBuilders.termQuery(&quot;loglvl.keyword&quot;, &quot;ERROR&quot;));\n\/\/        boolQueryBuilder.must(QueryBuilders.wildcardQuery(&quot;loglvl.keyword&quot;, &quot;INFO&quot;));\n\/\/        boolQueryBuilder.must(QueryBuilders.rangeQuery(&quot;field&quot;).gt(&quot;value&quot;));\n\/\/        boolQueryBuilder.must(QueryBuilders.termsQuery(&quot;loglvl.keyword&quot;, &quot;INFO&quot;));\n\n\/\/        boolQueryBuilder.must(QueryBuilders.matchQuery(&quot;loglvl&quot;, &quot;ERROR&quot;));\n\n        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();\n        sourceBuilder.query(boolQueryBuilder);\n        sourceBuilder.sort(&quot;@timestamp&quot;, SortOrder.DESC);\n        sourceBuilder.from(0);\n        sourceBuilder.size(10);\n\n        RequestOptions requestOptions = RequestOptions.DEFAULT;\n\n        SearchRequest searchRequest = new SearchRequest(&quot;thaipay&quot;);\n        \/\/searchRequest.searchType(SearchType.QUERY_THEN_FETCH);\n        searchRequest.source(sourceBuilder);\n        log.info(searchRequest.toString());\n        SearchResponse searchResponse = client.search(searchRequest, requestOptions);\n        return JSON.toJSONString(searchResponse);\n    }\n\n}<\/code><\/pre>\n<p>\u5982\u679c\u662f<code>cluster<\/code>\uff0c<code>application.yml<\/code>\u7684<code>nodes<\/code>\u8bbe\u7f6e\u591a\u4e2a<code>ip:host<\/code>\u9017\u53f7\u9694\u5f00\u5373\u53ef\u3002<\/p>\n<p>ps: \u591a\u4e2a\u96c6\u7fa4\uff0c\u5728<code>application.yml<\/code>\u518d\u5b9a\u4e49\u4e00\u7ec4\u914d\u7f6e\uff0c\u5728ESConfig\u4e2d\u521b\u5efa\u4e24\u4e2aname\uff0c\u4e0d\u540c\u7684<code>RestHighLevelClient Bean<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java High Level REST Client\u662f\u76ee\u524d\u5b98\u65b9\u63a8\u8350\u4f7f\u7528\u7684\u5ba2\u6237\u7aef\uff0cElasticsearch  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[301],"tags":[],"class_list":["post-1385","post","type-post","status-publish","format-standard","hentry","category-elasticsearch"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1385"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1385\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}