1

I am trying to use elasticsearch in my webapplication. I am using spring boot 2.0.6. I didn't add any configuration file elastic search is auto configured by spring boot. I added spring data elastic search properties in application.properties like this

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  

When i run my application console showing

 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

I added one simple example for demo purpose but i am getting this error

.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}]]

this is example i used

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);
}

Why i am getting error? Is there any other property should i use?

vidy
  • 1,572
  • 6
  • 24
  • 43
  • 2
    see https://stackoverflow.com/questions/25912572/java-elasticsearch-none-of-the-configured-nodes-are-available – Alien Nov 26 '18 at 05:58
  • @Alien thanks that helps my problem solved. I was confused, thought downloading only spring boot elastic search dependancies is only step to connect to elastic search. – vidy Nov 26 '18 at 10:45
  • what was the issue actually? – Alien Nov 26 '18 at 10:47
  • i download elastic search from here https://www.elastic.co/downloads/elasticsearch#ga-release and added config file and it works for me. – vidy Nov 26 '18 at 10:53
  • @Alien i was trying to connect to elasticsearch without installing it. – vidy Nov 26 '18 at 10:58

2 Answers2

1

My problem solved after downloading elasticsearch 5.6.12 from here https://www.elastic.co/downloads/elasticsearch#ga-release

and adding config file

@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());
}
vidy
  • 1,572
  • 6
  • 24
  • 43
0

only doing a wild guess here based on the sparse information without providing any configuration details (please add those for further debugging): If you are using docker, probably your elasticsearch cluster is not available at 127.0.0.1 from your application?

  • Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ). For example like "If your problem is ... then the solution is to .... because .... ." – Yunnosch Mar 19 '22 at 08:48