Spring Boot with Amazon OpenSearch
Learn how to integrate Spring Boot with Amazon OpenSearch step by step. This guide covers OpenSearch architecture, indexing, searching, full-text search, REST APIs, Spring Boot integration, AWS SDK, monitoring, and production best practices.
Introduction
Modern applications need more than simple database queries.
Users expect features like:
- Instant product search
- Full-text document search
- Auto-complete
- Fuzzy search
- Log analytics
- Monitoring dashboards
- Real-time search
Traditional relational databases are not optimized for complex search operations.
Amazon OpenSearch Service provides a fully managed search and analytics engine built on the OpenSearch project.
In this article, we will integrate Spring Boot with Amazon OpenSearch and implement enterprise search functionality.
Learning Objectives
After completing this article, you will understand:
- What is OpenSearch?
- Why use a search engine?
- OpenSearch Architecture
- Documents
- Indexes
- Shards
- Replicas
- Full-Text Search
- REST APIs
- Spring Boot Integration
- Search Queries
- Production Best Practices
What is Amazon OpenSearch?
Amazon OpenSearch Service is a managed search and analytics service.
It allows applications to:
- Store searchable documents
- Perform full-text search
- Filter results
- Aggregate data
- Analyze logs
- Build dashboards
OpenSearch is commonly used alongside relational databases rather than replacing them.
Why OpenSearch?
Suppose an e-commerce application stores one million products.
Searching by SQL:
SELECT *
FROM products
WHERE name LIKE '%laptop%'
Problems:
- Slow for large datasets
- Limited ranking capabilities
- No typo tolerance
- Poor full-text search
OpenSearch solves these challenges with optimized indexing and search algorithms.
OpenSearch Architecture
flowchart LR
USER[Client]
API[Spring Boot]
OS[(Amazon OpenSearch)]
RDS[(Amazon Aurora)]
USER --> API
API --> OS
API --> RDS
Enterprise Architecture
flowchart TD
Users
ALB
SpringBoot
OpenSearch
Aurora
CloudWatch
Users --> ALB
ALB --> SpringBoot
SpringBoot --> OpenSearch
SpringBoot --> Aurora
SpringBoot --> CloudWatch
Core Components
| Component | Description |
|---|---|
| Domain | OpenSearch Cluster |
| Index | Collection of Documents |
| Document | JSON Record |
| Field | JSON Attribute |
| Node | Server |
| Shard | Data Partition |
| Replica | Copy of Shard |
Real-Time Use Cases
Amazon OpenSearch is widely used for:
- Product Search
- Blog Search
- Resume Search
- Banking Transactions
- Fraud Detection
- Application Logs
- Security Analytics
- AI Knowledge Search
- Customer Support Search
- Recommendation Systems
OpenSearch Concepts
Index
Equivalent to a table.
Example
employees
Document
Equivalent to a row.
{
"id":1,
"name":"Venu",
"department":"Architecture"
}
Field
Equivalent to a column.
Example:
name
department
designation
High-Level Flow
flowchart LR
Browser
SpringBoot
OpenSearch
Browser --> SpringBoot
SpringBoot --> OpenSearch
Search Workflow
flowchart LR
User
API
OpenSearch
Results
User --> API
API --> OpenSearch
OpenSearch --> Results
Database vs OpenSearch
| Database | OpenSearch |
|---|---|
| CRUD | Search |
| ACID | Eventually Consistent |
| Joins | No Joins |
| Primary Storage | Search Index |
| SQL | Query DSL |
Step 1 Create OpenSearch Domain
AWS Console
↓
Amazon OpenSearch Service
↓
Create Domain
Choose
Development and Testing
Learning purpose.
Production:
Production
Domain Configuration
Domain Name
codewithvenu-search
Engine Version
Latest available OpenSearch version.
Instance Type
Learning
t3.small.search
Production
m6g.large.search
or larger.
Storage
Enable
EBS GP3
20 GB
Network
Learning
Public Access
Production
VPC Only
Security
Enable
- Fine-Grained Access Control
- HTTPS
- Encryption at Rest
- Node-to-Node Encryption
Verify Domain
Status
Active
Step 2 Create Spring Boot Project
Dependencies
- Spring Web
- Validation
- OpenSearch Java Client
Maven Dependencies
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-java</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
application.yml
opensearch:
host: https://search-codewithvenu.us-east-1.es.amazonaws.com
port: 443
Configure Client
@Configuration
public class OpenSearchConfig {
@Bean
public OpenSearchClient openSearchClient() {
// Configure transport and authentication
return null;
}
}
Document Example
{
"id":1,
"name":"Venu",
"department":"Architecture",
"experience":13
}
Create Index
Index Name
employees
Indexing Flow
flowchart LR
A["Spring Boot"]
B["Index API"]
C["OpenSearch"]
A --> B
B --> C
Create Document
REST API
POST
/employees
Input
{
"name":"Venu",
"department":"Architecture"
}
Search API
GET
/employees/search?q=venu
Output
[
{
"name":"Venu",
"department":"Architecture"
}
]
Update Document
PUT
/employees/1
Delete Document
DELETE
/employees/1
CRUD Flow
flowchart LR
Client
Controller
Service
OpenSearch
Client --> Controller
Controller --> Service
Service --> OpenSearch
Full-Text Search
Supports:
- Exact Match
- Partial Match
- Phrase Search
- Wildcards
- Prefix Search
- Fuzzy Search
Example Search
Search
Architecture
Returns
Architecture Lead
Solution Architect
Architecture Manager
Fuzzy Search
User types
Sprng Boot
OpenSearch returns
Spring Boot
Automatically.
Auto Complete
User types
jav
Suggestions
Java
Java Spring
Java AWS
Filters
Search
Department=Cloud
Experience>10
Aggregations
Examples
- Employee Count
- Salary Average
- Orders Per Day
- Revenue Per Month
Log Analytics
Applications send logs.
↓
OpenSearch
↓
Dashboards
↓
Visualization
OpenSearch Dashboards
Supports
- Graphs
- Charts
- Tables
- Monitoring
- Alerts
Monitoring
CloudWatch Metrics
Monitor
- CPU
- JVM Memory
- Storage
- Search Latency
- Index Latency
- Thread Pools
Security
Enable
- IAM Authentication
- HTTPS
- Encryption
- Fine-Grained Access
- VPC Access
Never expose your domain publicly in production.
Scaling
Increase
- Data Nodes
- Storage
- Replicas
OpenSearch supports horizontal scaling.
Production Architecture
flowchart TD
Users
ALB
SpringBoot
OpenSearch
Aurora
CloudWatch
Users --> ALB
ALB --> SpringBoot
SpringBoot --> OpenSearch
SpringBoot --> Aurora
SpringBoot --> CloudWatch
Common Errors
Connection Refused
Check:
- Security Group
- Domain Endpoint
- HTTPS
Authentication Failed
Verify IAM permissions.
Index Not Found
Create index before indexing documents.
Slow Search
Review:
- Shard count
- Query complexity
- Mappings
- Cluster health
Best Practices
- Use OpenSearch only for search workloads
- Keep relational data in Aurora or RDS
- Use bulk indexing
- Design index mappings carefully
- Avoid very large documents
- Enable snapshots
- Monitor cluster health
- Use VPC deployment
- Enable encryption
- Configure IAM access
- Use aliases for index versioning
- Monitor slow queries
OpenSearch vs Aurora
| Feature | Aurora | OpenSearch |
|---|---|---|
| Purpose | Database | Search Engine |
| CRUD | Yes | Limited |
| SQL | Yes | No |
| Full Text Search | Basic | Excellent |
| Analytics | Limited | Advanced |
| Ranking | No | Yes |
Interview Questions
What is Amazon OpenSearch?
A managed search and analytics service used for full-text search, log analytics, and real-time data exploration.
What is an Index?
A logical collection of documents, similar to a table in a relational database.
What is a Document?
A JSON object stored inside an index.
Difference between Aurora and OpenSearch?
Aurora is the system of record for transactional data, while OpenSearch is optimized for fast searching and analytics.
Why not use SQL LIKE instead of OpenSearch?
LIKE queries become slow on large datasets and lack advanced search capabilities such as relevance scoring, typo tolerance, stemming, and fuzzy matching.
What are Shards?
Shards partition an index across multiple nodes to improve scalability and parallel search performance.
Summary
In this article, we integrated Spring Boot with Amazon OpenSearch.
We covered:
- OpenSearch fundamentals
- Indexes and documents
- Search architecture
- Full-text search
- CRUD operations
- Query concepts
- Monitoring
- Security
- Scaling
- Production best practices
Amazon OpenSearch is an excellent choice for enterprise Spring Boot applications that require fast, scalable search, analytics, and log exploration while keeping transactional data in Amazon Aurora or Amazon RDS.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...