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


  • 方法名中支持的关键字

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

文章插图
图片
【SpringBoot整合ElasticSearch详解及相关使用方法】方法返回值类型
  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的方式请求服务 。如下测试
测试@Resourceprivate ProductRepository productRepository ;@Resourceprivate ElasticsearchRestTemplate elasticTemplate ;@Testpublic 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) ;}@Testpublic void testQuery() {Product product = productRepository.findById(1L).orElse(null) ;System.out.println(product) ;}@Testpublic void testFindAll() {Pageable pageable = PageRequest.of(1, 2) ;Page<Product> page = productRepository.findAll(pageable) ;System.out.println(page.getTotalPages() + "n" + page.getContent()) ;}@Testpublic void testTermSearch() {for (Product p : productRepository.findByTitle("JAVA从入门到精通")) {System.out.println(p) ;}}@Testpublic void testFindByTitle() {Pageable pageable = PageRequest.of(0, 2) ;Page<Product> page = productRepository.findByTitle("Java", pageable) ;System.out.println(page.getTotalPages() + "n" + page.getContent()) ;}@Testpublic void testFindByCategory() {Pageable pageable = PageRequest.of(0, 2) ;Page<Product> page = productRepository.findByCategory("书籍", pageable) ;System.out.println(page.getTotalPages() + "n" + page.getContent()) ;}@Testpublic 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) ;}}@Testpublic 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) ;}}@Testpublic 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的状态及索引信息 。
SpringBoot整合ElasticSearch详解及相关使用方法

文章插图
图片
ES集群状态情况
SpringBoot整合ElasticSearch详解及相关使用方法

文章插图
图片
完毕?。。?




推荐阅读