Setting up an Elasticsearch Node in Java

Using native Java API of the Elasticsearch, you’re able to create several types of nodes: normal Elastic node, embedded Node, node for testing, and remote nodes. In this article, we skim through different types of nodes as well as showing the way of instantiating them.

Elasticsearch Node

There are 2 main libraries for interacting with Elasticsearch cluster using Java.

  • Native Java API
  • Jest

The first one is the one which is used in the source code of the Elasticsearch and is the most standard API to use in Java. Jest is yet another library as a client to the REST api of the Elastic search. In this and following posts, we focus on the native api.

By creating a Node, we want to obtain a Client object to be able to interact with the core. A Node can be either embedded or remote. Before launching the embedded node, you provide some configuration about the cluster name, ports, etc. This can be done using the java API or in a separate YAML file in your classpath – /src/main/resources/elasticsearch.yml

Embedded Node

By writing a few lines of code, you can have your own Embedded node within your application. If you use a same cluster name for other instances of your application in the cluster, Elasticsearch nodes are able to automatically find each other and form a cluster.

Each node is supposed to keep some shards. But, you are also able to launch a node and fetch the client and join the cluster; but keeping no data on the node. This is useful when you want to just use the client for interaction within the cluster.

Node for Unit/Integration Tests

For unit/integration tests, we usually need to create an isolated cluster as fast as possible. We can simply create a local node on JVM level and obtain its client as follows:

Remote

There are situations that because of your business/technical policies, your Elasticsearch cluster are apart from your application nodes. Then, you want to join the cluster remotely. This is how you can do it using TransportClient as follows:

 

Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Leave a Reply

Your email address will not be published. Required fields are marked *