课程的Controller
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@RequestMapping(value="/queryCourses",method= RequestMethod.POST) public AjaxResult queryCourses(@RequestBody CourseQuery courseQuery){ try { return courseService.queryCourses(courseQuery); } catch (Exception e) { e.printStackTrace(); return AjaxResult.me().setMessage("保存对象失败!"+e.getMessage()); } }
service的这里有一个问题就是
@RequestBody这个标签只能传入简单的数据传输简单的字段不能够传输ES封装的字段,所以我们需要把查询条件传入ES的Controller中封装查询条件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 查找课程】 * 从ES中找到课程 * 这里我们不能够在这里封装查询对象 * Java传输的是否只能够传输简单的对象所以我们需要在ES中封装数据 * @param courseQuery * @return */ @Override public AjaxResult queryCourses(CourseQuery courseQuery) { return esCourseClient.search(courseQuery); }
这里没什么写的主要在ES微服务中
ES的Controller
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 从ES中查询数据保存显示到前端 * queryCourses * @return */ @PostMapping("/es/search") public AjaxResult search(@RequestBody CourseQuery courseQuery){ try { PageList<ESCourseDoc> pageList= esCourseService.search(courseQuery); System.out.println(pageList+"controller"); return AjaxResult.me().setResultObj(pageList); } catch (Exception e) { e.printStackTrace(); return AjaxResult.me().setSuccess(false).setResultObj("查询失败"); } }
ES的service看注释即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 从ES中找到数据 * 1.获取条件 * 2.封装数据 * 3.保存数据 * 4.获得查询对象 * @param courseQuery * @return */ @Override public PageList<ESCourseDoc> search(CourseQuery courseQuery) { NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); //2.把courseQuery中的查询设置给Builder BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); //查询关键字 System.out.println(courseQuery.getKeyword()); if(StringUtils.isNotEmpty(courseQuery.getKeyword())){ boolQueryBuilder.must(QueryBuilders.matchQuery("searchField",courseQuery.getKeyword())); } //判断课程类型,精确匹配 if(null != courseQuery.getProductType()){ boolQueryBuilder.filter(QueryBuilders.termQuery("courseTypeId",courseQuery.getProductType())); } //判断范围需要分开判断 if(null != courseQuery.getPriceMin()){ boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMin())); } if(null != courseQuery.getPriceMax()){ boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").lte(courseQuery.getPriceMax())); } builder.withQuery(boolQueryBuilder); //3.把courseQuery中的分页 builder.withPageable(PageRequest.of(courseQuery.getPage() - 1 , courseQuery.getRows())); //4.把courseQuery中的排序设置给Builder if(StringUtils.isNotEmpty(courseQuery.getSortField())){ //排序编号 String sortFieldSN = courseQuery.getSortField(); //排序的字段 String sortField = null; //排序编号变成排序字段 switch (sortFieldSN){ case "xl": sortField = "saleCount"; break; case "xp": sortField = "onLineTime"; break; case "pl": sortField = "commentCount"; break; case "jg": sortField = "price"; break; case "rq": sortField = "viewCount"; break; } //排序方式 SortOrder sortOrder = SortOrder.DESC; if(StringUtils.isNotEmpty(courseQuery.getSortType()) && courseQuery.getSortType().toLowerCase().equals("asc")){ sortOrder = SortOrder.ASC; } builder.withSort(new FieldSortBuilder(sortField).order(sortOrder)); } //5.执行查询得到结果DOC对象 Page<ESCourseDoc> page = courseElasticsearchRepository.search(builder.build()); System.out.println(page+"service"); //6.把总天数和每天的内容封装进去 return new PageList<>(page.getTotalElements(),page.getContent()); }
然后通过fegin接口调用即可