How to Connect Elasticsearch to Kotlin Using Spring Boot, Spring Data and SSL

Raphael De Lio
4 min readApr 12, 2021

Twitter | LinkedIn | YouTube | Instagram

In this tutorial, you will learn how you can connect your Spring Boot application to an Elasticsearch instance using Spring Data and SSL.

If you still don’t have an instance of Elasticsearch running, you can check one of my previous stories and find out how you can deploy a secure one on Kubernetes with SSL:

We will be going through:

  • Set Up Spring Data Elasticsearch
  • Configuring Spring Data Elasticsearch
  • Defining a Document
  • Implementing a Repository
  • Performing Basic Operations

Set Up Spring Data Elasticsearch

For this tutorial, I will assume you already have a Spring Boot Application running. If you still don’t, you can follow this nice tutorial provided by Spring, it shouldn’t take longer than 15 minutes.

As soon as you have your Spring Boot Application, you can add the follow dependency to it:

Maven:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.1.7</version>
</dependency>
Gradle:
implementation("org.springframework.data:spring-data-elasticsearch:4.1.7")

Reload your Gradle or Maven project and your new dependency will be downloaded.

Configuring Spring Data Elasticsearch

There are several ways you can configure your application to connect to Elasticsearch. We will be covering two of them. One with and the other without SSL.

Without SSL

If you’re Elasticsearch instance doesn’t require a SSL certificate to authenticate, configuring the connection is really simple. All you have to do is adding the following properties to application.properties or bootstrap.yml

bootstrap.yml
spring:
elasticsearch:
rest:
username: elastic
password: <password>
uris: localhost:9200
application.properties
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=<password>
spring.elasticsearch.rest.uris=localhost:9200

With SSL

This one is trickier. As I said, there are several ways of doing the same thing. I decided to create a KeyStore programatically so I could use the existing ca.crt stored as a secret in my Kubernetes cluster instead of having to add it using the Java Keytool in the Dockerfile. Here is the Configuration class:

Defining a Document

Our entity is a simple document named Person where the id is of the type String. We also specify that such documents must be stored in an index named my-index.

I’m very happy you got to this part of the story, I‘m truly thankful for this.
Support my work: follow me and clap to this story.

Implementing a Repository

In order to implement our repository, we extend the provided ElasticsearchRepository and replace the generics with our entity and its primary key type.

Performing Basic Operations

Cool! Now we’ve got everything we need to implement our controller and perform some basic operations in our index. In the implementation below you can see we defined a Controller with a method to save a new entity, get all entities, delete an entity or update an entity’s name.

Now we can run our application and test by calling the previously defined endpoints

POST

PUT

GET

DELETE

I hope you have enjoyed this tutorial. The source code for it can be found on GitHub.

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Spring with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--

Raphael De Lio

Software Consultant @ Xebia - Dutch Kotlin User Group Organizer: https://kotlin.nl