Lucene索引操作,刪除,更新,優化

  1. IndexUtil.java 
  2. import java.io.File; 
  3. import java.io.IOException; 
  4.  
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer; 
  6. import org.apache.lucene.document.Document; 
  7. import org.apache.lucene.document.Field; 
  8. import org.apache.lucene.index.CorruptIndexException; 
  9. import org.apache.lucene.index.IndexReader; 
  10. import org.apache.lucene.index.IndexWriter; 
  11. import org.apache.lucene.index.IndexWriterConfig; 
  12. import org.apache.lucene.index.Term; 
  13. import org.apache.lucene.store.Directory; 
  14. import org.apache.lucene.store.FSDirectory; 
  15. import org.apache.lucene.store.LockObtainFailedException; 
  16. import org.apache.lucene.util.Version; 
  17.  
  18.  
  19. public class IndexUtil { 
  20.     private String[] ids = {"1","2","3","4","5","6"}; 
  21.     private String[] emails = {"[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"}; 
  22.     private String[] content ={ 
  23.             "Welcome to my home"
  24.             "Hello Kenan"
  25.             "Good morning"
  26.             "Are you OK?"
  27.             "Yeah hahahahahahaha"
  28.             "I like foot ball" 
  29.     }; 
  30.     private int[] attachs = {1,4,6,2,3,8}; 
  31.     private String[] names = {"zhangsan","kenan","soukenan","Micheal","Liangchengpeng","Jah"}; 
  32.     //詞典  
  33.     private Directory directory = null
  34.     //寫入筆 
  35.     private IndexWriter writer = null
  36.     //文檔對象 
  37.     private Document doc = null
  38.     //讀取對象 
  39.     private IndexReader reader = null
  40.     public IndexUtil(){ 
  41.         try { 
  42.             this.directory = FSDirectory.open(new File("d:/lucene/index02")); 
  43.         } catch (IOException e) { 
  44.             // TODO Auto-generated catch block 
  45.             e.printStackTrace(); 
  46.         } 
  47.     } 
  48.     /** 
  49.      * 構建索引 
  50.      */ 
  51.     public void buildIndex(){ 
  52.         try { 
  53.             writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36))); 
  54.             for(int i=0;i<6;i++){ 
  55.                 doc = new Document(); 
  56.                 doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  57.                 doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  58.                 doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED)); 
  59.                 doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  60.                 writer.addDocument(doc); 
  61.             } 
  62.         } catch (CorruptIndexException e) { 
  63.             // TODO Auto-generated catch block 
  64.             e.printStackTrace(); 
  65.         } catch (LockObtainFailedException e) { 
  66.             // TODO Auto-generated catch block 
  67.             e.printStackTrace(); 
  68.         } catch (IOException e) { 
  69.             // TODO Auto-generated catch block 
  70.             e.printStackTrace(); 
  71.         }finally
  72.             if(writer != null ){ 
  73.                 try { 
  74.                     writer.close(); 
  75.                 } catch (CorruptIndexException e) { 
  76.                     // TODO Auto-generated catch block 
  77.                     e.printStackTrace(); 
  78.                 } catch (IOException e) { 
  79.                     // TODO Auto-generated catch block 
  80.                     e.printStackTrace(); 
  81.                 } 
  82.             } 
  83.         } 
  84.          
  85.     } 
  86.     /** 
  87.      * 查詢文檔信息 
  88.      */ 
  89.     public void query(){ 
  90.         try { 
  91.             this.reader = IndexReader.open(directory); 
  92.             System.out.println("maxDoc:"+reader.maxDoc()); 
  93.             System.out.println("numDocs:"+reader.numDocs()); 
  94.             System.out.println("delDocs:"+reader.numDeletedDocs()); 
  95.              
  96.         } catch (CorruptIndexException e) { 
  97.             // TODO Auto-generated catch block 
  98.             e.printStackTrace(); 
  99.         } catch (IOException e) { 
  100.             // TODO Auto-generated catch block 
  101.             e.printStackTrace(); 
  102.         } 
  103.     } 
  104.     /** 
  105.      * 刪除文檔 
  106.      */ 
  107.     public void deleteIndex(){ 
  108.         try { 
  109.             writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); 
  110. //          writer.deleteAll();//刪除所有索引 
  111.             writer.deleteDocuments(new Term("id","1")); //刪除到回收站 
  112.             //刪除id索引爲1的記錄 
  113.             //也可以傳一個query對象 
  114. //          writer.deleteDocuments(query); 
  115.         } catch (CorruptIndexException e) { 
  116.             // TODO Auto-generated catch block 
  117.             e.printStackTrace(); 
  118.         } catch (LockObtainFailedException e) { 
  119.             // TODO Auto-generated catch block 
  120.             e.printStackTrace(); 
  121.         } catch (IOException e) { 
  122.             // TODO Auto-generated catch block 
  123.             e.printStackTrace(); 
  124.         }finally
  125.             if(writer!=null){ 
  126.                 try { 
  127.                     writer.close(); 
  128.                 } catch (CorruptIndexException e) { 
  129.                     // TODO Auto-generated catch block 
  130.                     e.printStackTrace(); 
  131.                 } catch (IOException e) { 
  132.                     // TODO Auto-generated catch block 
  133.                     e.printStackTrace(); 
  134.                 } 
  135.             } 
  136.         } 
  137.          
  138.     } 
  139.     /** 
  140.      * 恢復刪除文檔 
  141.      */ 
  142.     public void unDeleteIndex(){ 
  143.         try { 
  144.             reader = IndexReader.open(directory,false); 
  145.             reader.undeleteAll();//回覆所有被刪除的文檔 
  146.         } catch (CorruptIndexException e) { 
  147.             // TODO Auto-generated catch block 
  148.             e.printStackTrace(); 
  149.         } catch (IOException e) { 
  150.             // TODO Auto-generated catch block 
  151.             e.printStackTrace(); 
  152.         }finally
  153.             if(reader!=null){ 
  154.                 try { 
  155.                     reader.close(); 
  156.                 } catch (IOException e) { 
  157.                     // TODO Auto-generated catch block 
  158.                     e.printStackTrace(); 
  159.                 } 
  160.             } 
  161.         } 
  162.     } 
  163.      
  164.     public void forceDelete(){ 
  165.         try { 
  166.             writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); 
  167.  
  168.             writer.forceMergeDeletes();//刪除回收站中的內容 
  169.         } catch (CorruptIndexException e) { 
  170.             // TODO Auto-generated catch block 
  171.             e.printStackTrace(); 
  172.         } catch (LockObtainFailedException e) { 
  173.             // TODO Auto-generated catch block 
  174.             e.printStackTrace(); 
  175.         } catch (IOException e) { 
  176.             // TODO Auto-generated catch block 
  177.             e.printStackTrace(); 
  178.         }finally
  179.             if(writer!=null){ 
  180.                 try { 
  181.                     writer.close(); 
  182.                 } catch (CorruptIndexException e) { 
  183.                     // TODO Auto-generated catch block 
  184.                     e.printStackTrace(); 
  185.                 } catch (IOException e) { 
  186.                     // TODO Auto-generated catch block 
  187.                     e.printStackTrace(); 
  188.                 } 
  189.             } 
  190.         } 
  191.     } 
  192.      
  193.     /** 
  194.      * 文檔索引更新 
  195.      */ 
  196.     public void merge(){ 
  197.         try { 
  198.             writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); 
  199.              //消耗資源,不建議使用
  200.             writer.forceMerge(2); 
  201.         } catch (CorruptIndexException e) { 
  202.             // TODO Auto-generated catch block 
  203.             e.printStackTrace(); 
  204.         } catch (LockObtainFailedException e) { 
  205.             // TODO Auto-generated catch block 
  206.             e.printStackTrace(); 
  207.         } catch (IOException e) { 
  208.             // TODO Auto-generated catch block 
  209.             e.printStackTrace(); 
  210.         }finally
  211.             if(writer!=null){ 
  212.                 try { 
  213.                     writer.close(); 
  214.                 } catch (CorruptIndexException e) { 
  215.                     // TODO Auto-generated catch block 
  216.                     e.printStackTrace(); 
  217.                 } catch (IOException e) { 
  218.                     // TODO Auto-generated catch block 
  219.                     e.printStackTrace(); 
  220.                 } 
  221.             } 
  222.         } 
  223.     } 
  224.     /** 
  225.      * 更新文檔 
  226.      */ 
  227.     public void update(){ 
  228.         try { 
  229.             writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36))); 
  230.             doc = new Document(); 
  231.             doc.add(new Field ("id","10",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  232.             doc.add(new Field("email","email",Field.Store.YES,Field.Index.NOT_ANALYZED)); 
  233.             doc.add(new Field("content","content",Field.Store.NO,Field.Index.ANALYZED)); 
  234.             doc.add(new Field("name","names",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 
  235.             writer.updateDocument(new Term("id","3"), doc); 
  236.         } catch (CorruptIndexException e) { 
  237.             // TODO Auto-generated catch block 
  238.             e.printStackTrace(); 
  239.         } catch (LockObtainFailedException e) { 
  240.             // TODO Auto-generated catch block 
  241.             e.printStackTrace(); 
  242.         } catch (IOException e) { 
  243.             // TODO Auto-generated catch block 
  244.             e.printStackTrace(); 
  245.         }finally
  246.             if(writer != null ){ 
  247.                 try { 
  248.                     writer.close(); 
  249.                 } catch (CorruptIndexException e) { 
  250.                     // TODO Auto-generated catch block 
  251.                     e.printStackTrace(); 
  252.                 } catch (IOException e) { 
  253.                     // TODO Auto-generated catch block 
  254.                     e.printStackTrace(); 
  255.                 } 
  256.             } 
  257.         } 
  258.          
  259.     } 
  260. 測試類TestCase.java 
  261. import static org.junit.Assert.*; 
  262.  
  263. import org.junit.BeforeClass; 
  264. import org.junit.Test; 
  265.  
  266.  
  267. public class TestCase { 
  268.  
  269.     @BeforeClass 
  270.     public static void setUpBeforeClass() throws Exception { 
  271.     } 
  272.  
  273.     @Test 
  274.     public void testBuildIndex() { 
  275.         new IndexUtil().buildIndex(); 
  276.     } 
  277.     @Test 
  278.     public void testQuery(){ 
  279.         new IndexUtil().query(); 
  280.     } 
  281.  
  282.     @Test 
  283.     public void testDeleteIndex(){ 
  284.         new IndexUtil().deleteIndex(); 
  285.     } 
  286.      
  287.     @Test 
  288.     public void testUnDeleteIndex(){ 
  289.         new IndexUtil().unDeleteIndex(); 
  290.     } 
  291.      
  292.     @Test 
  293.     public void testForceDelete(){ 
  294.         new IndexUtil().forceDelete(); 
  295.     } 
  296.      
  297.     @Test 
  298.     public void testMerge(){ 
  299.         new IndexUtil().merge(); 
  300.     } 
  301.      
  302.     @Test 
  303.     public void testUpdate(){ 
  304.         new IndexUtil().update(); 
  305.     } 

 

本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1120058

發佈了115 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章