This project runs Elasticsearch cluster on one JVM instance for your development/testing easily. You can use Elasticsearch Cluster Runner as Embedded Elasticsearch in your application.
Put elasticsearch-cluster-runner if using Maven:
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>elasticsearch-cluster-runner</artifactId>
<version>x.x.x.0</version>
</dependency>
and add Maven repository to pom.xml:
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>codelibs.org</id>
<name>CodeLibs Repository</name>
<url>https://maven.codelibs.org/</url>
</repository>
</repositories>
import static org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs;
...
// create runner instance
ElasticsearchClusterRunner runner = new ElasticsearchClusterRunner();
// create ES nodes
runner.onBuild(new ElasticsearchClusterRunner.Builder() {
@Override
public void build(final int number, final Builder settingsBuilder) {
// put elasticsearch settings
// settingsBuilder.put("index.number_of_replicas", 0);
}
}).build(newConfigs());
build(Configs) method configures/starts Clsuter Runner.
// close runner
runner.close();
// delete all files(config and index)
runner.clean();
Put elasticsearch-cluster-runner as test scope:
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>elasticsearch-cluster-runner</artifactId>
<version>x.x.x.0</version>
<scope>test</scope>
</dependency>
and see ElasticsearchClusterRunnerTest.
Download and install Maven 3 from https://maven.apache.org/.
git clone https://github.com/codelibs/elasticsearch-cluster-runner.git
mvn compile
Run:
mvn exec:java
The default cluster has 3 nodes and the root directory for Elasticsearch is es_home. Nodes use 9201-9203 port for HTTP and 9301-9303 port for Transport. If you want to change the number of node, Run:
mvn exec:java -Dexec.args="-basePath es_home -numOfNode 4"
Type Ctrl-c or kill the process.