3

I have a Java application that is using ElasticSearch API to connect to Bonsai on Heroku. After deploying my app to Heroku I've found that it will be impossible to connect to a traditional ElasticSearch address 127.0.0.1:9300 via TransportClient.

So I've changed TransportAddress to the one below after reading one of the answers here.

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("http://juniper-325345373.eu-west-1.bonsaisearch.net/"), 80)); 

However now I'm getting a java.net.UnknownHostException: http://juniper-325345373.eu-west-1.bonsaisearch.net/: Name or service not known

How should I correctly define the address in order to connect to Bonsai ElasticSearch?

samba
  • 2,821
  • 6
  • 30
  • 85
  • Did you manage to resolve this? I'd also like to know – wesleyy Dec 29 '18 at 09:12
  • @wesleyy not really. The only solution that I see here for now is to deploy an ElasticSearch Docker image on some hosting. And then call it from Elastic Java API without having to deal with the weird Bonsai host/port issues. – samba Dec 29 '18 at 10:13

1 Answers1

2

I found very userful link on bonsai's web site. https://docs.bonsai.io/article/278-java

1. Extract your account data from URI

e.g. https://a1b2c3d4e:5f6g7h8i9@somehost-1234567.region-x-y.bonsaisearch.net

String username = a1b2c3d4e;
String password = 5f6g7h8i9;

2. Extract host address

String host = somehost-1234567.region-x-y.bonsaisearch.net;
String port = 443;

more information could be found here: https://docs.bonsai.io/article/94-connecting-to-bonsai

3. Implement credentials provider

  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

4. Implement RestHighLevelClient to connect to bonsai

RestHighLevelClient restClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost(host,port,"https"))
           .setHttpClientConfigCallback(httpAsyncClientBuilder -> 
    httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                       .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())));`
Denis_dev
  • 21
  • 2