lucene3.5学习笔记02--创建索引和建立搜索
生活随笔
收集整理的這篇文章主要介紹了
lucene3.5学习笔记02--创建索引和建立搜索
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先大致了解一下lucene的組成結構
lucene的組成結構:對于外部應用來說索引模塊(index)和檢索模塊(search)是主要的外部應用入口
| org.apache.Lucene.search/ | 搜索入口 |
| org.apache.Lucene.index/ | 索引入口 |
| org.apache.Lucene.analysis/ | 語言分析器 |
| org.apache.Lucene.queryParser/ | 查詢分析器 |
| org.apache.Lucene.document/ | 存儲結構 |
| org.apache.Lucene.store/? | 底層IO/存儲結構 |
| org.apache.Lucene.util/ | 一些公用的數據結構 |
接下來,我們構建一個最簡單的文件搜索樣例
E:\lucene\data?????????? 用來存放數據,代表要搜索的文件
E:\lucene\index???????? 原來存放lucene為數據創建的索引文件
E:\lucene\data\1.txt??????? 內容為 a1a2a3?
E:\lucene\data\2.txt??????? 內容為 b1b2b3
E:\lucene\data\3.txt??????? 內容為 c1c2c3 honor
public static void createIndex(String filePath, String indexPath) throws IOException { Version version = Version.LUCENE_35; File indexFile = new File(indexPath); FSDirectory directory = FSDirectory.open(indexFile); IndexWriterConfig conf = new IndexWriterConfig(version, new SimpleAnalyzer(version)); IndexWriter writer = new IndexWriter(directory, conf); List<File> files = FileList.getFiles(filePath);// 獲取該路徑下所有文件 for(File file:files){ System.out.println("Indexing file " + file); // 構造Document對象 Document doc = new Document(); doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("uri", file.getPath(), Field.Store.YES, Field.Index.NO)); String text = FileText.getText(file);// 獲取該文件內容 doc.add(new Field("text", text, Field.Store.YES, Field.Index.ANALYZED));//將文件內容索引在text // 將文檔寫入索引 writer.addDocument(doc); } // 關閉寫索引器 writer.close(); }
public static void main(String[] args) { String filePath = "E:/lucene/data"; String indexPath = "E:/lucene/index"; // try{ createIndex(filePath, indexPath); }catch(IOException e){ e.printStackTrace(); } }
這時E:\lucene\index\ 目錄下生成的索引文件如下
public static void search(String keyword, String indexPath) throws CorruptIndexException, IOException, ParseException { Version version = Version.LUCENE_35; // 指向索引目錄的搜索器 File indexFile = new File(indexPath); FSDirectory directory = FSDirectory.open(indexFile); IndexReader reader = IndexReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); // 查詢解析器:使用和索引同樣的語言分析器 查詢text字段 QueryParser parser = new QueryParser(version, "text", new SimpleAnalyzer(version));// text 字段 Query query = parser.parse(keyword); // 搜索結果使用Hits存儲 TopDocs hits = searcher.search(query, null, 10); // 通過hits可以訪問到相應字段的數據和查詢的匹配度 System.out.println(hits.totalHits + " total results"); System.out.println("-----匹配結果如下------"); ScoreDoc[] scoredocs = hits.scoreDocs; for(int i = 0; i < scoredocs.length; i++){ ScoreDoc scoreDoc = scoredocs[i]; Document d = searcher.doc(scoreDoc.doc); String path = d.get("uri"); System.out.println(i + "--得分:" +scoreDoc.score +" 文件路徑:"+path); } searcher.close(); }
public static void main(String[] args) { String indexPath = "E:/lucene/index"; try{ // 搜索 honor 這個關鍵字 search("honor",indexPath); }catch(CorruptIndexException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }catch(ParseException e){ e.printStackTrace(); } }
控制臺輸出如下
| 1 total results -----匹配結果如下------ 0--得分:0.70273256 文件路徑:E:\lucene\data\3.txt |
怎么樣,利用lucene實現檢索很簡單吧
由于沒有涉及到中文,使用lucene自帶的分析器就可以了
要是中文還得使用中文分詞器,這個接下來再學習
轉載于:https://www.cnblogs.com/hercules9/archive/2012/03/04/2461396.html
總結
以上是生活随笔為你收集整理的lucene3.5学习笔记02--创建索引和建立搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ocr技术 识别高级验证码
- 下一篇: 【转】Win7安装Oracle10g经验