Connecting to Elasticsearch Cluster Using Java API

Posted by coffeetechgaff on April 10, 2017

We can use Elasticsearch Client to connect to elasitcsearch. There are Transport Client and Node Client to connect to Elasticsearch cluster. Node is very lowever level client and knows the cluster very well. However, the TransportClient connects remotely to an Elasticsearch cluster using the transport module. It does not join the cluster, but simply gets one or more initial transport addresses and communicates with them in round robin fashion on each action. It is recommended to use Tranport client. After 5.0.0 version, we need to add following dependency for Transport client to make it work along with core jar.

                      
                      
                           org.elasticsearch.client
                           transport
                           5.2.0
                      
                      
                      
                           org.elasticsearch
                           elasticsearch
                           5.2.0
                      
                    

We can initialize the Elasticsearch Client as following using JAVA API. It is not recomended to create new connection for every call.

                      /**
                      * Class to initialized the Elasticsearch client. It will re-use the same client
                      *
                      * @author vsubedi
                      *
                      */
                      public class ElasticsearchClient{

                      private static Client client;

                      /**
                      * Initialized the Elasticsearch Client if the client is not null. If the
                      * client is not null, that client will be used
                      *
                      * @param clustername - clusername
                      * @param ipAddress -ES ip address
                      * @param port -ES port
                      * @return @Client
                      * @throws UnknownHostException
                      */
                      @SuppressWarnings("resource")
                      public static Client getElasticsearchClient(String clustername, String ipAddress, int port) throws UnknownHostException{

                         if(client != null){
                             return client;
                         }

                         Settings settings = Settings.builder().put("cluster.name", clustername).build();
                         client = new PreBuiltTransportClient(settings)
                      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipAddress), port));

                         return client;
                      }

                      public static void closeClient(){
                          if(client != null){
                              client.close();
                           }
                        }
                      }
                    

Before 5.0.0 version (2.* versions), we could use TranportClient that was in core. We could connect to elasticsearch that were before 5.0.0 as follows:

                      public static Client getElasticsearchClient(String clustername, String ipAddress, int port) throws UnknownHostException{

                             if(client != null){
                                  return client;
                             }

                             Settings settings = Settings.builder().put("cluster.name", clustername).build();
                             client = TransportClient.builder().settings(settings).build()
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipAddress), port));

                            return client;
                        }
                      

For more: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html