Eugene Goldberg
Eugene Goldberg

Reputation: 15544

How to properly connect Spring Boot app to Elasticsearch 6.1?

I'm working on a Spring Boot app, which needs to connect to a remote Elasticsearch 6.1 For some reason, when the app is started, it defaults to a local ES connection, ignoring the settingsBuilder.

Here is my ElasticConfiguration.java:

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


    @Bean
    public NodeBuilder nodeBuilder() {
        return new NodeBuilder();
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws IOException {
        File tmpDir = File.createTempFile("elastic", Long.toString(System.nanoTime()));
        System.out.println("Temp directory: " + tmpDir.getAbsolutePath());
        Settings.Builder elasticsearchSettings =
                Settings.settingsBuilder()
                        .put("network.host","34.230.72.180")
                        .put("cluster.name", "my-es-cluster")
                        .put("http.enabled", "true") // 1
                        .put("index.number_of_shards", "1")
                        .put("path.data", new File(tmpDir, "data").getAbsolutePath()) // 2
                        .put("path.logs", new File(tmpDir, "logs").getAbsolutePath()) // 2
                        .put("path.work", new File(tmpDir, "work").getAbsolutePath()) // 2
                        .put("path.home", tmpDir); // 3



        return new ElasticsearchTemplate(nodeBuilder()
                .local(false)
                .settings(elasticsearchSettings.build())
                .node()
                .client());
    }
}

And here is my pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.techprimers.elastic</groupId>
    <artifactId>spring-elastic-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-elastic-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.0.9</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

How can I force the app to connect to my remote ES server?

Upvotes: 1

Views: 3838

Answers (2)

G&#246;khan Ayhan
G&#246;khan Ayhan

Reputation: 1299

You can't use NodeBuilder with elasticsearch 6, read this , use Node(Settings) and upgrade spring boot version to "2.0.1.RELEASE" etc.

Upvotes: 0

riccardo.cardin
riccardo.cardin

Reputation: 8353

I don't know if you resolved your connection problem, but standing on the compatibility matrix in the Spring Data Elasticsearch Github project page, you cannot.

| spring data elasticsearch | elasticsearch | |:-----------------------------------:|:-------------:| | 3.0.0.RC2 | 5.5.0 | | 3.0.0.M4 | 5.4.0 | | 2.0.4.RELEASE | 2.4.0 | | 2.0.0.RELEASE | 2.2.0 | | 1.4.0.M1 | 1.7.3 | | 1.3.0.RELEASE | 1.5.2 | | 1.2.0.RELEASE | 1.4.4 | | 1.1.0.RELEASE | 1.3.2 | | 1.0.0.RELEASE | 1.1.1 |

That page is out-dated. However, I have just downloaded the release 2.1.9 of Spring Data Elasticsearch and the Elasticsearch jar I have is my .m2 repository in 2.4.6.

Upvotes: 1

Related Questions