gpt4 book ai didi

elasticsearch - Spring Data ElasticSearch无法与ElasticSearch 5.5.0连接

转载 作者:行者123 更新时间:2023-12-02 22:22:57 24 4
gpt4 key购买 nike

是ElasticSearch的新手...

真的很喜欢API(尤其是ElasticsearchTemplate和支持单元测试)...

在此示例中使用ElasticSearch 5.5.0(以下链接具有完整的代码内联并且还可以作为可下载的zip文件使用):

https://www.mkyong.com/spring-boot/spring-boot-spring-data-elasticsearch-example/

Maven的依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

EsConfig:
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.mkyong.book.repository")
public class EsConfig {

@Value("${elasticsearch.host}")
private String EsHost;

@Value("${elasticsearch.port}")
private int EsPort;

@Value("${elasticsearch.clustername}")
private String EsClusterName;

@Bean
public Client client() throws Exception {

Settings esSettings = Settings.settingsBuilder()
.put("cluster.name", EsClusterName)
.build();

//https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
return TransportClient.builder()
.settings(esSettings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
return new ElasticsearchTemplate(client());
}
}

src / main / resources:
elasticsearch.clustername = mkyong-cluster
elasticsearch.host = localhost
elasticsearch.port = 9300

发出以下消息时,请:
mvn spring-boot:run

要么
mvn clean test

标准输出:
Caused by: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:326)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:223)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)

另外,在我的ElasticSearch 5.5.0引擎的标准输出中获得以下内容:
[2017-07-23T02:48:46,135][WARN ][o.e.t.n.Netty4Transport  ] [vY7jxpr] exception caught on transport layer [[id: 0xa7e950be, L:/127.0.0.1:9300 - R:/127.0.0.1:60190]], closing connection
java.lang.IllegalStateException: Received message from unsupported version: [1.0.0] minimal compatible version is: [5.0.0]
at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport.java:1379) ~[elasticsearch-5.5.0.jar:5.5.0]
at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channelRead(Netty4MessageChannelHandler.java:74) ~[transport-netty4-5.5.0.jar:5.5.0]

有什么方法可以将Spring Data ElasticSearch与ElasticSearch 5.5.0引擎一起使用吗?

如果是这样,将如何实现连接?

我正在使用Elastic Search提供的Elastic Search Java客户端,并且能够在ElasticSearch 5.5.0上连接并创建索引,但我真的希望我可以使用此库的功能。

最佳答案

Spring Boot 1.5.1不支持ElasticSearch 5.x(您提供的文章也对此进行了说明)。为了使使用ElasticSearch 5.x成为可能,您需要使用Spring Boot 2的里程碑。

尝试使用以下依赖项配置项目:

# spring boot
compile 'org.springframework.boot:spring-boot:2.0.0.M2'

# elasticsearch
compile 'org.elasticsearch:elasticsearch:5.5.0'
compile 'org.elasticsearch.client:transport:5.5.0'

# spring-data-elasticsearch for spring boot
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.0.0.M2'

这应该允许您使用Elasticsearch Java API和spring-data-elasticsearch的所有优点。

更新:基本Gradle(v4.0.1)配置看起来像(build.gradle文件):
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/libs-snapshot' }
maven { url 'http://repo.spring.io/milestone/' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

apply plugin: 'java-library'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

jar {
baseName = 'elastic'
version = '0.0.1'
}

repositories {
mavenCentral()
jcenter()
maven { url 'https://repo.spring.io/libs-snapshot' }
maven { url 'http://repo.spring.io/milestone/' }
}

dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-logging'

compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

compile 'org.elasticsearch:elasticsearch:5.4.1'
compile 'org.elasticsearch.client:transport:5.4.1'

compile 'com.google.guava:guava:20.0'

testCompile 'junit:junit:4.12'
}

关于elasticsearch - Spring Data ElasticSearch无法与ElasticSearch 5.5.0连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45264023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com