lucene 簡單的例子

使用lucene-core-3.1.0.jar

一 創建索引

  1. public class Indexer {  
  2.     public static void main(String[] args) throws IOException {  
  3.         // 保存索引文件的地方  
  4.         String indexDir = "F://indexDir";  
  5.         // 將要搜索TXT文件的地方  
  6.         String dateDir = "F://dateDir";  
  7.         IndexWriter indexWriter = null;  
  8.         // 創建Directory對象  
  9.         Directory dir = new SimpleFSDirectory(new File(indexDir));  
  10.         // 創建IndexWriter對象,第一個參數是Directory,第二個是分詞器,第三個表示是否是創建,如果爲false爲在此基礎上面修改,第四表示表示分詞的最大值,比如說new  
  11.         // MaxFieldLength(2),就表示兩個字一分,一般用IndexWriter.MaxFieldLength.LIMITED  
  12.         indexWriter = new IndexWriter(dir, new StandardAnalyzer(  
  13.                 Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);  
  14.         File[] files = new File(dateDir).listFiles();  
  15.         for (int i = 0; i < files.length; i++) {  
  16.             Document doc = new Document();  
  17.             // 創建Field對象,並放入doc對象中  
  18.             doc.add(new Field("contents"new FileReader(files[i])));  
  19.             doc.add(new Field("filename", files[i].getName(), Field.Store.YES,  
  20.                     Field.Index.NOT_ANALYZED));  
  21.             doc.add(new Field("indexDate", DateTools.dateToString(new Date(),  
  22.                     DateTools.Resolution.DAY), Field.Store.YES,  
  23.                     Field.Index.NOT_ANALYZED));  
  24.             // 寫入IndexWriter  
  25.             indexWriter.addDocument(doc);  
  26.         }  
  27.         // 查看IndexWriter裏面有多少個索引  
  28.         System.out.println("numDocs/t" + indexWriter.numDocs());  
  29.         indexWriter.close();  
  30.     }  
  31. }  

2 索引文件

  1. public class Seacher {  
  2.     public static void main(String[] args) throws IOException, ParseException {  
  3.         // 保存索引文件的地方  
  4.         String indexDir = "F://indexDir";  
  5.         Directory dir = new SimpleFSDirectory(new File(indexDir));  
  6.         // 創建 IndexSearcher對象,相比IndexWriter對象,這個參數就要提供一個索引的目錄就行了  
  7.         IndexSearcher indexSearch = new IndexSearcher(dir);  
  8.         // 創建QueryParser對象,第一個參數表示Lucene的版本,第二個表示搜索Field的字段,第三個表示搜索使用分詞器  
  9.         QueryParser queryParser = new QueryParser(Version.LUCENE_30,  
  10.                 "contents"new StandardAnalyzer(Version.LUCENE_30));  
  11.         // 生成Query對象  
  12.         Query query = queryParser.parse("pause");  
  13.         // 搜索結果 TopDocs裏面有scoreDocs[]數組,裏面保存着索引值  
  14.         TopDocs hits = indexSearch.search(query, 3);  
  15.         // hits.totalHits表示一共搜到多少個  
  16.         System.out.println("找到了" + hits.totalHits + "個");  
  17.         // 循環hits.scoreDocs數據,並使用indexSearch.doc方法把Document還原,再拿出對應的字段的值  
  18.         for (int i = 0; i < hits.scoreDocs.length; i++) {  
  19.             ScoreDoc sdoc = hits.scoreDocs[i];  
  20.             Document doc = indexSearch.doc(sdoc.doc);  
  21.             System.out.println(doc.get("filename") + "/t"  
  22.                     + hits.scoreDocs[i].score);  
  23.         }  
  24.         indexSearch.close();  
  25.     }  
  26. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章