SpringBoot整合ElasticSearch详解及相关使用方法

开发 前端
继承ElasticsearchRepository后 我们也可以像data-jpa一样使用findBy*等语法来写相关查询方法。

环境:springboot2.3.10.RELEASE + ElasticSearch7.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

数据模型建立

@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

这里我们只需要继承ElasticsearchRepository即可,是不是和data-jpa一样一样的的。

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

继承ElasticsearchRepository后 我们也可以像data-jpa一样使用findBy*等语法来写相关查询方法。

  • 方法名中支持的关键字

图片图片


  • 方法返回值类型
  1. List<T>
  2. Stream<T>
  3. SearchHits<T>
  4. List<SearchHit<T>>
  5. Stream<SearchHit<T>>
  6. SearchPage<T>

Repository中也支持@Query注解的方式自定义查询字符串。

public interface ProductRepository extends ElasticsearchRepository<Product, Long> {
  
  List<Product> findByTitle(String title) ;
  
  @Query("{\"fuzzy\": {\"title\": \"?0\"}}")
  Page<Product> findByTitle(String sex,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) ;
}

除了使用Repository方式,我们还可以使用ElasticsearchRestTemplate的方式请求服务。如下测试

测试

@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) ;
}
  
@Test
public void testFindAll() {
  Pageable pageable = PageRequest.of(1, 2) ;
  Page<Product> page = productRepository.findAll(pageable) ;
  System.out.println(page.getTotalPages() + "\n" + page.getContent()) ;
}
  
@Test
public void testTermSearch() {
  for (Product p : productRepository.findByTitle("Java从入门到精通")) {
    System.out.println(p) ;
  }
}
  
@Test
public void testFindByTitle() {
  Pageable pageable = PageRequest.of(0, 2) ;
  Page<Product> page = productRepository.findByTitle("Java", pageable) ;
  System.out.println(page.getTotalPages() + "\n" + page.getContent()) ;
}
  
@Test
public void testFindByCategory() {
  Pageable pageable = PageRequest.of(0, 2) ;
  Page<Product> page = productRepository.findByCategory("书籍", pageable) ;
  System.out.println(page.getTotalPages() + "\n" + page.getContent()) ;
}
  
@Test
public void testCriteriaQuery() {
  Criteria criteria = new Criteria("price").greaterThan(50).lessThan(80);
  Query query = new CriteriaQuery(criteria);
  SearchHits<Product> hits = elasticTemplate.search(query, Product.class, IndexCoordinates.of("products")) ;
  for (SearchHit<Product> hit : hits) {
    System.out.println(hit) ;
  }
}
  
@Test
public void testStringQuery() {
  Query query = new StringQuery("{ \"match\": { \"category\": { \"query\": \"配件\" } } } ");
  SearchHits<Product> hits = elasticTemplate.search(query, Product.class);
  for (SearchHit<Product> hit : hits) {
    System.out.println(hit) ;
  }
}


@Test
public void testStringQueryFuzzy() {
  Query query = new StringQuery("{ \"fuzzy\":{\"title\":{\"value\":\"Java\"}} }");
  HighlightQuery highlightQuery = null ;
  HighlightBuilder highBuilder = new HighlightBuilder().preTags("<font color='red'>").postTags("</font>").field("title") ;
  highlightQuery = new HighlightQuery(highBuilder) ;
  query.setHighlightQuery(highlightQuery) ;
  SearchHits<Product> hits = elasticTemplate.search(query, Product.class);
  for (SearchHit<Product> hit : hits) {
    System.out.println(hit + "\n" + hit.getHighlightField("title")) ;
  }
}

在启动服务时会自动地为我们创建索引。

我们可以安装Chrome插件 ElasticSearch Head非常方便地查看es的状态及索引信息。

图片图片


这里我是搭建的集群。

图片图片

责任编辑:武晓燕 来源: 实战案例锦集
相关推荐

2023-11-10 08:17:01

分布式搜索引擎

2023-05-11 08:00:44

Golangsync.Pool

2009-12-28 13:28:03

WPF视频

2023-06-28 08:34:02

Bind()函数JavaScript

2017-08-18 14:01:44

大数据dataWrangle

2009-12-02 16:04:44

PHP fsockop

2010-01-28 17:07:03

Android Gal

2010-10-09 10:30:03

JS event

2011-09-06 09:26:03

2023-08-09 08:01:00

WebSockett服务器web

2010-06-03 17:38:03

Hadoop命令

2010-06-01 19:55:30

SVN使用

2009-08-04 08:32:06

Linux Find命Linux Find使用方法

2023-01-06 08:55:00

2010-05-31 11:30:57

SVN使用

2009-11-30 17:43:54

PHP split()

2010-01-25 14:10:21

C++堆栈

2012-05-11 10:13:54

2009-08-25 16:54:28

C# RichText

2010-11-19 09:56:38

SQLiteAndroid
点赞
收藏

51CTO技术栈公众号