Skip to main content

Elasticsearch : Introduction to Spring Data Elasticsearch

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

Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

Reactive programming in Java with Project Reactor

Reactive programming is a declarative programming paradigm that focuses on building applications that are responsive, resilient, and scalable in the face of modern challenges like concurrency, distributed systems, and asynchronous data streams . Reactive programming provides a set of tools, patterns, and abstractions to handle asynchronous and event-driven programming more effectively. Imperative programming focuses on describing the step-by-step instructions or commands that the computer needs to follow to achieve a specific task. In this paradigm, you explicitly state how to perform each operation and control flow in your code. The emphasis is on "how" the computation should be done. int sum = 0; for (int i = 1; i Declarative programming emphasizes specifying what you want to achieve rather than detailing how to achieve it. You describe the desired outcome or the properties of the result, and the programming language or framework handles the execution detai...