Elasticsearch integration for full-text search

David Kumar Jan 2026
3 tabs
package com.example.demo.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.time.LocalDateTime;
import java.util.List;

@Document(indexName = "products")
public class Product {

    @Id
    private String id;

    @Field(type = FieldType.Text, analyzer = "standard")
    private String name;

    @Field(type = FieldType.Text, analyzer = "standard")
    private String description;

    @Field(type = FieldType.Keyword)
    private String category;

    @Field(type = FieldType.Double)
    private Double price;

    @Field(type = FieldType.Integer)
    private Integer stock;

    @Field(type = FieldType.Keyword)
    private List<String> tags;

    @Field(type = FieldType.Date)
    private LocalDateTime createdAt;

    @Field(type = FieldType.Boolean)
    private Boolean available;

    // Getters and setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }

    public Double getPrice() { return price; }
    public void setPrice(Double price) { this.price = price; }

    public Integer getStock() { return stock; }
    public void setStock(Integer stock) { this.stock = stock; }

    public List<String> getTags() { return tags; }
    public void setTags(List<String> tags) { this.tags = tags; }

    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }

    public Boolean getAvailable() { return available; }
    public void setAvailable(Boolean available) { this.available = available; }
}
3 files · java Explain with highlit

Elasticsearch provides powerful full-text search capabilities. Spring Data Elasticsearch offers repository abstraction similar to JPA. @Document annotates entity classes with index mapping. @Field customizes field types and analyzers. Queries use method names, @Query annotation, or query builders. Full-text search supports stemming, synonyms, fuzzy matching. Aggregations enable faceted search and analytics. Highlighting shows match context. Geospatial queries find nearby locations. Auto-complete uses completion suggesters. Bulk operations optimize indexing. Elasticsearch scales horizontally for large datasets. Integration enables sophisticated search features—filtering, sorting, pagination, relevance tuning. Proper mapping design is crucial for performance and relevance. Elasticsearch complements relational databases for search-heavy applications.