Lucene实战之初体验
生活随笔
收集整理的這篇文章主要介紹了
Lucene实战之初体验
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
? ? ?最早做非結構化數據搜索時用的還是lucene.net,一直說在學習java的同時把lucene這塊搞一搞,這拖了2年多了,終于開始搞這塊了。
?
開發環境
? ? idea2016、lucene6.0、jdk1.8
?
使用lucene準備條件
1、pom.xml
2、測試數據。 ?我從博客園首頁拿了幾條數據,直接寫了個偽Dao返回一個List<Blog>
public List<Blog> listBlogs(){List<Blog> list = new ArrayList<Blog>();Blog b1=new Blog(6538488,"風過無痕-唐","你真的了解volatile嗎,關于volatile的那些事","http://www.cnblogs.com/tangyanbo/p/6538488.html");Blog b2=new Blog(6541312,"布魯克石","TypeScript設計模式之職責鏈、狀態","http://www.cnblogs.com/brookshi/p/6541312.html");Blog b7=new Blog(6547215,"嶸么么","輕松理解JavaScript閉包","http://www.cnblogs.com/rongmm/p/6547215.html");Blog b3=new Blog(6541220,"一線碼農","使用Task的一些知識優化了一下同事的多線程協作取消的一串代碼","http://www.cnblogs.com/huangxincheng/p/6541220.html");Blog b4=new Blog(6516387,"xybaby","并發與同步、信號量與管程、生產者消費者問題","http://www.cnblogs.com/xybaby/p/6516387.html");Blog b5=new Blog(6532713,"王福朋","深入理解 JavaScript 異步系列(4)—— Generator","http://www.cnblogs.com/wangfupeng1988/p/6532713.html");Blog b6=new Blog(6517788,"歡醉","入坑系列之HAProxy負載均衡","http://www.cnblogs.com/zhangs1986/p/6517788.html");list.add(b1);list.add(b2);list.add(b3);list.add(b4);list.add(b5);list.add(b6);list.add(b7);return list;}?
IndexSearcher搜索
1、搜索體驗,基于servlet開發,直接接受路徑參數key
2、先寫入索引
public class IndexWriterServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String index_path="d:\\indexDir";Directory dir=null;Analyzer analyzer=null;IndexWriterConfig config=null;IndexWriter indexWriter=null;try {dir =new SimpleFSDirectory(Paths.get(index_path));analyzer=new StandardAnalyzer();config =new IndexWriterConfig(analyzer);indexWriter =new IndexWriter(dir,config);BlogDao blogDao=new BlogDao();List<Blog> list = blogDao.listBlogs();for (Blog blog:list){Document document=new Document();document.add(new Field("id",blog.getId().toString(),TextField.TYPE_STORED));document.add(new Field("author",blog.getAuthor(),TextField.TYPE_STORED));document.add(new Field("title",blog.getTitle(),TextField.TYPE_STORED));document.add(new Field("url",blog.getUrl(),TextField.TYPE_STORED));indexWriter.addDocument(document);}indexWriter.commit();System.out.println("====索引創建完成====");}catch (Exception ex){ex.printStackTrace();}finally {if(indexWriter !=null){indexWriter.close();}}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp);} }2、查詢索引數據
public class IndexReaderServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Directory dir =null;DirectoryReader reader =null;IndexSearcher searcher=null;String index_path="d:\\indexDir";try{//dir =new RAMDirectory();dir =new SimpleFSDirectory(Paths.get(index_path));reader = DirectoryReader.open(dir);searcher=new IndexSearcher(reader);String key = req.getParameter("key");Query query = new TermQuery(new Term("title",key));TopDocs rs=searcher.search(query,100);resp.setContentType("text/html");resp.setCharacterEncoding("UTF-8");PrintWriter out = resp.getWriter();StringBuilder strHtml =new StringBuilder();for (int i = 0; i < rs.scoreDocs.length; i++) {Document firstHit = searcher.doc(rs.scoreDocs[i].doc);strHtml.append("title:"+firstHit.getField("title").stringValue()+"<br>");strHtml.append("author:"+firstHit.getField("author").stringValue()+"<br>");strHtml.append("<a href='"+firstHit.getField("url").stringValue()+"'>"+ firstHit.getField("url").stringValue()+"</a><br>");strHtml.append("====================================================<br>");}out.write(strHtml.toString());}catch (Exception ex){ex.printStackTrace();}finally {reader.close();}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp);} }
總結
? ? ?讀寫索引用到的幾個關鍵類:IndexWriter、IndexSearcher、Directory、DirectoryReader、Document、TermQuery、Field
轉載于:https://www.cnblogs.com/sword-successful/p/6547965.html
總結
以上是生活随笔為你收集整理的Lucene实战之初体验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光纤温度传感器在电力系统的应用
- 下一篇: 1.I2C协议