分页查询主要涉及两个类。一个是Page,一个是Pageable
Repository中编写代码
/** * 根据content和title分页查询 * * @param content * @param title * @param pageable * @return */ Page<Article> findByContentLikeOrTitleLike(String content, String title, Pageable pageable);
Service中添加代码
/** * 分页查询 * @param content * @param title * @param page * @param size * @return */ public Page<Article> getByContentOrTitlePage(String content, String title, int page, int size) { // 构造Pageable Pageable pageable = PageRequest.of(page - 1, size); Page<Article> articlePage = articleDao.findByContentLikeOrTitleLike(content, title, pageable); return articlePage; }
测试类
@Test public void testPage1() { Page<Article> page = articleService.getByContentOrTitlePage("奥利给", "PHP", 1, 2); System.out.println("总条数:" + page.getTotalElements()); System.out.println("总页数:" + page.getTotalPages()); System.out.println("本页数据:" + page.getContent()); }
虽然基本查询和自定义方法已经很强大了,但是如果是复杂查询(模糊、通配符、词条查询等)就显得力不从心了。此时,我们只能使用原生查询。
11.4.7.1 基本查询
先看看基本玩法
/** * 使用content的原生查询 * @param content * @return */ public List<Article> getByContentNative(String content) { MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("content", content); NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); queryBuilder.withQuery(matchQueryBuilder); SearchHits<Article> searchHits = elasticsearchRestTemplate.search(queryBuilder.build(), Article.class); return searchHits.get().map(SearchHit::getContent).collect(Collectors.toList()); }
测试方法
@Test public void getByContentNative() { List<Article> articleList = articleService.getByContentNative("干就完了,奥利给"); System.out.println(articleList); }
QueryBuilders提供了大量的静态方法,用于生成各种不同类型的查询对象,例如:词条、模糊、通配符等QueryBuilder对象。
NativeSearchQueryBuilder:Spring提供的一个查询条件构建器,帮助构建json格式的请求体
11.4.7.2 分页查询
利用NativeSearchQueryBuilder
可以方便的实现分页:
public com.example.pojo.Page<Article> getPageByContentNative(String content, String title, int page, int size) { NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 构造queryBuilder BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.should(QueryBuilders.matchQuery("content", content)); boolQueryBuilder.should(QueryBuilders.matchQuery("title", title)); queryBuilder.withQuery(boolQueryBuilder); queryBuilder.withPageable(PageRequest.of(page - 1, size)); SearchHits<Article> searchHits = elasticsearchRestTemplate.search(queryBuilder.build(), Article.class); // 获取查询的数据 List<Article> articleList = searchHits.get().map(SearchHit::getContent).collect(Collectors.toList()); // 获取总条数 long totalHits = searchHits.getTotalHits(); return new com.example.pojo.Page<Article>(totalHits, size, articleList); }
测试方法
@Test public void getPageByContentNative() { com.example.pojo.Page<Article> page = articleService.getPageByContentNative("奥利给,Java,PHP,文档", "奥利给,Java,PHP,文档", 1, 2); System.out.println("总条数:" + page.getTotalCount()); System.out.println("总页数:" + page.getTotalPage()); System.out.println("本页数据:"); page.getList().forEach(System.out::println); }
可以发现,Elasticsearch中的分页是从第0页开始。
11.4.7.3 排序
排序也通用通过NativeSearchQueryBuilder
完成:
public com.example.pojo.Page<Article> getPageByContentNativeSort(String content, String title, int page, int size) { NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 构造queryBuilder BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.should(QueryBuilders.matchQuery("content", content)); boolQueryBuilder.should(QueryBuilders.matchQuery("title", title)); queryBuilder.withQuery(boolQueryBuilder); queryBuilder.withPageable(PageRequest.of(page - 1, size)); // 构造排序对象 queryBuilder.withSort(SortBuilders.fieldSort("read").order(SortOrder.DESC)); SearchHits<Article> searchHits = elasticsearchRestTemplate.search(queryBuilder.build(), Article.class); // 获取查询的数据 List<Article> articleList = searchHits.get().map(SearchHit::getContent).collect(Collectors.toList()); // 获取总条数 long totalHits = searchHits.getTotalHits(); return new com.example.pojo.Page<Article>(totalHits, size, articleList); }
测试方法
@Test public void getPageByContentNativeSort() { com.example.pojo.Page<Article> page = articleService.getPageByContentNativeSort("奥利给,Java,PHP,文档", "奥利给,Java,PHP,文档", 1, 2); System.out.println("总条数:" + page.getTotalCount()); System.out.println("总页数:" + page.getTotalPage()); System.out.println("本页数据:"); page.getList().forEach(System.out::println); }