Spring Data Elasticsearch

Reading material

  1. https://spring.io/projects/spring-data-elasticsearch/
  2. https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/template.html

Spring Data Elasticsearch provides a simple interface to perform these operations on Elasticsearch as an alternative to using the REST APIs directly.

Elasticsearch Operations with Spring Data

Although the Elasticsearch Client can be used directly to work with the cluster, applications using Spring Data Elasticsearch normally use the higher level abstractions of Elasticsearch Operations and Elasticsearch Repositories.

Higher level abstraction Elasticsearch Operations Elasticsearch Repositories
[Reactive]ElasticsearchTemplate: We create queries with method chaining and native queries to have more control over creating Elasticsearch queries in relatively complex scenarios. Repositories: We define methods in an interface, and Elasticsearch queries are generated from method names at runtime.
https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/template.html https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/repositories/elasticsearch-repositories.html
ElasticsearchRestTemplate implements the interface ElasticsearchOperations, which does the heavy lifting for low-level search and cluster actions. In Service class: private final ElasticsearchOperations elasticsearchOperations; public interface ProductRepository extends ElasticsearchRepository<Product, String>
Storaging/Indexing .index, .bulkIndex .save, .saveAll
Search query or aggragation .search (NativeQuery, StringQuery, CriteriaQuery) .findBy...
https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/template.html#elasticsearch.operations.queries https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/repositories/elasticsearch-repository-queries.html

ElasticsearchRestTemplate

The Spring Data repository may not be suitable when we need more control over how we design our queries or when the team already has expertise with Elasticsearch syntax.

ElasticsearchRestTemplate implements the interface ElasticsearchOperations, which does the heavy lifting for low-level search and cluster actions.

Indexing

Searching

  1. https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html

    1. Intervals
    2. Match
    3. Match boolean prefix
    4. Match phrase
    5. Match phrase prefix
    6. Combined fields
    7. Multi-match
    8. Query string
    9. Simple query string
  2. https://docs.spring.io/spring-data/elasticsearch/reference/elasticsearch/template.html#elasticsearch.operations.queries

  3. NativeQuery

    @Service
    @RequiredArgsConstructor
    public class ItemService {
        private final ElasticsearchOperations elasticsearchOperations;
    
        public SearchHits<Item> search(String name) {
            Query query = NativeQuery.builder().withQuery(q -> q.match(m -> m.field("name").query(name))).build();
            return elasticsearchOperations.search(query, Item.class);
        }
    }
    
    1. NativeQuery provides the maximum flexibility for building a query using objects representing Elasticsearch constructs like aggregation, filter, and sort.
    2. This feature allows you to execute native queries against Elasticsearch. This one provides the maximum flexibility for constructing queries.
  4. StringQuery

    @Service
    @RequiredArgsConstructor
    public class ItemService {
        private final ElasticsearchOperations elasticsearchOperations;
    
        public SearchHits<Item> search(String name) {
            Query query = new StringQuery("{ \"match\": { \"name\": { \"query\": \"" + name + " \" } } } ");
            return elasticsearchOperations.search(query, Item.class);
        }
    }
    
    1. A StringQuery gives full control by allowing the use of the native Elasticsearch query as a JSON string
    2. This feature allows to construct Elasticsearch queries as JSON String. With StringQuery you can write Elasticsearch queries using the Elasticsearch query DSL (Domain-Specific Language) syntax directly in the form of a string. It provides flexibility when you need to construct complex queries dynamically or when you have existing queries in string format.
  5. CriteriaQuery -

    @Service
    @RequiredArgsConstructor
    public class ItemService {
        private final ElasticsearchOperations elasticsearchOperations;
    
        public SearchHits<Item> search(String name) {
            Criteria criteria = new Criteria("name").is(name);
            Query searchQuery = new CriteriaQuery(criteria);
            return elasticsearchOperations.search(searchQuery, Item.class);
        }
    }
    
    1. With CriteriaQuery we can build queries without knowing any terminology of Elasticsearch. The queries are built using method chaining with Criteria objects. Each object specifies some criteria used for searching documents
    2. CriteriaQuery allows creating search queries for data without knowing the syntax or basics of Elasticsearch queries. Users can construct the queries by chaining and combining Criteria objects, where each object specifies specific criteria for searching documents.

Sample implementation

  1. https://github.com/explorer436/programming-playground/tree/main/java-playground/elasticsearch-examples