Lucene介紹和建立索引和搜索初步

Lucene是apache軟件基金會4 jakarta項目組的一個子項目,是一個開放源代碼的全文檢索引擎工具包,即它不是一個完整的全文檢索引擎,而是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎,部分文本分析引擎(英文與德文兩種西方語言)。Lucene的目的是爲軟件開發人員提供一個簡單易用的工具包,以方便的在目標系統中實現全文檢索的功能,或者是以此爲基礎創建起完整的全文檢索引擎。java

以上介紹來自百度百科。apache

在全文索引工具中,都是由三部分組成的:架構

1,索引部分ide

2,分詞部分工具

3,搜索部分學習

下面進入Lucene的學習,由lucene建立索引測試

建立工程引入jar包lucene-core-3.6.2.jarspa

HelloLucene.java開放源代碼

 

  
  
           
  
  
  1. import java.io.File; 
  2. import java.io.FileReader; 
  3. import java.io.IOException; 
  4. import java.io.Reader; 
  5.  
  6. import org.apache.lucene.analysis.Analyzer; 
  7. import org.apache.lucene.analysis.TokenStream; 
  8. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  9. import org.apache.lucene.document.Document; 
  10. import org.apache.lucene.document.Field; 
  11. import org.apache.lucene.index.CorruptIndexException; 
  12. import org.apache.lucene.index.IndexReader; 
  13. import org.apache.lucene.index.IndexWriter; 
  14. import org.apache.lucene.index.IndexWriterConfig; 
  15. import org.apache.lucene.queryParser.ParseException; 
  16. import org.apache.lucene.queryParser.QueryParser; 
  17. import org.apache.lucene.search.IndexSearcher; 
  18. import org.apache.lucene.search.Query; 
  19. import org.apache.lucene.search.ScoreDoc; 
  20. import org.apache.lucene.search.TopDocs; 
  21. import org.apache.lucene.store.Directory; 
  22. import org.apache.lucene.store.FSDirectory; 
  23. import org.apache.lucene.store.LockObtainFailedException; 
  24. import org.apache.lucene.store.RAMDirectory; 
  25. import org.apache.lucene.util.Version; 
  26.  
  27.  
  28. public class HelloLucene { 
  29.     /** 
  30.      * 創建索引 
  31.      */ 
  32.     public void index(){ 
  33.         IndexWriter writer = null
  34.          
  35.         try { 
  36.             //1,建立詞典 
  37. //          Directory directory = new RAMDirectory();//存儲到內存 
  38.             Directory directory = FSDirectory.open(new File("D:\\lucene\\index")); 
  39.             //2,建立IndexWriter索引筆 
  40.             IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)); 
  41.             writer = new IndexWriter(directory, config); 
  42.  
  43.             //3,建立Document對象 
  44.             Document doc = null
  45.             File file = new File("d:/lucene/file"); 
  46.             for(File f : file.listFiles()){ 
  47.                 doc = new Document(); 
  48.                 //4,爲Document添加Field 
  49.                 doc.add(new Field("content",new FileReader(f))); 
  50.                 doc.add(new Field("filename",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  51.                 doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  52.                 //5,經過IndexWriter添加文檔到索引中 
  53.                 writer.addDocument(doc); 
  54.             } 
  55.         } catch (CorruptIndexException e) { 
  56.             // TODO Auto-generated catch block 
  57.             e.printStackTrace(); 
  58.         } catch (LockObtainFailedException e) { 
  59.             // TODO Auto-generated catch block 
  60.             e.printStackTrace(); 
  61.         } catch (IOException e) { 
  62.             // TODO Auto-generated catch block 
  63.             e.printStackTrace(); 
  64.         }finally
  65.             if(writer!=null){ 
  66.                 try { 
  67.                     writer.close(); 
  68.                 } catch (CorruptIndexException e) { 
  69.                     // TODO Auto-generated catch block 
  70.                     e.printStackTrace(); 
  71.                 } catch (IOException e) { 
  72.                     // TODO Auto-generated catch block 
  73.                     e.printStackTrace(); 
  74.                 } 
  75.             } 
  76.         } 
  77.     } 
  78.     /** 
  79.      * 搜索 
  80.      */ 
  81.     public void search(){ 
  82.         IndexReader reader = null
  83.         try { 
  84.             //1,建立Directory 
  85.             Directory directory = FSDirectory.open(new File("d:/lucene/index")); 
  86.             //2,建立IndexReader 
  87.             reader = IndexReader.open(directory); 
  88.             //3,根據IndexReader建立IndexSearcher 
  89.             IndexSearcher searcher = new IndexSearcher(reader); 
  90.             //4,建立搜索的Query 
  91.             QueryParser parser = new QueryParser(Version.LUCENE_35, "content"new StandardAnalyzer(Version.LUCENE_35)); 
  92.             Query query = parser.parse("Apache License"); 
  93.             //5,根據searcher搜索並返回TopDocs 
  94.             TopDocs tds = searcher.search(query, 10); 
  95.             //6,根據TopDocs獲取ScoreDoc對象 
  96.             ScoreDoc[] sds = tds.scoreDocs; 
  97.             //7,根據searcher和ScoreDoc對象獲取具體的Document對象 
  98.             for(ScoreDoc sd:sds){ 
  99.                 Document doc = searcher.doc(sd.doc); 
  100.                 //8,根據Document對象獲取須要的值 
  101.                 System.out.println(doc.get("filename")+":"+doc.get("path")); 
  102.             } 
  103.         } catch (IOException e) { 
  104.             // TODO Auto-generated catch block 
  105.             e.printStackTrace(); 
  106.         } catch (ParseException e) { 
  107.             // TODO Auto-generated catch block 
  108.             e.printStackTrace(); 
  109.         }finally
  110.             //9,關閉reader 
  111.             if(reader!=null){ 
  112.                 try { 
  113.                     reader.close(); 
  114.                 } catch (IOException e) { 
  115.                     // TODO Auto-generated catch block 
  116.                     e.printStackTrace(); 
  117.                 } 
  118.             } 
  119.         } 
  120.          
  121.     } 
測試 

  
  
           
  
  
  1. import static org.junit.Assert.*; 
  2.  
  3. import org.junit.BeforeClass; 
  4. import org.junit.Test; 
  5.  
  6.  
  7. public class TestCase { 
  8.  
  9.     @BeforeClass 
  10.     public static void setUpBeforeClass() throws Exception { 
  11.     } 
  12.  
  13.     @Test 
  14.     public void testIndex() { 
  15.         new HelloLucene().index(); 
  16.     } 
  17.      
  18.     @Test 
  19.     public void testSearch(){ 
  20.         new HelloLucene().search(); 
  21.     } 
相關文章
相關標籤/搜索