Search code examples
gradleelasticsearch-7

Cannot instantiate interface 'ElasticsearchClient'


The official Elasticsearch docs tell to instantiate ElasticsearchClient like this:

ElasticsearchClient client = new ElasticsearchClient(transport);

Once I write this in my Grails 3 application with Gradle build management, I get the following compilation error: Cannot instantiate interface 'ElasticsearchClient'

The import statement is: import org.elasticsearch.client.ElasticsearchClient. Indeed, ElasticsearchClient gets resolved to:

package org.elasticsearch.client;
// ...
public interface ElasticsearchClient {

The Gradle dependency is: compile 'org.elasticsearch:elasticsearch:7.17.2'

  1. Why do they propose to instantiate an interface in their docs?
  2. What can I do to make it compile and use the Elasticsearch client?

Solution

  • The reason for this was: ElasticsearchClient was mis-resolved to an old elasticsearch JAR file that had still been in my local user's .gradle directory. Removing the old JAR from .gradle enabled me to do the correct import.

    The correct import is import co.elastic.clients.elasticsearch.ElasticsearchClient instead of import org.elasticsearch.client.ElasticsearchClient