在使用官方Elasticsearch5.5.0版本的java客户端来连接启动在本地的ES的时候会出现如下错误:
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{RtE8nzE9RMSz6nsPf28R8w}{localhost}{127.0.0.1:9300}] ] at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59) at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363) at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408) at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:397) at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1250) at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.analyze(AbstractClient.java:1647) at org.visualchina.elasticsearch.api.demo.BaseDemo.testConnection(BaseDemo.java:38) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) tStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我的pom.xml文件为:
`<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>com.vcg.community</groupId>
<artifactId>elasticsearch-java-api</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<developers>
<developer>
<id>felayman</id>
<name>felayman</name>
<email>felayman@gmail.com</email>
</developer>
</developers>
<properties>
<elasticsearch.version>5.5.0</elasticsearch.version>
<spring-version>4.2.8.RELEASE</spring-version>
<spring-elasticsearch-version>2.0.5.RELEASE</spring-elasticsearch-version>
<spring-data-elasticsearch-version>3.0.0.BUILD-SNAPSHOT</spring-data-elasticsearch-version>
<spring-test-version>4.0.2.RELEASE</spring-test-version>
<fastjson-version>1.2.7</fastjson-version>
<junit-version>4.10</junit-version>
<slf4j-version>1.7.5</slf4j-version>
<ik.version>3.2.8</ik.version>
<spring-test-version>4.1.6.RELEASE</spring-test-version>
<dubbo-version>2.5.3</dubbo-version>
<httpclient-version>4.5.1</httpclient-version>
<zkclient-version>0.1</zkclient-version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>${spring-data-elasticsearch-version}</version>
<exclusions>
<exclusion>
<artifactId>elasticsearch</artifactId>
<groupId>org.elasticsearch</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.wltea.ik-analyzer</groupId>
<artifactId>ik-analyzer</artifactId>
<version>${ik.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<name>Spring Snapshot Repository</name>
<url>http://repo.spring.io/libs-snapshot</url>
</repository>
</repositories>
`
我的测试代码为:
`public class BaseDemo {
protected TransportClient client;
protected ElasticsearchTemplate elasticsearchTemplate;
@Before
public void setUp() throws Exception {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
elasticsearchTemplate = new ElasticsearchTemplate(client);
}
@Test
public void testConnection() throws Exception {
AnalyzeRequest analyzeRequest = new AnalyzeRequest();
analyzeRequest.text("Sean Archer");
ActionFuture<AnalyzeResponse> analyzeResponseActionFuture = elasticsearchTemplate.getClient().admin().indices().analyze(analyzeRequest);
System.out.println(analyzeResponseActionFuture.actionGet().getTokens());
}
}`
我测试了好几个版本,发现5.x版本中有好几个都无法连接,切换成2.x则可以