Lucene全文检索
Posted 彼扬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucene全文检索相关的知识,希望对你有一定的参考价值。
Lucene写入和更新操作:
if (id.equals("")) { 2 this.goodsService.save(goods); 3 4 String goods_lucene_path = System.getProperty("user.dir") 5 + File.separator + "luence" + File.separator + "goods"; 6 File file = new File(goods_lucene_path); 7 if (!file.exists()) { 8 CommUtil.createFolder(goods_lucene_path); 9 } 10 LuceneVo vo = new LuceneVo(); 11 vo.setVo_id(goods.getId()); 12 vo.setVo_title(goods.getGoods_name()); 13 vo.setVo_content(goods.getGoods_details()); 14 vo.setVo_type("goods"); 15 vo.setVo_store_price(CommUtil.null2Double(goods 16 .getStore_price())); 17 vo.setVo_add_time(goods.getAddTime().getTime()); 18 vo.setVo_goods_salenum(goods.getGoods_salenum()); 19 LuceneUtil lucene = LuceneUtil.instance(); 20 LuceneUtil.setIndex_path(goods_lucene_path); 21 lucene.writeIndex(vo); 22 } else { 23 this.goodsService.update(goods); 24 25 String goods_lucene_path = System.getProperty("user.dir") 26 + File.separator + "luence" + File.separator + "goods"; 27 File file = new File(goods_lucene_path); 28 if (!file.exists()) { 29 CommUtil.createFolder(goods_lucene_path); 30 } 31 LuceneVo vo = new LuceneVo(); 32 vo.setVo_id(goods.getId()); 33 vo.setVo_title(goods.getGoods_name()); 34 vo.setVo_content(goods.getGoods_details()); 35 vo.setVo_type("goods"); 36 vo.setVo_store_price(CommUtil.null2Double(goods 37 .getStore_price())); 38 vo.setVo_add_time(goods.getAddTime().getTime()); 39 vo.setVo_goods_salenum(goods.getGoods_salenum()); 40 LuceneUtil lucene = LuceneUtil.instance(); 41 LuceneUtil.setIndex_path(goods_lucene_path); 42 lucene.update(CommUtil.null2String(goods.getId()), vo); 43 }
Lucene查询:
LuceneResult pList = lucene.search(keyword, CommUtil.null2Int(currentPage), CommUtil.null2Int(store_price_begin), CommUtil.null2Int(store_price_end), null, sort); for (LuceneVo vo : pList.getVo_list()) { Goods goods = this.goodsService.getObjById(vo.getVo_id()); pList.getGoods_list().add(goods); } CommUtil.saveLucene2ModelAndView("goods", pList, mv);
public static void saveLucene2ModelAndView(String type, LuceneResult pList, ModelAndView mv) { if (pList != null) { if (type.equals("goods")) { mv.addObject("objs", pList.getGoods_list()); } if (type.equals("store")) { mv.addObject("objs", pList.getStore_list()); } mv.addObject("totalPage", Integer.valueOf(pList.getPages())); mv.addObject("pageSize", Integer.valueOf(pList.getPageSize())); mv.addObject("rows", Integer.valueOf(pList.getRows())); mv.addObject("currentPage", new Integer(pList.getCurrentPage())); mv.addObject( "gotoPageFormhtml", showPageFormHtml(pList.getCurrentPage(), pList.getPages())); } }
以上是关于Lucene全文检索的主要内容,如果未能解决你的问题,请参考以下文章