我正在尝试在我的Web应用程序中使用elasticsearch。我正在使用 spring boot 2.0.6 。我没有添加任何配置文件,弹性搜索是由Spring Boot自动配置的。我在像这样的application.properties中添加了spring数据弹性搜索属性
spring-data-elasticsearch-3.0.11
elasticsearch-5.6.12
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.properties.node.master: true
spring.data.elasticsearch.properties.node.data: false
spring.data.elasticsearch.properties.node.name: my-node
spring.data.elasticsearch.properties.node.attr.type: hot
spring.data.elasticsearch.properties.http.enabled: true
spring.data.elasticsearch.repositories.enabled=true
运行显示
的应用程序控制台时 o.elasticsearch.plugins.PluginsService : no modules loaded
o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
o.s.d.e.c.TransportClientFactoryBean : Adding transport node : 127.0.0.1:9300
我为演示目的添加了一个简单的示例,但出现此错误
.d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{zYXxPcpaQ_6I9GI2yID8cQ}{localhost}{127.0.0.1:9300}]
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loaders':
Invocation of init method failed; nested exception is NoNodeAvailableException
[None of the configured nodes are available: [{#transport#-1}{zYXxPcpaQ_6I9GI2yID8cQ}{localhost}{127.0.0.1:9300}]]
这是我使用的示例
Users.java
@Document(indexName = "users", type = "users", shards = 1, replicas = 0, refreshInterval = "-1")
public class Users {
private String name;
private Long id;
private String teamName;
private Long salary;
Loaders.java
@Component
public class Loaders {
@Autowired
ElasticsearchOperations operations;
@Autowired
UsersRepository usersRepository;
@PostConstruct
@Transactional
public void loadAll(){
operations.putMapping(Users.class);
System.out.println("Loading Data");
usersRepository.saveAll(getData());
System.out.printf("Loading Completed");
}
private List<Users> getData() {
List<Users> userses = new ArrayList<>();
userses.add(new Users("Ajay",123L, "Accounting", 12000L));
userses.add(new Users("Jaga",1234L, "Finance", 22000L));
userses.add(new Users("Thiru",1235L, "Accounting", 12000L));
return userses;
}
}
UsersRepository.java
public interface UsersRepository extends ElasticsearchRepository<Users, Long> {
List<Users> findByName(String text);
List<Users> findBySalary(Long salary);
}
为什么我会出错?我应该使用其他财产吗?
答案 0 :(得分:1)
从这里下载elasticsearch 5.6.12后我的问题解决了 https://www.elastic.co/downloads/elasticsearch#ga-release
并添加配置文件
@Bean
public Client client() throws UnknownHostException {
Settings settings = Settings.builder()
.put("client.transport.sniff", true)
.put("cluster.name", "elasticsearch").build();
@SuppressWarnings("resource")
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(client());
}