Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Rest Client with Spring Data Elasticsearch

I am in a situation where I am using Spring boot and AWS elasticsearch service. AWS Elasticsearch service which only provides REST interface.

Elasticsearch Rest Client is here.

Simply, Is it possible to use REST client with Spring Data Elasticsearch?

In other words, Does Spring Data Elasticsearch works with Elasticsearch Rest client?

Spring Data Elasticsearch is very easy to use and template provides very most functionality that I need. With Elasicsearch Rest client I have to implement all the functionality myself.

like image 644
abcdef12 Avatar asked Jul 17 '17 01:07

abcdef12


People also ask

Does spring data support Elasticsearch?

The Spring Data Elasticsearch project provides integration with the Elasticsearch search engine. Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with a Elastichsearch Documents and easily writing a Repository style data access layer.

Why ElasticsearchTemplate is deprecated?

The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7. + Users should switch to ElasticsearchRestTemplate or ReactiveElasticsearchTemplate .

How do I use Resthighlevelclient?

Search Documents First, create a SearchRequest passing the index name as the argument. After that, SearchSourceBuilder needs to be created. Add to it the query you want to execute. Lastly, execute the SearchRequest through the REST client.


2 Answers

[2020 May Update]

https://spring.io/blog/2020/05/27/what-s-new-in-spring-data-elasticsearch-4-0

As you can read Spring Data Elasticsearch 4.0:

Spring Data Elasticsearch now uses Elasticsearch 7, 7.6.2 in particular. Elasticsearch clusters running on 6.x versions are not supported anymore. The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7.+ Users should switch to ElasticsearchRestTemplate or ReactiveElasticsearchTemplate.

[2019 February Update]

A see now that 3.2.0 M1 Spring Data Elasticsearch supports the HTTP client (https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.M1/reference/html/#reference)

According to the documentation (it could of course change because it's not final version so I will put it here):

The well known TransportClient is deprecated as of Elasticsearch 7.0.0 and is expected to be removed in Elasticsearch 8.0.

2.1. High Level REST Client

The Java High Level REST Client provides a straight forward replacement for the TransportClient as it accepts and returns the very same request/response objects and therefore depends on the Elasticsearch core project. Asynchronous calls are operated upon a client managed thread pool and require a callback to be notified when the request is done.

Example 49. High Level REST Client

static class Config {

  @Bean
  RestHighLevelClient client() {

    ClientConfiguration clientConfiguration = ClientConfiguration.builder() 
      .connectedTo("localhost:9200", "localhost:9201")
      .build();

    return RestClients.create(clientConfiguration).rest(); 
  }
}

// ...

IndexRequest request = new IndexRequest("spring-data", "elasticsearch", randomID())
  .source(singletonMap("feature", "high-level-rest-client"))
  .setRefreshPolicy(IMMEDIATE);

IndexResponse response = client.index(request);

[Original answer]

Currently Spring Data Elasticsearch doesn't support the communication by the REST API. They are using the transport client.

There is separate fork of Spring Data Elasticsearch (the guy needed it for AWS the same as you) where the JEST library is used and communication is made by REST:

https://github.com/VanRoy/spring-data-jest

You will find the interesting discussion under the following ticked of Spring Data Elasticsearch:

https://jira.spring.io/browse/DATAES-220

I think the Spring Data Elasticseach will need to migrate to REST on the future according to the statements from Elasticsearch team that they are planning to support only HTTP communication for ES.

Hope it helps.

like image 101
Przemek Nowak Avatar answered Oct 20 '22 19:10

Przemek Nowak


I think jest client for elasticsearch will serve your purpose. https://github.com/searchbox-io/Jest/tree/master/jest. Jest is a Java HTTP Rest client for ElasticSearch. It has a very good documentation too and have support for all queries in elasticsearch.

like image 29
Jerin D Joy Avatar answered Oct 20 '22 18:10

Jerin D Joy