lucene 的索引建立

對lucene的學習之建立索引

需要lucene3.03.zip

先創建一個接口:

  1. package cn.net.persist.dao; 
  2.  
  3. import org.apache.lucene.analysis.Analyzer; 
  4. import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; 
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  6. import org.apache.lucene.index.IndexWriter.MaxFieldLength; 
  7. import org.apache.lucene.util.Version; 
  8.  
  9.  
  10. public interface Constants { 
  11.      
  12.     public String FILE_DIR = "D:\\Workspaces\\MyEclipse 8.5\\luceneDemo\\dataSource";//文件存放的目錄 
  13.     public String INDEX_DIR = "D:\\Workspaces\\MyEclipse 8.5\\luceneDemo\\index";   //索引存放的目錄 
  14.      
  15.     static Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_30);// 中文采用該分詞器 
  16.     static Analyzer enAnalyzer = new StandardAnalyzer(Version.LUCENE_30);// 英文采用該分詞器 
  17.  
  18.     static Version version = Version.LUCENE_30;//Lucene版本 
  19.     static MaxFieldLength maxLength = MaxFieldLength.LIMITED;//Field的長度限制 

然後寫一個工具類:

 

  1. package cn.net.persist.utils; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.InputStreamReader; 
  7.  
  8. import org.apache.lucene.document.Document; 
  9. import org.apache.lucene.document.Field; 
  10. import org.apache.lucene.document.Field.Index; 
  11. import org.apache.lucene.document.Field.Store; 
  12.  
  13. public class File2DocumentUtils { 
  14.  
  15.  
  16.      
  17.  
  18.     /** 
  19.     * @param path 
  20.     */ 
  21.     public static Document file2Document(String path) { 
  22.        File file = new File(path); 
  23.  
  24.        Document doc = new Document(); 
  25.        doc.add(new Field("filename", file.getName(), Store.YES, Index.ANALYZED)); 
  26.        doc.add(new Field("contents", readFileContent(file), Store.YES, Index.ANALYZED)); 
  27.        doc.add(new Field("size", file.length() + "", Store.YES, Index.NOT_ANALYZED)); 
  28.        doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED)); 
  29.  
  30.        return doc; 
  31.     } 
  32.  
  33.     /** 
  34.     * @param file 
  35.     */ 
  36.     public static Document file2Document(File file) throws Exception { 
  37.      
  38.        
  39.        Document doc = new Document(); 
  40.        doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED)); 
  41.        // doc.add(new Field("contents", new FileReader(f)));// that is not stored. 
  42.        doc.add(new Field("contents", readFileContent(file), Store.YES, Index.ANALYZED)); 
  43.        doc.add(new Field("size", file.length() + "", Field.Store.YES, Field.Index.NOT_ANALYZED)); 
  44.        
  45.        return doc; 
  46.     } 
  47.  
  48.     /** 
  49.     * 
  50.     * 獲取 name 屬性的值的兩種方法: 
  51.     * 
  52.     * <pre> 
  53.     * 1,Field f = doc.getField(&quot;name&quot;); 
  54.     *      f.stringValue(); 
  55.     * 2,doc.get(&quot;name&quot;); 
  56.     * </pre> 
  57.     * 
  58.     * @param doc 
  59.     */ 
  60.     public static void printDocumentInfo(Document doc) { 
  61.        System.out.println("filename     = " + doc.get("filename")); 
  62.        System.out.println("contents = " + doc.get("contents")); 
  63.        System.out.println("size     = " + doc.getField("size")); 
  64.        System.out.println("path     = " + doc.getField("path")); 
  65.     } 
  66.  
  67.  
  68.     /** 
  69.     * 
  70.     * 讀取文件內容 
  71.     * @param file 
  72.     */ 
  73.     public static String readFileContent(File file) { 
  74.        try { 
  75.         InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "gbk"); 
  76.         BufferedReader reader = new BufferedReader(inputStreamReader); 
  77.         StringBuffer content = new StringBuffer(); 
  78.         for (String line = null; (line = reader.readLine()) != null;) { 
  79.          content.append(line).append("\n"); 
  80.         } 
  81.         return content.toString(); 
  82.        } catch (Exception e) { 
  83.         throw new RuntimeException(e); 
  84.        } 
  85.     } 

