Error at createIndex elasticsearch using ElasticsearchOperations. Why is the "BytesReference class was expected" exception happening?

Viewed 2268

I used org.elasticsearch:elasticsearch version 6.8 and for connectivity I used TransportClient. I then decided to upgrade to version 7.6 and since TransportClient is Deprecated, I had to use the connection to elastic via the RestHighLevelClient. After this, the following error appeared after the start:

java.lang.IncompatibleClassChangeError: Found interface org.elasticsearch.common.bytes.BytesReference, but class was expected
at org.elasticsearch.client.RequestConverters.createEntity(RequestConverters.java:689)
at org.elasticsearch.client.RequestConverters.createEntity(RequestConverters.java:684)
at org.elasticsearch.client.IndicesRequestConverters.createIndex(IndicesRequestConverters.java:128)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1760)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1734)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1696)
at org.elasticsearch.client.IndicesClient.create(IndicesClient.java:191)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.createIndex(ElasticsearchRestTemplate.java:1248)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.createIndex(ElasticsearchRestTemplate.java:1256)

It appears on the index creation line:

elasticsearchOperations.deleteIndex(clazz);
elasticsearchOperations.createIndex(clazz, loadFromFileSetting(pathSetting, someParams));

I use the following config class for connection:

@Slf4j
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.project.elastic.repository")
public class ElasticConfig extends AbstractElasticsearchConfiguration {

    @Value("${spring.data.elastic.url}")
    private String url;

    @Override
    public RestHighLevelClient elasticsearchClient() {
        return RestClients.create(ClientConfiguration.create(url)).rest();
    }
}

What am I doing wrong?

2 Answers

I would like to supplement Opster Elasticsearch Ninja`s answer. The problem described here has been solved by implementation "org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2" in your gradle.build. After that you should indicate the index type as _doc and make sure that your java client version matches your Elasticsearch version.

The issue was resolved by using the _doc in the Elasticsearch API as types are deprecated in Elasticsearch 7.X version and only _doc type is allowed for backward compatibility purpose and will be totally removed in Elasticsearch 8.X.

More information on removal of types Elasticsearch official documentation.

Related