一、修改pom.xml文件
<properties>
<es.version>7.14.2</es.version>
<jackson.version>2.10.0</jackson.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>percolator-client</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
注:pom.xml文件中springFarmeWrok版本要一直,否则项目启动报错:
httpClient版本不对也会报错:elasticsearch--Invalid receiver type interface org.apache.http.Header - 景岳 - 博客园
二:变更客户端client
RestHighLevelClient:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
我本地初始化客户端代码稍有不同:
public static RestHighLevelClient buildClient(String clusterName, String hosts, int port) {
return buildClient(clusterName, hosts.split("\\s*,\\s*"), port);
}
public static RestHighLevelClient buildClient(String clusterName, String[] hosts, int port) {
// RestClientBuilder builder = RestClient.builder(Arrays.stream(hosts).map(host -> new HttpHost(host, port)).toArray(HttpHost[]::new));
// final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
// //xpack账户认证
// credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "d0QSyh6tVI4W7663zxuc"));
// //认证和线程数
// builder.setHttpClientConfigCallback(httpClientBuilder -> {
// httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
if (null != esProperties.getThreadCount()) {
}
// httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(25).build());
// return httpClientBuilder;
// });
// builder.setRequestConfigCallback(requestConfigBuilder -> {
// requestConfigBuilder.setConnectTimeout(30000);
// requestConfigBuilder.setSocketTimeout(30000);
// requestConfigBuilder.setConnectionRequestTimeout(30000);
// return requestConfigBuilder;
// });
// builder.setHttpClientConfigCallback(httpClientBuilder -> {
// httpClientBuilder.setMaxConnTotal(25);
// httpClientBuilder.setMaxConnPerRoute(15);
// return httpClientBuilder;
// });
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(Arrays.stream(hosts).map(host -> new InetSocketAddress(host, port)).toArray(InetSocketAddress[]::new))
.withBasicAuth("cluster.name", clusterName)
.withConnectTimeout(30000).withSocketTimeout(30000)
.withHttpClientConfigurer(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(25);
httpClientBuilder.setMaxConnPerRoute(15);
return httpClientBuilder;
})
.build();
return RestClients.create(clientConfiguration).rest();
}
return RestClients.create(clientConfiguration).rest();
三、请求构建
//构建查询器
//5.1版本构建:
//SearchRequestBuilder builder = esClientProvider.getClient().prepareSearch(CommonUtils.getIndex(ds));
//7.14.2版本:
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//sourceBuilder.setQuery(QueryBuilders.boolQuery().must(queryBuilder));
//更新为:
sourceBuilder.query(QueryBuilders.boolQuery().must(queryBuilder);
//相应的aggregation(),from(),size()等方法也稍有变更
//执行获取结果
//response = builder.execute().actionGet();
//更新为:
SearchResponse response = esClientProvider.getClient()
.search(new SearchRequest()
.source(sourceBuilder), RequestOptions.DEFAULT);