Skip to main content

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

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


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

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

Feign , an early declarative REST client, was initially part of the Spring Cloud Netflix stack and later rebranded as Spring Cloud OpenFeign . Before its introduction, crafting HTTP calls using RestTemplate involved repetitive code for each service interaction. With Feign, developers could simply define an interface with method contracts mirroring the service's endpoints. Behind the scenes, proxy magic generated a fully functional HTTP client, eliminating the need for boilerplate code . HTTP Interface (Spring Framework 6) The recent release of Spring Framework 6 integrated this declarative REST client as a native part of the core web framework in the form of the HTTP Interface . All the necessary components reside in the spring-web module, which is a transitive dependency for either the spring-boot-starter-web or spring-boot-starter-webflux modules. Currently, the WebFlux dependency is essential due to the HttpServiceProxyFactory , responsible for client gener...