Unable to cast org.elasticsearch.client.RestClient to org.elasticsearch.client.Client

Hi, In my code I am casting RestClient to Client. I don't see any runtime error but my application is not coming up due to the following error. Can someone help me
org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [classname]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ESClient' defined in URL [<>]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.elasticsearch.client.RestClient cannot be cast to org.elasticsearch.client.Client
Also, I installed ES 7.8.1 version and I do not see org.elasticsearch.client.RestHighLevelClient in my package. Can you please help

Neither RestClient or RestHighLevelClient implement the Client interface. They are different clients with different methods.

You can't cast them like that.

Hmm basically I want to replace TransportClient which implements Client interface. How do I replace it so I can cast to Client.

You cannot do a drop in replacement for TransportClient. The RestHighLevelClient is the closest match, but it is not identical. You will need to make code changes to switch your application to use RestHighLevelClient instead.

Thank Tim. I am using ES 7.8.1 and in my package I do not find org.ealsticsearch.client.RestHighLevelClient. It is missing. I only see RestClient. Can you suggest what I am missing here.

I just see restclient

This might help: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.8/_how_to_migrate.html

Ok. Thanks Tim. Will try adding the dependency

Also how do I check if indicies and mapping exists in RestHighLevelClient. In transportclient which implements we do something like this client.admin().indices().prepareExists(index);
How can I do it using RestHighLevelClient. I don't see any such methods. Please suggest

It sounds like you want client.indices().exists()

Thanks Tim. Yes, right now modifying the code.

Hi Tim, Currently we are using lucene version 6.6.0; do we need to even upgrade lucene to 8.5.1 if we want to upgrade ES to 7.8.1 or it is independent.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.