Mohamed Ali
Mohamed Ali

Reputation: 782

using elasticsearchoperations vs elasticsearchtemplate whats the difference?

I am trying to figure out why I have to set my bean name to elasticsearchTemplate. Without it, my application crashes. I have the code below to configure my Rest client. The issue is if I don't add the elasticsearchTemplate as the bean name, it fails and says it cannot find elasticsearchTemplate. Any idea on why it does this and also what is the difference of using elasticsearchoperations vs elasticsearchtemplate?

Using Spring-Data-Elasticsearch Version 3.2
Using Java High-Level Rest Client Version 6.8.0

Works

@Bean("elasticsearchtemplate")
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

Doesn't Work

public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

Upvotes: 0

Views: 1646

Answers (2)

northmorn
northmorn

Reputation: 31

  1. You can manually configure rest client by extending AbstractElasticsearchConfiguration.
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
  @Override
  public RestHighLevelClient elasticsearchClient() {       
    return RestClients.create(ClientConfiguration.localhost()).rest();
  }
}
  1. what is the difference of using elasticsearchoperations vs elasticsearchtemplate?

The ElasticsearchTemplate is an implementation of the ElasticsearchOperations interface using the Transport Client.
https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RELEASE/reference/html/#elasticsearch.operations.resttemplate

Upvotes: 0

AchillesVan
AchillesVan

Reputation: 4356

Maybe because the startup configuration (application.properties) is missing the configuration related to elasticsearch. You need to define some elastic search properties in your application.properties file such as cluster-nodes, cluster-names which are used by ElasticsearchTemplate and ElasticsearchRepository to connect to the Elasticsearch engine. as follows

Upvotes: 1

Related Questions