A complete Spring Boot application demonstrating Elasticsearch integration with comprehensive search capabilities, following Amazon's system design principles.
- 🔍 Full-text search with relevance scoring
- 📊 Advanced filtering (category, price range, rating, brand)
- 📄 Pagination and sorting
- 🏪 Product management (CRUD operations)
- 🎯 Complex queries with multiple filters
- 📈 Performance optimized with proper indexing
- Spring Boot 3.2.0 - Application framework
- Spring Data Elasticsearch - Elasticsearch integration
- Elasticsearch 8.11.0 - Search engine
- Maven - Build tool
- Lombok - Code generation
- Validation - Input validation
- Repository Pattern - Data access abstraction
- Service Layer - Business logic separation
- REST API - HTTP interface
- Information Reduction - Optimized search queries
- Java 17+
- Maven 3.6+
- Docker & Docker Compose
- Postman (for testing)
docker-compose up -dThis will start:
- Elasticsearch on
http://localhost:9200 - Kibana on
http://localhost:5601
# Build the project
mvn clean compile
# Run the application
mvn spring-boot:runThe application will start on http://localhost:8080
curl -X POST http://localhost:8080/api/products/initialize- Open Postman
- Click "Import"
- Select the
Elasticsearch Demo.postman_collection.jsonfile - The collection contains 12 pre-configured requests
Run the requests in this order:
- 🚀 Initialize Sample Data - Load test data
- 📋 Get All Products - Verify data loading
- ➕ Create New Product - Test CRUD
- 🔍 Get Product by ID - Verify creation
- 🔎 Search by Name - Basic search
- 📂 Search by Category - Filtered search
- 💰 Search by Price Range - Range queries
- 🔤 Full-Text Search - Advanced search
- 🎯 Advanced Search - Multiple filters
- 📄 Search with Pagination - Pagination
- 📊 Search In-Stock - Boolean filters
- ✏️ Update Product - Update operations
- 🗑️ Delete Product - Delete operations
POST /api/products- Create productGET /api/products/{id}- Get product by IDGET /api/products- Get all productsPUT /api/products/{id}- Update productDELETE /api/products/{id}- Delete productPOST /api/products/initialize- Load sample data
GET /api/products/search/name?name={term}- Search by nameGET /api/products/search/category?category={cat}- Search by categoryGET /api/products/search/brand?brand={brand}- Search by brandGET /api/products/search/price?min={min}&max={max}- Search by price rangeGET /api/products/search/in-stock- Search in-stock productsGET /api/products/search/keyword?keyword={term}- Full-text searchGET /api/products/search/advanced- Advanced search with multiple filtersGET /api/products/search/paginated- Paginated searchGET /api/products/search/category-price- Category + price search
# Search products containing "laptop"
curl "http://localhost:8080/api/products/search/name?name=laptop"
# Search in Electronics category
curl "http://localhost:8080/api/products/search/category?category=Electronics"# Multiple filters: Electronics, $500-$1500, rating 4.0+
curl "http://localhost:8080/api/products/search/advanced?category=Electronics&minPrice=500&maxPrice=1500&minRating=4.0"
# Paginated search with sorting
curl "http://localhost:8080/api/products/search/paginated?keyword=apple&page=0&size=5&sortBy=price&sortDirection=asc"This demo implements key concepts from Amazon's store search:
- Inverted Index: Fast text search
- Filter Caching: Reusable filter results
- Query Optimization: Minimal data transfer
- Relevance Scoring: TF-IDF + BM25 algorithms
- Fuzzy Search: Handles typos and variations
- Multi-field Search: Search across name, description, brand, tags
- Faceted Search: Category and brand aggregations
- Horizontal Scaling: Multiple Elasticsearch nodes
- Read Replicas: Separate search clusters
- Circuit Breakers: Fault tolerance
- Async Updates: Event-driven indexing
- Query Response Time: < 50ms for simple queries
- Complex Queries: < 200ms with multiple filters
- Indexing Speed: 1000+ documents/second
- Memory Usage: 512MB heap for development
- Field Mapping: Proper field types for efficient storage
- Analyzer Configuration: Standard analyzer for text fields
- Filter Caching: Reusable filter results
- Query DSL Optimization: Efficient query construction
- Information Reduction: Only index searchable fields
- Query Optimization: Use filters vs queries appropriately
- Caching Strategy: Cache frequent searches
- Data Modeling: Proper field mapping and analyzers
- API Design: RESTful endpoints with proper HTTP methods
- Customer Obsession: Fast, relevant search results
- Dive Deep: Proper indexing and query optimization
- Invent and Simplify: Clean API design
- Bias for Action: Comprehensive testing suite
# Clean and compile
mvn clean compile
# Run tests
mvn test
# Package application
mvn package
# Run application
mvn spring-boot:run
# Generate dependency tree
mvn dependency:tree# Start services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
# Remove volumes
docker-compose down -v# Check cluster health
curl -X GET "localhost:9200/_cluster/health"
# View indices
curl -X GET "localhost:9200/_cat/indices?v"
# Search products index
curl -X GET "localhost:9200/products/_search?pretty"
# Delete index
curl -X DELETE "localhost:9200/products"-
Elasticsearch Connection Failed
# Check if Elasticsearch is running docker-compose ps # View logs docker-compose logs elasticsearch
-
Spring Boot Fails to Start
# Check port conflicts lsof -i :8080 # Clear Maven cache mvn clean
-
Search Returns No Results
# Check if index exists curl -X GET "localhost:9200/products/_search?pretty" # Reinitialize data curl -X POST "http://localhost:8080/api/products/initialize"
Enable debug logging in application.yml:
logging:
level:
org.springframework.data.elasticsearch: DEBUG
com.example.elasticsearchdemo: DEBUG-
Extend Search Features
- Add autocomplete/suggestions
- Implement search analytics
- Add personalization
-
Performance Optimization
- Implement search result caching
- Add search result ranking
- Optimize index mapping
-
Production Readiness
- Add monitoring and metrics
- Implement health checks
- Add comprehensive logging
-
Advanced Features
- Add search result highlighting
- Implement search synonyms
- Add geospatial search
Happy Searching! 🚀