Skip to content

Latest commit

 

History

History

boot-mongodb-elasticsearch

Spring Boot Reactive MongoDB and Elasticsearch Integration

A reactive Spring Boot application demonstrating seamless integration between MongoDB and Elasticsearch, featuring real-time search capabilities including geospatial queries, full-text search, and aggregations.


Highlights

  • Reactive Streams Approach: Uses Spring Data’s reactive driver for both MongoDB and Elasticsearch.
  • Advanced Queries: Demonstrates geospatial searches, text-indexed searches, and filtering in near real-time.
  • Scalable Architecture: Docker-compose-based environment for local testing.

Architecture

The application follows a reactive architecture pattern using Spring WebFlux:

sequenceDiagram
participant Client
participant RestaurantController
participant RestaurantService
participant MongoDB
participant ChangeStreamResume
participant Elasticsearch

Client->>RestaurantController: Create/Update Restaurant
RestaurantController->>RestaurantService: Process Request
RestaurantService->>MongoDB: Save Data
MongoDB-->>ChangeStreamResume: Trigger Change Stream
ChangeStreamResume->>Elasticsearch: Sync Changes
Note over ChangeStreamResume,Elasticsearch: Resume Token Management
RestaurantService-->>RestaurantController: Return Response
RestaurantController-->>Client: HTTP Response
Loading

Key Features

  • Reactive API endpoints using Spring WebFlux and reactive drivers
  • Real-time Synchronization: MongoDB change streams to Elasticsearch
  • MongoDB for primary data storage
  • Advanced Search Capabilities:
    • Full-text search
    • Geospatial queries
    • Aggregation operations
  • Comprehensive validation
  • Exception handling
  • API Documentation: OpenAPI/Swagger UI integration

Sequence Diagrams

Restaurant Creation Flow

sequenceDiagram
    participant C as Client
    participant RC as RestaurantController
    participant RS as RestaurantService
    participant MR as MongoRepository
    participant ER as ESRepository
    
    C->>RC: POST /api/restaurant
    RC->>RC: Validate Request
    RC->>RS: createRestaurant()
    RS->>MR: save()
    MR-->>RS: Restaurant
    RS->>ER: index()
    ER-->>RS: Success
    RS-->>RC: Restaurant
    RC-->>C: 201 Created
Loading

Search Operation Flow

sequenceDiagram
    participant C as Client
    participant SC as SearchController
    participant SS as SearchService
    participant ER as ESRepository
    
    C->>SC: GET /api/search
    SC->>SC: Validate Parameters
    SC->>SS: search()
    SS->>ER: search()
    ER-->>SS: SearchResults
    SS-->>SC: SearchPage
    SC-->>C: 200 OK
Loading

Data Flow

flowchart LR
    Client --> RestController
    RestController --> Service
    Service --> MongoDB
    MongoDB --> ChangeStream
    ChangeStream --> Elasticsearch
    Service --> Elasticsearch
Loading

Configuration Notes

MongoDB

  • Uses replica set for change streams
  • Transaction support with ReactiveMongoTransactionManager
  • Default database: mongoes

Getting Started

Prerequisites

  • JDK 21+
  • Docker and Docker Compose
  • Maven 3.9+

Running Locally

  1. Start the infrastructure:
docker compose -f docker/docker-compose.yml up -d
  1. Run the application:
./mvnw spring-boot:run -Dspring-boot.run.profiles=local

Running Tests

./mvnw clean verify

API Documentation

Monitoring

Development Tools

Useful Elasticsearch Commands

  • Count documents: GET /restaurant/_count
  • Search all: GET /restaurant/_search
  • View mapping: GET /restaurant/_mapping

Configuration Properties

Key application properties:

spring.data.mongodb.database=mongoes
spring.data.mongodb.uri=mongodb://localhost:27017/mongoes?replicaSet=rs0
spring.elasticsearch.uris=localhost:9200
spring.elasticsearch.socket-timeout=10s

Exception Handling

The application includes global exception handling for:

  • Validation errors
  • Duplicate entries
  • Resource not found
  • General server errors

Reference