Master Spring Boot 2.3 with Elasticsearch 7: Full Integration Guide

This guide demonstrates how to integrate Spring Boot 2.3.10 with Elasticsearch 7.8, covering Maven dependencies, application configuration, data model annotations, repository interfaces, custom query methods, and comprehensive JUnit tests using both repository and ElasticsearchRestTemplate approaches.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot 2.3 with Elasticsearch 7: Full Integration Guide

Related Dependencies and Configuration

Environment: Spring Boot 2.3.10.RELEASE + Elasticsearch 7.8.0

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
spring:
  elasticsearch:
    rest:
      uris:
      - http://localhost:9201
---
logging:
  level:
    com.pack: debug
    org.springframework.data.elasticsearch.core: debug

Data Model Definition

@Document(createIndex = true, indexName = "products", shards = 3, replicas = 1)
public class Product {

  @Id
  private Long id;

  @Field(analyzer = "ik_max_word", type = FieldType.Text)
  private String title;

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

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

  @Field(type = FieldType.Keyword, index = false)
  private String images;

  @Override
  public String toString() {
    return "Product [id=" + id + ", title=" + title + ", category=" + category + ", price=" + price + ", images=" + images + "]";
  }
}

ProductRepository

Extending ElasticsearchRepository provides CRUD operations similar to Spring Data JPA.

public interface ProductRepository extends ElasticsearchRepository<Product, Long> {
}

Custom query methods and annotations:

public interface ProductRepository extends ElasticsearchRepository<Product, Long> {
  List<Product> findByTitle(String title);
  @Query("{\"fuzzy\": {\"title\": \"?0\"}}")
  Page<Product> findByTitle(String title, Pageable pageable);
  @Query("{\"match\": {\"category\": \"?0\"}}")
  Page<Product> findByCategory(String category, Pageable pageable);
  @Highlight(fields = {@HighlightField(name = "title"), @HighlightField(name = "category")})
  List<SearchHit<Product>> findByTitleOrCategory(String title, String category, Pageable pageable);
}

Testing

@Resource
private ProductRepository productRepository;
@Resource
private ElasticsearchRestTemplate elasticTemplate;

@Test
public void testCreate() {
  Product product = new Product();
  product.setId(3L);
  product.setCategory("配件");
  product.setPrice(299.5d);
  product.setImages("http://www.pack.com/memory.jpg");
  product.setTitle("很牛逼的内存条");
  productRepository.save(product);
}

@Test
public void testQuery() {
  Product product = productRepository.findById(1L).orElse(null);
  System.out.println(product);
}

// Additional test methods for pagination, term search, criteria query, and highlight query are omitted for brevity.

When the application starts, the index is created automatically. Tools like the Chrome plugin ElasticSearch Head can be used to view cluster status and index information.

ElasticSearch Head UI
ElasticSearch Head UI
Elasticsearch cluster diagram
Elasticsearch cluster diagram

Finished!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javatestingbackend-developmentElasticsearchSpring Boot
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.