我期待将Elasticsearch集成到Spring Boot Web应用程序中。以下是我创建传输客户端的配置:
@Configuration
public class ElasticsearchConfig {
private TransportClient client;
@Bean
public TransportClient client() throws UnknownHostException{
Settings settings = Settings.builder()
.put("client.transport.nodes_sampler_interval", "5s")
.put("client.transport.sniff", false)
.put("transport.tcp.compress", true)
.put("cluster.name", "clusterName")
.put("xpack.security.transport.ssl.enabled", true)
.build();
client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
return client;
}
当我启动项目时出现以下错误,我不知道原因:
java.lang.ClassNotFoundException: org.elasticsearch.plugins.NetworkPlugin
我忘记添加依赖项吗?
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</version>
</dependency>
希望你能帮助我
答案 0 :(得分:14)
我只是偶然发现了同样的问题。似乎Elasticsearch文档还没有完成。除了传输客户端依赖项之外,还需要添加elasticsearch依赖项:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.1.1</version>
</dependency>
您还需要log4j依赖关系,但Elasticsearch docs中明确说明了这一点。
答案 1 :(得分:2)
<properties>
<log4j.version>2.6.2</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</version>
<exclusions>
<exclusion>
<artifactId>elasticsearch</artifactId>
<groupId>org.elasticsearch</groupId></exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
</dependency>
尝试用5.1.1替换该版本 好吧它看起来还需要log4j?!
最好的问候, noirabys
答案 2 :(得分:2)
如Elasticsearch Github页面上的this issue所述,SpringBoot starter管理一些默认依赖项,它定义了5.1.1以下的ElasticSearch的默认版本,因此没有这样的类。
您可以在自己的pom中明确定义属性以覆盖其定义。
<properties>
<elasticsearch.version>5.1.1</elasticsearch.version>
</properties>
希望它有所帮助。