1

I'm new to Elastic search. We are building a Spring boot application with Elastic search.

Currently, we are bound to use Spring Boot 2.1.3.RELEASE but we can use the latest stable Elastic search version.

Done some R&D and found below two dependencies to integrate with Elastic search.

1. elasticsearch-rest-high-level-client
2. spring-boot-starter-data-elasticsearch

There might be other ways of integrating the Spring boot with Elastic search.

Could anyone help with identifying the best way to integrating the Spring boot with Elastic search?

Which version of Elastic search should I use as per the above-provided Spring boot version?

16
  • I have an App built with Spring boot 2.1.0.RELEASE and Elasticsearch 6.2.2. Make sure to use same versions. Most import is the pom.xml and the versions. it works well for me. github.com/Georges73/Touring-fullstack/tree/master/… Commented Jun 12, 2020 at 4:23
  • Thanks for your suggestion! Can't I use the latest version of Elastic search? If not then why? Commented Jun 12, 2020 at 4:25
  • 1
    Since data-elasticsearch only supprt ES transportclient it will not work with rest-high-level-client (see my first example) For performance I would go for elasticsearch-rest-high-level-client as shown in my second example. Commented Jun 12, 2020 at 4:46
  • 1
    You can also combine both (see the answer below ). When combined you will use spring-data-elasticsearch so you can use its cool (but limited) built in query language. For more compex queries use the rest-high-level-client. By complex, I mean analytics and such. Commented Jun 12, 2020 at 4:49
  • 1
    I'm bound to use Spring boot! Commented Jun 12, 2020 at 6:05

1 Answer 1

1

Coming to the Elasticsearch Version, Refer this site:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions

For using Elasticsearch with SpringBoot, we include three dependencies:

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>

As You can see My Elasticsearch Version is 6.2.2(to match the server side version) and my Spring Version is 2.1.13.RELEASE.

There are basically 2 Clients used. I would suggest you to use Rest High Level Client. The other one is Transport Client.

Here is a how you can integrate Rest High Level Client to your application:

@Configuration
public class ElasticClientService extends AbstractElasticsearchConfiguration {

  @Override
  @Bean
  public RestHighLevelClient elasticsearchClient() {
    final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
        .connectedTo("localhost:9200").build();
    return RestClients.create(clientConfiguration).rest();
  }
}

Once the client is created, only thing left is to perform CRUD operations.

  @Autowired
  ElasticClientService client;

  public void save(Object object, String id, String type, String indexName) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, Object> objectMap = objectMapper.convertValue(object, Map.class);
    IndexRequest indexRequest = new IndexRequest(indexName, type, id);
    indexRequest.source(objectMap);
    IndexResponse response = client.elasticsearchClient().index(indexRequest);
  }

  public void deleteById(String id, String type, String indexName) throws IOException {
    DeleteRequest request = new DeleteRequest(indexName, type, id);
    DeleteResponse deleteResponse = client.elasticsearchClient().delete(request);
  }

The above two operations create a Document(row) in elastic index and delete a document(row) from elastic index according to ID.

For more reference See :https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-document-delete.html *
*Change Version According to your need

You could Refer this for further assistance

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, i found that better to use "High level Rest client" only. Can I use "elasticsearch-7.7.1" with my "Spring Boot 2.1.3.RELEASE" if i integrate using high level rest client?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.