Skip to main content

Integrating Elasticsearch with a Spring Boot and PostgreSQL application

In this article, we will integrate Elasticsearch into a Spring Boot application to enable full-text search and automatic data updates based on PostgreSQL data. Specifically, we will cover the following:

1) How to integrate Elasticsearch into a Spring Boot application.

2) How to automatically add, update, and delete data in the Elasticsearch index based on operations on PostgreSQL data.

3) How to interact with Elasticsearch to perform CRUD operations and full-text searches using Spring Data Elasticsearch.

1) Add Dependencies

Add the required dependencies for Elasticsearch in your pom.xml.

2) Configure Elasticsearch

Ensure Elasticsearch is running on your local machine or adjust the URL if it̢۪s hosted elsewhere.

3) Create an Elasticsearch/JPA Entity

This entity represents a row in PostgreSQL and a document in Elasticsearch. It will be indexed in Elasticsearch to enable efficient search queries.

This class represents an Article entity, which functions as both a row in a PostgreSQL database and a document in an Elasticsearch index. It is designed to be indexed in Elasticsearch, enabling efficient search capabilities. The class also uses an EntityListener to handle specific lifecycle events.


@Document(indexName = "articles"): This annotation indicates that Article is also an Elasticsearch document, which will be stored in an Elasticsearch index named articles.

@EntityListeners(ArticleEntityListener.class): This annotation registers an entity listener (ArticleEntityListener) to handle JPA entity lifecycle events such as persist or update.

4) Synchronizing PostgreSQL and Elasticsearch

The ArticleEntityListener class is an entity listener that listens to specific lifecycle events (persist, update, and remove) on the Article entity.

Its purpose is to synchronize changes between the PostgreSQL database and the Elasticsearch index by updating the Elasticsearch index whenever there is a change in the PostgreSQL data.

5) Create an Elasticsearch Repository

Define a repository for Elasticsearch using ElasticsearchRepository. This will allow you to perform searches directly on your Elasticsearch index.

6) Custom Elasticsearch Operations for Articles

The ArticleSearchOperations class provides custom methods for interacting with the Elasticsearch index specifically for Article entities. It uses ElasticsearchOperations to perform indexing, retrieval, updating, deletion, and various types of search queries on the Article documents.


7) PostgreSQL Repository for Article Entity

The ArticleRepository interface provides a way to interact with the PostgreSQL database for Article entities. It extends JpaRepository, enabling CRUD operations and query execution on Article records stored in the articles table.

8) Periodical Sync between PostgreSQL and Elasticsearch

The BatchSyncService class is a Spring service responsible for periodically synchronizing Article data between PostgreSQL and Elasticsearch. It runs on a fixed schedule, fetching data from the PostgreSQL database and updating the Elasticsearch index to ensure both are in sync.

9) REST Controller for Managing Articles in PostgreSQL

The ArticlePostgresRest class is a Spring Boot REST controller that provides CRUD operations for the Article entity, allowing interaction with PostgreSQL through HTTP requests. It exposes endpoints for creating, reading, updating, and deleting articles.

10) REST Controller for Elasticsearch Operations on Articles

The ArticleSearchOperationsRest class is a Spring Boot REST controller that exposes APIs for interacting with articles stored in Elasticsearch. It provides endpoints for indexing, retrieving, updating, and deleting articles, as well as performing full-text search queries.

11) REST Controller for Elasticsearch Operations Using Repository

The ArticleSearchRepositoryRest class is a Spring Boot REST controller that exposes APIs for managing articles in Elasticsearch using a repository-based approach. This class performs operations such as indexing, updating, retrieving, deleting articles, and performing search queries.

12) Application Entry Point

The Application class serves as the entry point for the Spring Boot application.

13) Test the Application

Navigate to http://localhost:8080/swagger-ui.html (or the configured URL for Swagger UI). You should now see an interactive interface to test the endpoints for the Article APIs.

14) More Info

In Spring Data Elasticsearch, there are several ways to interact with an Elasticsearch cluster:

1) ElasticsearchRepository: A high-level interface based on Spring Data repositories, which provides CRUD operations and query methods for Elasticsearch.

2) ElasticsearchOperations: A more generic interface for interacting with Elasticsearch. It offers a lower-level API than ElasticsearchRepository and provides flexibility to define custom methods, work with indices, mappings, and perform custom queries.

3) ElasticsearchRestTemplate (formerly ElasticsearchTemplate): A concrete implementation of ElasticsearchOperations using the REST client (since Spring Data Elasticsearch 4.0+). ElasticsearchRestTemplate is often used for more complex operations, such as bulk indexing, custom queries, and handling more advanced Elasticsearch configurations.

Source Code: GitHub

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...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...