最後寫測試方法:

 

  1. package cn.net.persist.dir; 
  2.  
  3. import java.io.File; 
  4.  
  5. import org.apache.lucene.document.Document; 
  6. import org.apache.lucene.index.IndexWriter; 
  7. import org.apache.lucene.store.Directory; 
  8. import org.apache.lucene.store.FSDirectory; 
  9. import org.apache.lucene.store.RAMDirectory; 
  10. import org.junit.Test; 
  11.  
  12. import cn.net.persist.dao.Constants; 
  13. import cn.net.persist.utils.File2DocumentUtils; 
  14.  
  15. public class Directorys implements Constants{ 
  16.     private static String filePath = "D:\\Workspaces\\MyEclipse 8.5\\luceneDemo\\dataSource\\xiaoli.txt"
  17.      
  18.     /** 
  19.     * 把文件寫入索引 
  20.     */ 
  21.     @Test 
  22.     public void test1() throws Exception { 
  23.        Directory fsDir = FSDirectory.open(new File(INDEX_DIR)); 
  24.        Document doc = File2DocumentUtils.file2Document(filePath); 
  25.  
  26.        // 寫入索引 
  27.        IndexWriter indexWriter = new IndexWriter(fsDir, analyzer, maxLength); 
  28.        indexWriter.addDocument(doc); 
  29.  
  30.        // 優化索引 
  31.        indexWriter.optimize(); 
  32.        indexWriter.close(); 
  33.  
  34.        // 打印相關信息 
  35.        File2DocumentUtils.printDocumentInfo(doc); 
  36.     } 
  37.      
  38.      
  39.     /** 
  40.      * 把磁盤索引加載到內存當中讀寫 
  41.      */ 
  42.     @Test 
  43.     public void test2() throws Exception{ 
  44.         Directory fsDir =FSDirectory.open(new File(INDEX_DIR)); 
  45.         //1,設置啓動時讀取 
  46.         Directory ramDir = new RAMDirectory(fsDir); 
  47.         //2,運行程序是操作ramDir 
  48.         IndexWriter ramIndexWriter = new IndexWriter(ramDir,analyzer,maxLength); 
  49.         //添加Document 
  50.         Document doc = File2DocumentUtils.file2Document(filePath); 
  51.         ramIndexWriter.addDocument(doc); 
  52.         ramIndexWriter.close(); 
  53.         //4,把內存中的修改同步到磁盤文件 
  54.         IndexWriter fsIndexWriter = new IndexWriter(fsDir,analyzer,true,maxLength); 
  55.         fsIndexWriter.addIndexesNoOptimize(ramDir); 
  56.         fsIndexWriter.optimize();//優化 
  57.         fsIndexWriter.commit();//提交事務 
  58.         System.out.println("是否有刪除="+fsIndexWriter.hasDeletions()); 
  59.         System.out.println("一共有="+fsIndexWriter.maxDoc()); 
  60.         System.out.println("還剩="+fsIndexWriter.numDocs()); 
  61.         fsIndexWriter.close(); 
  62.     } 
  63.      

測試成功,在控制檯輸出:

 

  1. filename     = xiaoli.txt 
  2. contents = 1 會員登陸沒有驗證碼問題: 
  3.     因爲在提交的時候代碼沒有對驗證碼處理,所以,當沒有驗證碼的時候,也可以通過驗證,進而實現登陸 
  4. 2 個人會員註冊問題: 
  5.     註冊會失敗,因爲沒有驗證碼的原因,所以把驗證碼加上的時候,就可以實現會員的註冊 
  6. 3 企業會員註冊問題 
  7.     企業會員的註冊可以放在個人會員的時候,讓用戶選擇註冊時個人會員還是企業會員,這樣在action中稍加判斷就可以實現個人和企業的註冊, 
  8. 4 當登陸後臺的時候,修改會員組的時候,會出現亂碼問題,處理中 
  9.  
  10. size     = stored,indexed<size:437> 
  11. path     = stored,indexed<path:D:\Workspaces\MyEclipse 8.5\luceneDemo\dataSource\xiaoli.txt> 
  12. ------------------------------------------------ 
  13. 是否有刪除=false 
  14. 一共有=13 
  15. 還剩=13
  16.  

哇,寫完,收工,希望大家能學到東西!

 

(因附件大過4m,沒能上傳成功,需要的給我要)

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