1

I trying to access 2 Elasticsearch Server instance through HighLevelRestClient but I couldn't through this array object

HttpHost[] httpHost = new HttpHost(hostName[i...], Integer.parseInt(hostName[i..]), "http");

In hostname i have 2 values in the restHighLevelClient = new RestHighLevelClient( RestClient.builder(httpHost));

I'm also unable to access through second array instance.

Can I have 2 configuration Class if such way a how to create 2 instance of HighLevelRestClient

Or is it any possible way through 2 bean instance if such a way how it is possible

Since we need to have 2 different restHighLevelClient instance.

Kindly let me know in case of more information needed.

enter image description here

Code

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppElasticSearchConfiguration extends AbstractFactoryBean<RestHighLevelClient> {

    private static final Logger LOG = LoggerFactory.getLogger(AppElasticSearchConfiguration.class);

    @Value("${application.elasticsearch.host}")
    private String hostName[];

    private RestHighLevelClient restHighLevelClient;

    @Override
    public void destroy() {
        try {
            if (restHighLevelClient != null) {
                restHighLevelClient.close();
            }
        } catch (final Exception e) {
            LOG.error("Error closing ElasticSearch client: ", e);
        }
    }

    @Override
    public Class<RestHighLevelClient> getObjectType() {
        return RestHighLevelClient.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public RestHighLevelClient createInstance() {
        return buildClient();
    }

    private RestHighLevelClient buildClient() {
        try {
            HttpHost[] httpHost = null;
            if(hostName!=null) {
                httpHost = new HttpHost[hostName.length];
                for (int i = 0; i < httpHost.length; i++) {
                    httpHost[i] = new HttpHost(hostName[i].split(":")[0], 
                                            Integer.parseInt(hostName[i].split(":")[1]), "http");               
                }
            }
            restHighLevelClient = new RestHighLevelClient( RestClient.builder(httpHost));

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return restHighLevelClient;
    }


    //public RestHighLevelClient getAppRestHighLevelClient() { return restHighLevelClient; }
}
Arun
  • 1,010
  • 6
  • 18
  • 37
  • Just to be sure: This is a single cluster consisting of 2 (or hopefully 3 at least) instances and you want to add multiple hosts in the connection string? – xeraa Sep 16 '19 at 09:19
  • 2 separate different host and instance – Arun Sep 18 '19 at 06:03
  • This doesn't answer the question. Is this two different Elasticsearch clusters or a single Elasticsearch cluster with two hosts? – xeraa Sep 18 '19 at 06:07
  • It is two different Elasticsearch clusters – Arun Sep 18 '19 at 09:44
  • Yes, you will need to instantiate two [`RestHighLevelClient`](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-getting-started-initialization.html). Could be two configuration classes or however you want to instantiate them. – xeraa Sep 18 '19 at 17:49
  • If we have 2 configuration class how can we access RestHighLevelClient restHighLevelClient, I hope this is a shared object, how it can access two different Elasticsearch clusters? any example available ? – Arun Sep 23 '19 at 05:43
  • I'm not sure I follow the question about the shared object. You'll need two different `AppElasticSearchConfiguration` classes (at least with your current setup) — how should the configuration otherwise know which connection it should use? – xeraa Sep 24 '19 at 20:17

1 Answers1

1

Hi Just pass the secondary instance in the constructor of HttpHost.

@Bean(destroyMethod = "close")
public RestHighLevelClient buildClient() {

    RestClientBuilder builder = RestClient.builder(new HttpHost(hostPrimary, Integer.valueOf(portPrimary), "http"),
            new HttpHost(hostSecondary, Integer.valueOf(portSecondary), "http"));
    RestHighLevelClient client = new RestHighLevelClient(builder);
    LOG.info("RestHighLevelClient has been created with:{}", client);

    return client;
}
Chirag Gupta
  • 115
  • 1
  • 7