Cannot use RestHighLevelClient.performRequest() because of protected access


(Mike) #1

I'm currently in the process of switching our system from using the TransportClient, to the RestHighLevel client. We are using the Rest Client v5.6 and following these instructions for migrating: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_changing_the_application_8217_s_code.html#java-rest-high-level-migration-manage-indices

I copied the code from the migration guide into my configuration object, but there's one method that I can't get to from my class.
My problem is, the following line (that is copied from the migration guide) is showing an error in IntelliJ:
Response response = restClient.performRequest("PUT", "my-index", emptyMap(), entity);

The error I see is:
'performRequest()' has protected access in 'org.elasticsearch.client.RestHighLevelClient'
I have confirmed that RestHighLevelClient.performRequest() does have protected access.

Am I misunderstanding how to use the example code?

Here is my config file with the problem code:

package com.sample.web.config;

import org.apache.http.*;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;

@Configuration
public class ElasticSearchConfiguration {
@Value("${elasticsearch.cluster.name:elasticsearch}")
private String esClusterName;
@Value("${elasticsearch.host:localhost}")
private String esHost;
@Value("${elasticsearch.rest.port}")
private Integer esPort;

@Bean
public RestHighLevelClient createESRestHighLevelClient()  throws IOException{
    RestClient lowLevelRestClient = getLowLevelRestClient();
    RestHighLevelClient restClient = new RestHighLevelClient(lowLevelRestClient);

    Settings indexSettings = Settings.builder()
            .put("cluster.name", esClusterName)
            .build();

    String payload = XContentFactory.jsonBuilder()
            .startObject()
            .startObject("settings")
            .value(indexSettings)
            .endObject()
            .startObject("mappings")
            .startObject("doc")
            .startObject("properties")
            .startObject("time")
            .field("type", "date")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject().string();

    HttpEntity entity = new NStringEntity(payload, ContentType.APPLICATION_JSON);

    Response response = restClient.performRequest("PUT", "my-index", emptyMap(), entity);
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

    }
    return restClient;
}

private RestClient getLowLevelRestClient() {
    return RestClient.builder(
            new HttpHost(esHost, esPort, "http")).build();
}

}

(Mike) #2

Solved. The restClient object in the migration guide is apparently a Low Level Rest Client, not a High Level Rest Client. Switching it fixed the issue.