- 方法名中支持的关键字
文章插图
图片
【SpringBoot整合ElasticSearch详解及相关使用方法】方法返回值类型
- List<T>
- Stream<T>
- SearchHits<T>
- List<SearchHit<T>>
- Stream<SearchHit<T>>
- SearchPage<T>
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的状态及索引信息 。
文章插图
图片
ES集群状态情况
文章插图
图片
完毕?。。?
推荐阅读
- Springboot之把外部依赖包纳入Spring容器管理的两种方式
- SpringBoot接口参数校验N种实用技巧大揭秘
- Springboot 框架中事件监听和发布机制详细介绍
- 如何将本地jar文件打包到 springboot 执行jar文件中
- 我在前端写Java SpringBoot项目
- SpringBoot如何实现热部署?
- 解密SpringBoot线程池
- SpringBootCms
- Java面试题之SpringBoot 框架
- SpringBoot中ApplicationEvent详解