Skip to content
Advertisement

Multiple Elasticsearch connections in a program

I was working on a service in which I get a searchHit from elastic cluster A and then use a field obtained from A to fetch details from elastic cluster B. I have created a class ClientFactory, which creates ES client based on values that I send. (A’s or B’s) But after getting details from A, I’m not able to create connection to B. I can either fetch details from A or B at one time. Any idea how to solve this problem?

Advertisement

Answer

You can create two resthighlevelclient one which talks to cluster A and another which talks to cluster B , below is the sample code to show :

Create client A

 RestHighLevelClient restHighLevelClientA = new RestHighLevelClient(
                RestClient.builder(new HttpHost(configuration.getClusteAConfig().getHost(),
                        configuration.getClusteAConfig().getPort(),
                        "http")));

Create client B

 RestHighLevelClient restHighLevelClientB = new RestHighLevelClient(
                RestClient.builder(new HttpHost(configuration.getClusteBConfig().getHost(),
                        configuration.getClusteBConfig().getPort(),
                        "http")));

You should have 2 configurations for cluster A and B, which is read by client creation code.

ClusterA:
  host: cluste A hosts
  port: 9200

ClusterB:
  host: cluste B hosts
  port: 9200
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement