java Spring Boot 应用程序中的 Spring Data 弹性搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36902060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data Elastic search in a Spring Boot application
提问by jemlifathi
I have a Spring Boot application and I want to use Elastic search 2.2.0 standalone (not the embedded server) in it, I wanna use Spring Data Elastic search, so what are the Elastic search supported versions by Spring Data and how can I configure it to connect to elasticsearch instance running in localhost:9200?
我有一个 Spring Boot 应用程序,我想在其中使用独立的 Elastic search 2.2.0(不是嵌入式服务器),我想使用 Spring Data Elastic search,那么 Spring Data 支持哪些 Elastic search 版本以及如何配置它连接到在 localhost:9200 中运行的 elasticsearch 实例?
Actually, I tried adding this options to my application.properties file:
实际上,我尝试将此选项添加到我的 application.properties 文件中:
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=localhost:9200
And later, I created this configuration class:
后来,我创建了这个配置类:
@Configuration
public class ElasticConfig {
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
@Bean
public Client client() {
TransportClient client = new TransportClient();
TransportAddress address = new InetSocketTransportAddress(
"localhost",9200);
client.addTransportAddress(address);
return client;
}
}
I get this stacktrace:
我得到这个堆栈跟踪:
2016-04-28 00:03:52.246 INFO 25613 --- [ restartedMain] org.elasticsearch.plugins : [Aardwolf] loaded [], sites [] 2016-04-28 00:04:01.356 INFO 25613 --- [ restartedMain] org.elasticsearch.client.transport : [Aardwolf] failed to get node info for [#transport#-1][fathi-HP-Pavilion-g6-Notebook-PC][inet[localhost/127.0.0.1:9200]], disconnecting...
org.elasticsearch.transport.ReceiveTimeoutTransportException: [][inet[localhost/127.0.0.1:9200]][cluster:monitor/nodes/info] request_id [0] timed out after [5001ms] at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:529) ~[elasticsearch-1.5.2.jar:na] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_77] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_77] at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_77]
2016-04-28 00:04:01.512 ERROR 25613 --- [ restartedMain] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
2016-04-28 00:03:52.246 INFO 25613 --- [restartedMain] org.elasticsearch.plugins : [Aardwolf] 加载 [], 站点 [] 2016-04-28 00:04:01.356 INFO 25613 [ restartedMain] org.elasticsearch.client.transport :[Aardwolf] 无法获取 [#transport#-1][fathi-HP-Pavilion-g6-Notebook-PC][inet[localhost/127.0.0.1:9200] 的节点信息],断开连接...
org.elasticsearch.transport.ReceiveTimeoutTransportException: [][inet[localhost/127.0.0.1:9200]][cluster:monitor/nodes/info] request_id [0] 在 org.elasticsearch.transport.TransportService$ [5001ms] 后超时TimeoutHandler.run(TransportService.java:529) ~[elasticsearch-1.5.2.jar:na] 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_77] 在 java。 util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_77] 在 java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_77]
2016-04-28 00:04:01.512 错误 25613 --- [restartedMain] .dersAbstractElasticsearchRepository:加载弹性搜索节点失败:org.elasticsearch.client.transport.NoNodeAvailableException:没有配置的节点可用:[]
回答by jemlifathi
I got this answer from the ES forum and it worked for me:
我从 ES 论坛得到了这个答案,它对我有用:
First, Spring Data Elasticsearch works officially with ES 1.x versions(for me it worked with 1.7.1). Second, the port used in the configuration must be 9300
首先,Spring Data Elasticsearch 正式使用 ES 1.x 版本(对我来说它适用于 1.7.1)。二、配置中使用的端口必须是9300
I made these changes and it worked pretty perfect.
我进行了这些更改,效果非常完美。
回答by ignacio.suay
As Jemli said you will need to use the port 9300.
正如 Jemli 所说,您将需要使用端口 9300。
Also make sure that your elastiscsearch client and server are using the same major version. If you are using elasticsearch 2.x you will need to update spring boot to the latest version ( > 1.4.0.RC1).
还要确保您的 elasticsearch 客户端和服务器使用相同的主要版本。如果您使用的是 elasticsearch 2.x,则需要将 spring boot 更新到最新版本(> 1.4.0.RC1)。
Please have a look to this post if you need more information: http://ignaciosuay.com/how-to-connect-spring-boot-to-elasticsearch-2-x-x/
如果您需要更多信息,请查看这篇文章:http: //ignaciosuay.com/how-to-connect-spring-boot-to-elasticsearch-2-xx/
回答by mengying.ye
I read official document. If use Java Config,please try:
我阅读了官方文档。如果使用 Java Config,请尝试:
@Configuration
@EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
static class Config {
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
}
}
If use XML, please try:
如果使用 XML,请尝试:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" />
</beans>
You can read http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.introduction