2

Am new to ElasticSearch...

Really love the API (especially ElasticsearchTemplate & supporting Unit Tests)...

Was following this example using ElasticSearch 5.5.0 (the following link has the full code inline and also available as a downloadable zip file):

https://www.mkyong.com/spring-boot/spring-boot-spring-data-elasticsearch-example/

maven dependencies:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

EsConfig:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.mkyong.book.repository")
public class EsConfig {

    @Value("${elasticsearch.host}")
    private String EsHost;

    @Value("${elasticsearch.port}")
    private int EsPort;

    @Value("${elasticsearch.clustername}")
    private String EsClusterName;

    @Bean
    public Client client() throws Exception {

        Settings esSettings = Settings.settingsBuilder()
                .put("cluster.name", EsClusterName)
                .build();

        //https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
        return TransportClient.builder()
                .settings(esSettings)
                .build()
                .addTransportAddress(
                  new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }
}

src/main/resources:

elasticsearch.clustername = mkyong-cluster
elasticsearch.host = localhost
elasticsearch.port = 9300

When issuing the following, either:

mvn spring-boot:run

or

mvn clean test

stdout:

Caused by: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]]
   at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:326)
   at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:223)
   at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)

Also, get the following in my ElasticSearch 5.5.0 engine's stdout:

[2017-07-23T02:48:46,135][WARN ][o.e.t.n.Netty4Transport  ] [vY7jxpr] exception caught on transport layer [[id: 0xa7e950be, L:/127.0.0.1:9300 - R:/127.0.0.1:60190]], closing connection
    java.lang.IllegalStateException: Received message from unsupported version: [1.0.0] minimal compatible version is: [5.0.0]
        at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport.java:1379) ~[elasticsearch-5.5.0.jar:5.5.0]
        at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channelRead(Netty4MessageChannelHandler.java:74) ~[transport-netty4-5.5.0.jar:5.5.0]    

Is there any way to use Spring Data ElasticSearch with ElasticSearch 5.5.0 engine?

If so, how would the connection be implemented?

I am using the Elastic Search Java client provided by Elastic Search and am able to connect and create indices on ElasticSearch 5.5.0 but really wish I could use the power of this library.

1 Answer 1

2

Spring Boot 1.5.1 does not support ElasticSearch 5.x (the article you provided also says it). To make it possible to work with ElasticSearch 5.x you need to use a milestone of Spring Boot 2.

Try to configure your project with following dependencies:

# spring boot
compile 'org.springframework.boot:spring-boot:2.0.0.M2'

# elasticsearch
compile 'org.elasticsearch:elasticsearch:5.5.0'
compile 'org.elasticsearch.client:transport:5.5.0'

# spring-data-elasticsearch for spring boot
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.0.0.M2'

This should allow you to use all the goodies of Elasticsearch Java API ans spring-data-elasticsearch.

Update: Basic Gradle (v4.0.1) configuration would look like (build.gradle file):

buildscript {
    ext {
        springBootVersion = '2.0.0.M2'
    }
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/libs-snapshot' }
        maven { url 'http://repo.spring.io/milestone/' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

apply plugin: 'java-library'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

jar {
    baseName = 'elastic'
    version = '0.0.1'
}

repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://repo.spring.io/libs-snapshot' }
    maven { url 'http://repo.spring.io/milestone/' }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-logging'

    compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

    compile 'org.elasticsearch:elasticsearch:5.4.1'
    compile 'org.elasticsearch.client:transport:5.4.1'

    compile 'com.google.guava:guava:20.0'

    testCompile 'junit:junit:4.12'
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! This is good news but I don't know how to configure this with my Maven pom.xml... Also, I can't seem to find where in the article it mentioned Spring Boot 1.5.1 has lack of ElasticSearch 5.x. support.
At the beginning of the article there is a Note section: SpringBoot 1.5.1.RELEASE and Spring Data Elasticsearch 2.10.RELEASE supports only ElasticSearch 2.4.0. They don’t support the latest version of ElasticSearch 5.x version... I use mostly gradle for dependency management - I updated my answer with the configuration so it's easier to translate it to maven pom. I recomend start.spring.io for generating starter of pom file - it wil also generate the right repositories for milestones as they are not present in cental repository.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.