Search code examples
javaelasticsearchelasticsearch-java-api

Elasticsearch Java API: Create amd reuse node


Hi,

maybe I'm missing some understanding about the internals of elasticsearch. What I want to do is to create an embedded ES node, but keep the indices persistent across multiple startups of my application.

This is my startup code (executed on each application startup):

Node node = nodeBuilder().build().start();

Then I do some indexing/querying and at some time, the application will stop. Now if I restart the application, all of my data is lost and a new empty node will be created on the fileystem.

Is it possible to reuse a previously created node together with its data using the Java API?


Solution

  • I just found the solution to my problem: The name of the node can be set in the settings. If you do so, the node and its data will be reused on restart.

    ImmutableSettings.Builder settings
                = ImmutableSettings.builder();
    settings.put("node.name", "nameOfTheNode"); // set the name to reuse the node
    settings.put("path.data", "/opt/elasticsearch/data"); // the data will be stored in this folder
    
    Node node = NodeBuilder.nodeBuilder()
            .settings(settings)
            .clusterName("nameOfTheCluser")
            .data(true) // make the node store data
            .node()
            .start();