scalaelasticsearchupgradedeprecatedresthighlevelclient

class RestHighLevelClient in package client is deprecated


I am upgrading elastic search from 7.9.2 to 7.17.6 and then to 8.4.2, in the first step i am upgrading the es cluster to the 7.17.6 version , and compiling all other ES clients, the compilation is good for all the other clients besides one client ( a scala 2.12.11 elastic search client), I got the error

RestManagedServices.scala:32:26: class RestHighLevelClient in package client is deprecated

here is the definition of the elasticConnector class:

package com.carrefour.phenix.support

import java.io.Closeable
import com.typesafe.config.Config
import org.apache.http.HttpHost
import org.apache.http.auth.{ AuthScope, UsernamePasswordCredentials }
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client._
import org.slf4j.LoggerFactory

/**
 *
 */
class ElasticConnector(val config: ElasticConfiguration) extends Closeable {

  def this(conf: Config) = this(new ElasticConfiguration(conf))

  val logger = LoggerFactory.getLogger(classOf[ElasticConnector].getName)

  import scala.language.{ implicitConversions, postfixOps }

  val hosts = config.hosts.map { host ⇒
    new HttpHost(host.getString("host"), host.getInt("port"), config.scheme)
  }.toList

  lazy val esClientBuilder: RestClientBuilder = RestClient.builder(hosts: _*)

  //configure authentication if provided
  (config.user, config.password) match {
    case (Some(user), Some(password)) ⇒ {
      val credentialsProvider = new BasicCredentialsProvider
      credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password))
      esClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
        override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
      })
    }
    case _ ⇒
  }

  val esRestClient = esClientBuilder.build()
  val esVersion = getElasticsearchVersion(esRestClient)

  lazy val esHighLevelClient: RestHighLevelClient = new RestHighLevelClientBuilder(esRestClient)
    .setApiCompatibilityMode(esVersion >= 8)
    .build()

  override def close(): Unit = {
    esHighLevelClient.close()
  }

  private def getElasticsearchVersion(restClient: RestClient): Int = {
    try {
      val response = new RestHighLevelClientBuilder(restClient).build.info(RequestOptions.DEFAULT)
      val strVersion = response.getVersion.getNumber
      strVersion.split("\\.").head.toInt
    } catch {
      case e: Exception ⇒
        logger.warn("Failed to get ES server version", e)
        -1
    }
  }

}

and the client class where the error is raised:

package com.carrefour.phenix.customer_case.api

import com.carrefour.phenix.core.configuration.Configuration
import com.carrefour.phenix.customer_case.api.services.{ AfterSalesServiceConfiguration, CustomerCaseSearchService, RefundReceiptSearchService }
import com.carrefour.phenix.rest.app.{ LogSupport, ManagedServices }
import com.typesafe.config.Config

import scala.util.Try
import java.io.Closeable
import com.carrefour.phenix.support.{ ElasticConfiguration, ElasticConnector }
import com.google.cloud.bigtable.data.v2.{ BigtableDataClient, BigtableDataSettings }
import org.elasticsearch.client.RestHighLevelClient

final class RestManagedServices(val customerCaseSearchConfig: AfterSalesServiceConfiguration, bigtableSettings: BigtableDataSettings)
    extends ManagedServices with LogSupport {

  val provider = new RestManagedServicesProvider(customerCaseSearchConfig.elasticConf)

  val customerCaseSearch = new CustomerCaseSearchService(provider.elasticConnector.esHighLevelClient, customerCaseSearchConfig)

  lazy val bigtableDataClient: BigtableDataClient = BigtableDataClient.create(bigtableSettings)
  lazy val refundReceiptService = new RefundReceiptSearchService(customerCaseSearchConfig, bigtableDataClient)

  override def releaseServices(): Unit = {
    log.info("Shutting down customers-after-sales-service-api services.")
  }
}

class RestManagedServicesProvider(config: Config) extends Configuration(config) with Closeable {

  val elasticConnector = new ElasticConnector(new ElasticConfiguration(config))
  val esHighLevelClient: RestHighLevelClient = elasticConnector.esHighLevelClient

  override def close(): Unit = {
    Try(elasticConnector.close)
  }
}

By the way I need the method search and all other methodes defined in es 7.9.2 and still supported by 7.17.6 and then 8.4.2.

The official doc of es explains that the rest high level clients will be used for an ES cluster under 8.x version when setting setApiCompatibilityMode to true , which is my case here while defining the es connector class:

REST API compatibility workflow

Please a call to all Es experts and scala lover , please help me


Solution

  • The idea was to impose no deprecation warning, it solved the problem