刚开始使用spring data elasticsearch 会有有一个疑问就是:能不能像操作mysql那样打印sql日志,也便于分析啊,其实特别简单,具体如下的代码所示:
/** * 高亮显示 * @param keyword * @param pageNum * @param pageSize * @return */ @GetMapping("/getUserList2") public Page<User> query3( String keyword, Integer pageNum, Integer pageSize) { if(pageNum == null || pageNum <= 0){ pageNum = 1; } if(pageSize == null || pageSize <= 0){ pageSize = 5; } //获取QueryBuilder BoolQueryBuilder querryBuilder = this.getQuerryBuilder(keyword,"三1006"); NativeSearchQueryBuilder nativeSearchQueryBuilder = this.getWildcardQuery(keyword, "myes", "myuser"); //关联分页、过滤器 nativeSearchQueryBuilder .withIndices("myes") .withTypes("myuser") //添加bool过滤器 .withFilter(querryBuilder) .withSort(SortBuilders.fieldSort("age").order(SortOrder.DESC)) //添加分页条件 .withPageable(PageRequest.of(pageNum - 1, pageSize)); //查询结果 NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build(); //打印filter 的dsl语句 log.info(searchQuery.getFilter().toString()); System.out.println("================="); //log.info(searchQuery.get); //打印query的 的dsl语句 System.out.println(nativeSearchQueryBuilder.build().getQuery().toString()); AggregatedPage<User> esEntityList = estemplate.queryForPage(nativeSearchQueryBuilder.build(), User.class); return esEntityList; }