Spring Boot中的Elasticsearch 8.3配置



我一直在尝试用spring-boot实现Elastic Search 8.3。

我得到以下例外:

Cannot convert value of type 'co.elastic.clients.elasticsearch.ElasticsearchClient' to required type 'org.springframework.data.elasticsearch.core.ElasticsearchOperations'

以下是我的弹性搜索配置:

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
@Configuration
@EnableElasticsearchRepositories
public class ElasticConfig {
@Bean
public RestClient client() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "test321"));
HttpHost host = new HttpHost("localhost", 9200);
RestClient restClient = RestClient.builder(host)
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
// TODO Auto-generated method stub
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
return restClient;
}
@Bean
public ElasticsearchClient elasticsearchTemplate() {
ElasticsearchTransport transport = new RestClientTransport(client(), new JacksonJsonpMapper());
return new ElasticsearchClient(transport);
} 

}

以下是POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>aisha</groupId>
<artifactId>courseapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>courseapp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<guava.version>20.0</guava.version>
<geoip2.version>2.15.0</geoip2.version>
<uap-java.version>1.4.0</uap-java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>${geoip2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>


<dependency>
<groupId>com.github.ua-parser</groupId>
<artifactId>uap-java</artifactId>
<version>${uap-java.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>

<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

下面是调用索引产品的简单查询的测试类:

@SpringBootTest
public class ElkSpringTest {
@Autowired
private ElasticsearchRestTemplate elasticConfig;



@Test
void contextLoads() {
SearchResponse<ELKProduct> search;
try {
search = elasticConfig.search(s -> s.index("product").size(10), ELKProduct.class);
System.out.println(search.hits().total().value());
for (Hit<ELKProduct> hit : search.hits().hits()) {
System.out.println(hit.source().getId());
}
System.out.println("******ENding *****");
} catch (ElasticsearchException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
}

由于elastic已升级到最新版本8.3。我无法在春季靴中设置弹性搜索。我通常使用ElasticSearch存储库将数据存储在我的索引中,并使用ElastICearchOperations进行查询。

当前弹簧数据弹性搜索(版本4.4(与es版本8.3(>7.17.6(不太兼容。

ESRestHighLevelClient可以setApiCompatibilityMode(true)启用兼容模式,允许HLRC 7.17与Elasticsearch 8.x一起工作。但兼容性不是很好。

spring-data elasticsearch(版本5.0(现在与ES 8.5.0 兼容

RestHighLevelClient似乎从这个版本开始就被弃用了。

请查看此文档以了解客户端配置的实现情况。

在这个答案的日期,我目前能够配置Java API客户端(由Elasticsearch官方提供(来使用Elasticsearch 8.3.x

使用的依赖项版本(如果有帮助的话(:

  • org.springframework.boot:spring-bootstarter:2.7.13
  • org.springframework.data:spring数据弹性搜索:4.4.13
  • co.elastic.clients:elasticsearchjava:7.17.9(取决于上面的内容(

您的ElasticConfig(带有注释@Configuration(类只需要扩展ElasticsearchConfiguration抽象类

类似于这个的东西

@Configuration
public class ElasticConfig extends ElasticsearchConfiguration {
@Override
public ClientConfiguration clientConfiguration() {
try {
// TODO: Should be on the dev profile to run this on self-signed TLS server.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.esUsername, this.esPassword));
SSLContextBuilder sslBuilder = SSLContexts.custom()
.loadTrustMaterial(null,
(x509Certificates, s) -> true
);
final SSLContext sslContext = sslBuilder.build();
return ClientConfiguration.builder()
.connectedTo(esUris)
.usingSsl(sslContext, NoopHostnameVerifier.INSTANCE)
.withConnectTimeout(10000)
.withBasicAuth(esUsername, esPassword)
.build();
} catch (Exception e) {
// throwing properly
return null;
}
}
}

抽象类已经(预先(声明了足够的Bean来完成我们的工作。我们只需要声明一些操作或存储库就可以完成这项工作。

将您的esUris、esUsername、esPassword与您的相匹配,或者如果您使用的是不安全的本地ES服务器,则将其删除。

最新更新