Integrating AWS OpenSearch with Spring Boot (Index, Search, Pagination & Aggregation)

  • 4.0/5
  • 274
  • Jan 05, 2025

In this post, we'll explore how to integrate AWS OpenSearch with a Spring Boot application to perform key operations such as indexing, searching, pagination, and aggregation.

Prerequisites

Before starting with the integration, ensure you have the following:

1) An AWS OpenSearch domain: You can create it via the AWS Management Console.

2) A Spring Boot application: If you don't have one already, you can create a Spring Boot app using Spring Initializr. Maven or Gradle for managing dependencies.

Integrating AWS OpenSearch with Spring Boot

Add Dependencies

Start by adding the necessary dependencies in your pom.xml (for Maven users):

Configure OpenSearch in application.yml

Next, configure the OpenSearch client to connect to your OpenSearch domain by adding the following to your application.yml:

Make sure you replace your-opensearch-domain, your-username, and your-password with your actual OpenSearch domain details.

Define the Entity Class

Define the entity class you want to index in OpenSearch. For example, let’s consider a Book entity:

Here, we are annotating the class with @Document to indicate that this class should be indexed in OpenSearch. We also use @Field annotations to define how each field should be indexed.

Create the Repository Interface

Create a repository interface that extends ElasticsearchRepository for performing CRUD operations:

This repository interface will allow us to easily interact with OpenSearch without writing any custom query logic.

Service Class to Handle Indexing, Searching, and Aggregations

Now, let's create a service class where we'll define methods to handle indexing, searching, pagination, and aggregation.

In the service class, we have three key operations:

Indexing: The 'saveBook' and 'saveBooks' methods will index new book(s) in OpenSearch.

Searching with Pagination: The 'searchBooks' method performs a search with pagination, making it efficient for large datasets.

Aggregation: The 'countBooksByGenre' method is used to count number of books in a genre using aggregation.

Controller Class

Finally, create a REST controller to expose these operations via API endpoints:

Testing and Verifying the Integration

Swagger UI provides an interactive documentation interface where you can directly test the API endpoints, including those for indexing, searching, and aggregating OpenSearch data.

Start your Spring Boot application by running the following command: mvn spring-boot:run

After the application starts, you should be able to access Swagger UI at the following URL: http://localhost:8080/swagger-ui/index.html

This will bring up the Swagger UI interface, where you can interactively test the APIs.

Source Code: GitHub

Index
How to Implement PostgreSQL Full-Text Search with Spring Boot

15 min

Spring's transaction management with the @Transactional annotation

9 min

Spring Boot Rest APIs with PostgreSQL (Spring Boot + Rest APIs)

15 min

Caching in Spring Boot (@Cacheable, @CacheEvict & @CachePut)

21 min

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

13 min

Circuit Breaker in Spring Boot (Spring Cloud Circuit Breaker + Resilience4j)

12 min

Handling Concurrent Service Calls in a Spring Boot Application: CompletableFuture and @Async

11 min

Profiling a Spring Boot application with Pyroscope

7 min

Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)

9 min

Dockerize Spring Boot app and Push image to DockerHub (Spring Boot + DockerHub)

4 min

Creating a Jenkins Pipeline for Spring Boot application

2 min

Edge Server Pattern in Microservices (Spring Cloud Gateway)

7 min

Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)

7 min

Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)

4 min

Spring Cloud config server setup with Git

8 min

Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)

9 min

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

24 min

Deploying Spring Boot microservices on Kubernetes Cluster

12 min

Reactive programming in Java with Project Reactor

50 min

Spring Reactive with PostgreSQL (Spring Boot WebFlux + PostgreSQL)

13 min

Spring Reactive, Thymeleaf Hello World (Spring Webflux + Thymeleaf + JS/CSS)

9 min

Problem JSON (application/problem+json) in Spring WebFlux

15 min

Spring Boot Login/Logout (Spring Security + MySql + Thymeleaf)

21 min

Securing Server-to-Server Communication with "Spring Boot" & "OAuth 2"

18 min

Integrating AWS OpenSearch with Spring Boot (Index, Search, Pagination & Aggregation)

8 min

Integrating Elasticsearch with a Spring Boot and PostgreSQL application

16 min

Sending Emails in Spring Boot via SMTP

7 min

How to create a basic Spring 6 project using Maven

5 min

Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)

9 min