Connecting to Elasticsearch 6.2.3 with Spring Boot

13,806

Solution 1

Now you can use 6.x. Anyway you can check supported versions here: https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RC3/reference/html/#preface.versions

Solution 2

I am able to connect to the last current Elasticsearch server (6.4.2) using spring-boot-starter-data-elasticsearch version 2.1.0.RC1. Please check the next configuration, I think it should work for 6.2.3.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dino-pocs</groupId>
<artifactId>elasticsearch-spring-boot-integration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Elasticsearch Integration with Spring Boot</name>

<properties>
    <java.version>1.8</java.version>
    <spring.boot.version>2.0.6.RELEASE</spring.boot.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>

    <!-- Wait for the next release version... -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>2.1.0.RC1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${spring.boot.version}</version>
        <scope>test</scope>
    </dependency>

</dependencies>
<repositories>
    <!-- When next release version of spring-boot-starter-data-elasticsearch 
        artifact appears you won't need this repository -->
    <repository>
        <id>spring-libs-snapshot</id>
        <name>Spring Snapshot Repository</name>
        <url>http://repo.spring.io/libs-snapshot</url>
    </repository>
</repositories>
</project>

Configuration class (remember to set basePackage value if you need to and host, port, and cluster name in application.properties or application.yml)

import java.net.InetAddress;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
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 = "<your-base-packages>")
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 settings = Settings.builder().put("cluster.name", esClusterName).build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName(esHost), esPort));
        return client;
    }

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

}

Solution 3

In addition to David's answer here is the list of supported ES

enter image description here

Solution 4

Elasticsearch v6 plus is supported now. Make sure to check the correct versions of spring-data-elasticsearch on your POM.xml

Here are the version support matrix from Spring-data:

spring-data-elasticsearch supported version mapping

Share:
13,806
Michael Draper
Author by

Michael Draper

Updated on June 15, 2022

Comments

  • Michael Draper
    Michael Draper almost 2 years

    Trying to figure out how to get a working config for spring boot to connect to Elasticsearch 6.2.3, here's my connection code:

    pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    

    And my connection code:

    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import java.net.UnknownHostException;
    
    
    @Configuration
    public class ElasticsearchConfig {
    
    @Bean
    public ElasticsearchTemplate elasticsearchTemplate() throws 
    UnknownHostException {
    
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true).build();
    
        // Can't resolve symbol "PreBuiltTransportClient" 
        TransportClient client = new PreBuiltTransportClient(settings);
    
        return new ElasticsearchTemplate(client);
    
    }
    

    }

    • Richa
      Richa about 6 years
      Hey Michael. I have got the same configuration and its works fine. Also I can see Transport address is an interface in 2.0.1.RELEASE.
    • Michael Draper
      Michael Draper about 6 years
      Can you pass me the connection code you use?
    • Michael Draper
      Michael Draper about 6 years
      Or if you can point me to the docs where you found it, that would even be more helpful. It's hard to reconcile the ES API vs. the Spring Data API without some code samples relevant to the versions I'm working with.
  • Michael Draper
    Michael Draper about 6 years
    Thank you very much! Do you have a code sample for the connection API I would use for this set of jars? There have been a few shifts in the connection API for ES, I was struggling getting it to connect which is how I landed on the above versions. Now I'm using the starter, but a few of the classes aren't available (like PreBuiltTransportClient) and TransportAddress is abstract in this version as well.
  • Michael Draper
    Michael Draper about 6 years
    I've upgraded to : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifact‌​Id> <version>2.0.1.RELEASE</version> </dependency> No joy :(
  • shazin
    shazin about 6 years
    What is the Spring Boot version you are using?
  • Michael Draper
    Michael Draper about 6 years
    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent>
  • Michael Draper
    Michael Draper about 6 years
    TransportClient and TransportAddress are both elasticsearch objects though
  • Prometheus
    Prometheus almost 5 years
    This answer is no longer up to date. Now it is supported.
  • Federico Piazza
    Federico Piazza over 4 years
    Link does not exist anymore