Home Post BigData

Elasticsearch : Introduction to Spring Data Elasticsearch

Mar 31, 2024

In this article we will see how to integrate and use "Elasticsearch" in "Spring Boot" with the help of "Spring Data Elasticsearch".

We are going to use default configurations for now, while providing only the "elasticsearch.host" and "elasticsearch.port" in "application.properties".

1) Download and Install Elasticsearch

Download the latest Elasticsearch release and extract it:

% wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.3-darwin-x86_64.tar.gz
% tar -zxvf elasticsearch-7.17.3-darwin-x86_64.tar.gz
% cd elasticsearch-7.17.3

Run the following command in order to start Elasticsearch:

% bin/elasticsearch

Run the following command in a new tab to check status:

% curl http://localhost:9200/
{
  "name" : "Codeburps-MacBook-Pro.local",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "FflvHoxoS_q2MYxrTKZuWw",
  "version" : {
    "number" : "7.17.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "5ad023604c8d7416c9eb6c0eadb62b14e766caff",
    "build_date" : "2022-04-19T08:11:19.070913226Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2) Spring Data Elasticsearch Example

2.1) Dependencies

Lets create a project with Spring Initializr, make sure to add required dependencies as shown in the picture below:

Also add "springdoc-openapi-ui" dependency to project, we need this to test our APIs. The final "pom.xml" should look something like this:

2.2) Properties file

Lets add required "elasticsearch.host", "elasticsearch.port" and "swagger" related properties to "application.properties".

2.3) Model/Schema

We are creating "CRUD" Rest APIs" for an "Article (doc)", here is the model to represent it:

2.4) Repository

This is where "Spring Data Elasticsearch" comes into the picture, we are creating an "ArticleRepository" implementing "ElasticsearchRepository", this will save us from writing any boilerplate code.

Read More: Spring Data Elasticsearch.

2.5) Controller

Finally, this is the place where we are defining rest endpoints for our service; please take special attention to the usage of "consumes" and "produces".

2.6) Spring Boot

This is plain old Spring Boot driver class; all the execution starts from here.

2.7) Testing

Now we are all done with our project setup, remember we have added a dependency for "springdoc-openapi-ui". This will help us in testing our end-points with an inbuilt interface.

Run the application and swagger UI for testing APIs can be accessed here: http://localhost:8080/swagger-ui.html

Source Code

avatar

NK Chauhan

NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.

Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.

His hobbies include playing Cricket, Video Games and hanging with friends.

Categories
Spring Framework
Microservices
BigData
Core Java
Java Concurrency