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.
Reading font
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: debugData 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.
Finished!
For questions about attribution, corrections or content rights, please contactand we will review it promptly.