Search code examples
javaelasticsearch

Migrate old deprecated methods in elasticsearch


I'm trying to migrate elasticsearch:5.6.15 to elasticsearch:7.17.18. I have the following issues:

TransportClient client;
DeleteByQueryRequestBuilder deleteRequestBuilder = DeleteByQueryAction.INSTANCE.newRequestBuilder(client);
deleteRequestBuilder.source().setIndices(index);

Error:

Cannot resolve method 'newRequestBuilder' in 'DeleteByQueryAction'

Second issues is:

Retry retryRequest = Retry.on(Exception.class)

Error:

Cannot resolve method 'on' in 'Retry'

Third issue:

Map<String, Object> indexMapping;
CreateIndexRequest request = new CreateIndexRequest(index);
request.source(indexMapping);

Required type: XContentBuilder
Provided:Map<java.lang.String,java.lang.Object>

Do you know how I can migrate the code accordingly?

P.S I created this test project to show migration issues:

https://github.com/rcbandit111/elasticsearch_migration_poc/blob/main/src/main/java/com/test/portal/account/platform/service/WebhookPlatformClient.java

Please clone it and run it.


Solution

  • Elasticsearch has already deprecated Java REST Client in favor of Java API Client. So you should migrate to Java API Client.

    For your code, you can use the following code update for the issues.

    1. Use a constructor instead of newRequestBuilder
    DeleteByQueryRequestBuilder delete = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE);
    
    1. Use constructor instead of Retry.on
    BackoffPolicy backoff = BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(firstRetryWaitTimeInMillis), noOfRetries);
    Retry retryBulkRequest = new Retry(backoff, client.threadPool());
    BulkResponse response = retryBulkRequest.withBackoff(
                (bulkReq, bulkResponseActionListener) -> client.bulk(bulkReq, bulkResponseActionListener),
                bulkRequest.request()).actionGet();
    BulkResponseDto responseDto = new BulkResponseDto();
    responseDto.setTimeTakenInMilli(response.getIngestTookInMillis());
    
    1. Pass DeprecationHandler as the second argument.
    request.source(indexMapping, DeprecationHandler.IGNORE_DEPRECATIONS);
    

    The above changes will solve the compilation issues. You can look for a migration guide or Elasticsearch Github for changes in different versions.