Elasticsearch : Introduction to Spring Data Elasticsearch

  • 4.5/5
  • 5483
  • Jul 20, 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

Index
Apache Kafka : Install Zookeeper ensemble on AWS EC2

3 min

Apache Kafka : Developing Producer & Consumer in Java

10 min

Apache Kafka : Best Practices-Topic, Partitions, Consumers and Producers

2 min

Apache Kafka : Setup Multi Broker Kafka Cluster on Amazon EC2

3 min

RabbitMQ Java Client Example (Producer & Consumer)

5 min

Elasticsearch : Introduction to Spring Data Elasticsearch

7 min

Elasticsearch : Getting started with Elasticsearch & Kibana

3 min

Getting started with Spring Data MongoDB (Spring Boot + MongoDB)

11 min

Spring Data Redis with RedisTemplate and CrudRepository

14 min