Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Elastic search in a Spring Boot application

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?

Actually, I tried adding this options to my application.properties file:

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: []

like image 849
jemlifathi Avatar asked Feb 27 '26 05:02

jemlifathi


1 Answers

I got this answer from the ES forum and it worked for me:

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

I made these changes and it worked pretty perfect.

like image 116
jemlifathi Avatar answered Feb 28 '26 17:02

jemlifathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!