1.引入依赖(在入门环境搭建中已全部引入)
2.加入页面和资源
3.编写配置文件application.yml
spring: thymeleaf: cache: false #关闭缓存
4.pojo包下加入ResultModel实体类(做分页用)
/** * 自定义分页实体类 */ public class ResultModel { // 商品列表 private List<Sku> skuList; // 商品总数 private Long recordCount; // 总页数 private Long pageCount; // 当前页 private long curPage; /********* get/set方法 *********/ }
5.controller代码
@Controller @RequestMapping("/list") public class SearchController { @Autowired private SearchService searchService; /** * 搜索 * @param queryString 查询关键字 * @param price 查询价格范围 * @param page 当前页 * @return * @throws Exception */ @RequestMapping public ModelAndView query(String queryString, String price, Integer page) throws Exception { ModelAndView mv = new ModelAndView(); if (StringUtils.isEmpty(page) || page <= 0) { page = 1; } ResultModel resultModel = searchService.query(queryString, price, page); mv.addObject("result", resultModel); mv.addObject("queryString", queryString); mv.addObject("price", price); mv.setViewName("search"); return mv; } }
6.service代码
@Service public class SearchService { public final static Integer PAGE_SIZE = 20; /** * 分页查询 * @param queryString * @param price * @param page * @return * @throws Exception */ public ResultModel query(String queryString, String price, Integer page) throws Exception { ResultModel resultModel = new ResultModel(); Integer start = (page - 1) * PAGE_SIZE; Integer end = page * PAGE_SIZE; Analyzer analyzer = new IKAnalyzer(); BooleanQuery.Builder builder = new BooleanQuery.Builder(); QueryParser queryParser = new QueryParser("name", analyzer); Query query1 = null; if (StringUtils.isEmpty(queryString)) { query1 = queryParser.parse("*:*"); } else { query1 = queryParser.parse(queryString); } builder.add(query1, BooleanClause.Occur.MUST); if (!StringUtils.isEmpty(price)) { String[] split = price.split("-"); Query query2 = DoublePoint.newRangeQuery("price", Integer.parseInt(split[0]), Integer.parseInt(split[1])); builder.add(query2, BooleanClause.Occur.MUST); } Directory dir = FSDirectory.open(Paths.get("D:\lucene")); IndexReader indexReader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(indexReader); TopDocs topDocs = searcher.search(builder.build(), end); long total = topDocs.totalHits; resultModel.setRecordCount(total); ScoreDoc[] docs = topDocs.scoreDocs; List<Sku> skus = new ArrayList<>(); if (docs != null) { for (int i = start; i < end; i++) { // 通过文档id读取文档 Document document = searcher.doc(docs[i].doc); Sku sku = new Sku(); sku.setId(document.get("id")); sku.setName(document.get("name")); sku.setPrice(Double.parseDouble(document.get("price"))); sku.setImage(document.get("image")); sku.setCategoryName(document.get("categoryName")); sku.setBrandName(document.get("brandName")); skus.add(sku); } } resultModel.setSkuList(skus); resultModel.setCurPage(page); resultModel.setPageCount(total % PAGE_SIZE > 0 ? total / PAGE_SIZE + 1 : total / PAGE_SIZE); return resultModel; } }
7.编写启动类
@SpringBootApplication public class LuceneApplication { public static void main(String[] args) { SpringApplication.run(LuceneApplication.class, args); } }
8.测试.........