javaspringelasticsearchresthighlevelclient

Java Data Elasticsearch client appending port 9200 to url


Spring Data Elasticsearch version:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>3.2.6.RELEASE</version>
</dependency>

I do not understand why my Elasticsearch Highlevel client is always forcing the port to 9200 even though I am specifying the port 443.

Here is how I am defining the RestHighLevelClient:

@Slf4j
@Configuration
public class ElasticsearchConfiguration {

    @Value("${elasticsearch.host:127.0.0.1}")
    private String elasticsearchHost;

    @Value("${elasticsearch.port:9200}")
    private String elasticsearchPort;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() throws IOException {
        log.info("Creating High level rest client for Elasticsearch with host: " + elasticsearchHost);
        ClientConfiguration configuration = ClientConfiguration.builder()
                    .connectedTo(elasticsearchHost + ":" + elasticsearchPort)
                    .usingSsl()
                    .build();

        return RestClients.create(configuration).rest();
    }

    @Bean
    public ElasticsearchRestTemplate getElasticsearchTemplate() throws IOException {
        return new ElasticsearchRestTemplate(client());
    }
}

and how I have used the template:

private ElasticsearchRestTemplate elasticsearchTemplate;

@Autowired
public ElasticsearchServiceImpl(@Qualifier("getElasticsearchTemplate") ElasticsearchRestTemplate elasticsearchRestTemplate) {
    this.elasticsearchTemplate = elasticsearchRestTemplate;
}

@PostConstruct
public void init() throws IOException {
    create indexes...
}

I have verified that the host and the port are both set in the properties file:

--elasticsearch.host=https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com
--elasticsearch.port=443

In the stacktrace error I am receiving: Caused by: java.io.IOException: https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com:443:9200, it appears to be appending 9200 to the end of the url. Why is this happening?

I have verified that the connection is successfully made via curl.

Edit: Added another stacktrace

un 09 21:44:44 ip-10xxxx95.pre.xxx-prototype.xxx.uk bash[19738]: 2020-06-09 21:44:44.023 ERROR 19738 --- [           main] c.g.c.s.impl.ElasticsearchServiceImpl    : https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com: Name or service not known
Jun 09 21:44:44 ip-10-2xxxxx5.pre.xxx-prototype.xxx.uk bash[19738]: 2020-06-09 21:44:44.024 ERROR 19738 --- [           main] c.g.c.s.impl.ElasticsearchServiceImpl    : [org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:964), org.elasticsearch.client.RestClient.performRequest(RestClient.java:233), org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1764),

Solution

  • Can you try creating the resthighleval client like below :

    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost(configuration.getElasticsearchConfig().getHost(),
                            configuration.getElasticsearchConfig().getPort(),
                            "https")));
    

    And use the below config, note I left the port config empty:

    --elasticsearch.host=https://vpc-example-ryphgjfwji3zonliyhhd3nu.eu-west-2.es.amazonaws.com --elasticsearch.port=

    I've created a connection to AWS ES and ES hosted in AWS in past and let me know if you face issue with code, would be happy to help further.