Arun
Arun

Reputation: 1085

Connect springboot java RestHighLevelClient to aws elasticsearch

Config:

private static final String HOST = "search-qas-sam-zxbalglrksamjfcazl3zmkiiq7.ap-south-1.es.amazonaws.com";
private static final int PORT = 443; // tries 9200, 9300, 80
private static final String PROTOCOL = "https";

@Bean
public RestHighLevelClient client() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("myusername", "mypassword"));

    RestClientBuilder builder = RestClient.builder(new HttpHost( HOST, PORT, PROTOCOL))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
            });

When connecting to ElasticSearch on my local box, running on localhost and port 9200, it works.

But when trying to connect to AWS, it gives the following error:

Caused by: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost:9200'

Also, if I open my AWS ElasticSearch in the browser, its status appears as:

{
  "name" : "asjsbsghjmnkl1f05283a21d38yh9ij",
  "cluster_name" : "5583974864:qas",
  "cluster_uuid" : "8wWV8ujji9kbzg",
  "version" : {
    "number" : "7.10.2",
    "build_flavor" : "oss",
    "build_type" : "tar",
    "build_hash" : "unknown",
    "build_date" : "2021-04-21T04:22:10.395481Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Using spring-boot 2.4.5 with spring-data-elasticsearch:

implementation ('org.springframework.data:spring-data-elasticsearch') {
    exclude group: 'org.apache.lucene'
}

Before posting this, I tried the following approaches:

But none of them worked.

Upvotes: 1

Views: 4037

Answers (3)

you code work for me with AWS elasticsearch 7.10 over https with springboot, i used the next configuration in my ms:

Java version = 11

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.2.2.RELEASE</version>
</dependency>

thank you friend.

Upvotes: 0

aditya verma
aditya verma

Reputation: 36

There are two options:

  1. Java Config:
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.arun.elasticsearch.repository")
@ComponentScan(basePackages = { "com.arun.elasticsearch.service" })
public class ElasticSearchConfig {
    private static final String HOST = "aws_host"; // localhost
    private static final int PORT = 443; // 9092
    private static final String PROTOCOL = "https"; // http

    @Bean
    public RestHighLevelClient client() {
       final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("UserName", "Password"));

        RestClientBuilder builder = RestClient.builder(new HttpHost( HOST, PORT, PROTOCOL))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });
    }
  1. Spring yml:
elasticsearch:
  rest:
    uris: https://MyHost:9200
    username: arun
    password: mypassword

Upvotes: 2

Abacus
Abacus

Reputation: 19421

Please check the Spring Data Elasticsearch documentation, especially about

Upvotes: 0

Related Questions