1

am trying to configure elasticsearch in Spring boot but Bean instantiation is failing, Node Builder has been removed from elasticsearch api so am trying to use Settings.Builder but its not helping below is the code:

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.demo.elastic.elasticdemo.repository")
public class ElasticConfiguration {

@SuppressWarnings("resource")
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws IOException {

    File tempDir = File.createTempFile("temp-elastic", Long.toString(System.nanoTime()));
    System.out.println("Temp directory: "+ tempDir.getAbsolutePath());

    Settings.Builder settings = Settings.builder()

            //http settings
            .put("http.enable", "true")
            .put("http.cor.enable", "true")
            .put("http.cors.allow-origin", "https?:?/?/localhost(:[0-9]+)?/")
            .put("http.port", "9200")

            //transport settings
            .put("transport.tcp.port", "9300")
            .put("network.host", "localhost")

            //node settings
            .put("node.data", "true")
            .put("node.master", "true")

            //configuring index
            .put("index.number_of_shards", "1")
            .put("index.number_of_replicas", "2")
            .put("index.refresh_interval", "10s")
            .put("index.max_results_window", "100")

            //configuring paths
            .put("path.logs", new File (tempDir, "logs").getAbsolutePath())
            .put("path.data", new File (tempDir, "data").getAbsolutePath())
            .put("path.home", tempDir);
            //.build();

    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));

    return new ElasticsearchTemplate(client);
}
}

what am i doing wrong.??

error am getting:

2018-09-27 19:23:35.825 ERROR 57876 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loaders': Unsatisfied dependency expressed through field 'operations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [com/demo/elastic/elasticdemo/config/ElasticConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.core.ElasticsearchOperations]: Factory method 'elasticsearchTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/transport/Netty3Plugin
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
salsinga
  • 1,957
  • 1
  • 14
  • 26
Rakesh
  • 11
  • 1
  • 3
  • 3
    Please also add error that you are getting. – nitzien Sep 27 '18 at 14:14
  • 1
    Welcome to Stackoverflow. Please edit your question to clarify your problem description. Tell what you tried to solve the problem, add some tags if possible. This might help you finding answers. For guidance please read [how to ask questions](https://stackoverflow.com/help/how-to-ask) and [how to create a minimal example](https://stackoverflow.com/help/mcve) – 5th Sep 27 '18 at 14:46
  • hi nitzien, attached the error am getting – Rakesh Sep 27 '18 at 15:04
  • Possible duplicate of [Caused by: java.lang.ClassNotFoundException: org.elasticsearch.transport.Netty3Plugin](https://stackoverflow.com/questions/51039938/caused-by-java-lang-classnotfoundexception-org-elasticsearch-transport-netty3p) – g00glen00b Sep 28 '18 at 07:38
  • its not the same, i already tried adding the dependencies, nothing changes – Rakesh Sep 28 '18 at 14:25

0 Answers0