lucene索引删除,恢复,更新

  1. package org.se.lucene;  
  2.   
  3. //主类  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  7. import org.apache.lucene.document.Document;  
  8. import org.apache.lucene.document.Field;  
  9. import org.apache.lucene.index.CorruptIndexException;  
  10. import org.apache.lucene.index.IndexReader;  
  11. import org.apache.lucene.index.IndexWriter;  
  12. import org.apache.lucene.index.IndexWriterConfig;  
  13. import org.apache.lucene.index.Term;  
  14. import org.apache.lucene.store.Directory;  
  15. import org.apache.lucene.store.FSDirectory;  
  16. import org.apache.lucene.store.LockObtainFailedException;  
  17. import org.apache.lucene.util.Version;  
  18.   
  19. public class lucene_index {  
  20.         private String[] ids={"1","2","3","4","5","6"};  
  21.         private String[] emails={"aa@aa.org","cc@cc.org","dd@@dd.org","bb@bb.org","ee@ee.org","ff@ff.org"};  
  22.         private String[] contents={"welcometotyu","hellowboy","higirl","howareyou","googluck","badgosh"};  
  23.         private int[] attachs={1,2,3,4,5,6};  
  24.         private String[] names={"liwu","zhangsan","xiaoqinag","laona","dabao","lisi"};  
  25.         private Directory directory=null;  
  26.           
  27.         public lucene_index()  
  28.         {  
  29.             try {  
  30.                 directory=FSDirectory.open(new File("f:/lucene/index02"));  
  31.             } catch (IOException e) {  
  32.                 // TODO Auto-generated catch block  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.         public void quary()  
  37.         {  
  38.             try {  
  39.                 IndexReader reader=IndexReader.open(directory);  
  40.                 System.out.println("numdocs"+reader.numDocs());  
  41.                 System.out.println("maxDocs"+reader.maxDoc());  
  42.                 System.out.println("detelemaxDocs"+reader.numDeletedDocs());  
  43.                 reader.close();  
  44.             } catch (CorruptIndexException e) {  
  45.                 // TODO Auto-generated catch block  
  46.                 e.printStackTrace();  
  47.             } catch (IOException e) {  
  48.                 // TODO Auto-generated catch block   
  49.                 e.printStackTrace();  
  50.             }  
  51.               
  52.         }  
  53.           
  54.         @SuppressWarnings("deprecation")  
  55.         public void undelete()  
  56.         {  
  57.             try {  
  58.                 //回复时必须把reader的只读设为false  
  59.                 IndexReader reader=IndexReader.open(directory,false);  
  60.                 reader.undeleteAll();  
  61.                 reader.close();  
  62.             } catch (CorruptIndexException e) {  
  63.                 // TODO Auto-generated catch block  
  64.                 e.printStackTrace();  
  65.             } catch (IOException e) {  
  66.                 // TODO Auto-generated catch block  
  67.                 e.printStackTrace();  
  68.             }  
  69.               
  70.         }  
  71.       
  72.         //清空回收站,强制优化  
  73.         public void forceDelete()  
  74.         {  
  75.             IndexWriter writer=null;  
  76.             try {  
  77.                 writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,  
  78.                         new StandardAnalyzer(Version.LUCENE_36)));  
  79.                 //参数十一个选项,可以是一个query,也可以是一个term  term就是一个精确查找的值  
  80.                 //此时删除的文档并未完全删除,而是存储在回收站中,可以恢复的  
  81.                 writer.forceMergeDeletes();  
  82.             } catch (CorruptIndexException e) {  
  83.                 e.printStackTrace();  
  84.             } catch (LockObtainFailedException e) {  
  85.                 e.printStackTrace();  
  86.             } catch (IOException e) {  
  87.                 e.printStackTrace();  
  88.             }  
  89.             finally{  
  90.                 if (writer!=null) {  
  91.                     try {  
  92.                         writer.close();  
  93.                     } catch (CorruptIndexException e) {  
  94.                         // TODO Auto-generated catch block  
  95.                         e.printStackTrace();  
  96.                     } catch (IOException e) {  
  97.                         // TODO Auto-generated catch block  
  98.                         e.printStackTrace();  
  99.                     }  
  100.                 }  
  101.             }  
  102.         }  
  103.           
  104.         public void merge()  
  105.         {  
  106.             IndexWriter writer=null;  
  107.             try {  
  108.                 writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,  
  109.                         new StandardAnalyzer(Version.LUCENE_36)));  
  110.                   
  111.                 writer.forceMerge(2);  
  112.             } catch (CorruptIndexException e) {  
  113.                 e.printStackTrace();  
  114.             } catch (LockObtainFailedException e) {  
  115.                 e.printStackTrace();  
  116.             } catch (IOException e) {  
  117.                 e.printStackTrace();  
  118.             }  
  119.             finally{  
  120.                 if (writer!=null) {  
  121.                     try {  
  122.                         writer.close();  
  123.                     } catch (CorruptIndexException e) {  
  124.                         // TODO Auto-generated catch block  
  125.                         e.printStackTrace();  
  126.                     } catch (IOException e) {  
  127.                         // TODO Auto-generated catch block  
  128.                         e.printStackTrace();  
  129.                     }  
  130.                 }  
  131.             }  
  132.         }  
  133.           
  134.         public void delete()  
  135.         {  
  136.             IndexWriter writer=null;  
  137.             try {  
  138.                 writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,  
  139.                         new StandardAnalyzer(Version.LUCENE_36)));  
  140.                 //参数十一个选项,可以是一个query,也可以是一个term  term就是一个精确查找的值  
  141.                 //此时删除的文档并未完全删除,而是存储在回收站中,可以恢复的  
  142.                 writer.deleteDocuments(new Term("id","1"));  
  143.             } catch (CorruptIndexException e) {  
  144.                 e.printStackTrace();  
  145.             } catch (LockObtainFailedException e) {  
  146.                 e.printStackTrace();  
  147.             } catch (IOException e) {  
  148.                 e.printStackTrace();  
  149.             }  
  150.             finally{  
  151.                 if (writer!=null) {  
  152.                     try {  
  153.                         writer.close();  
  154.                     } catch (CorruptIndexException e) {  
  155.                         // TODO Auto-generated catch block  
  156.                         e.printStackTrace();  
  157.                     } catch (IOException e) {  
  158.                         // TODO Auto-generated catch block  
  159.                         e.printStackTrace();  
  160.                     }  
  161.                 }  
  162.             }  
  163.         }  
  164.         public void index()  
  165.         {  
  166.                IndexWriter writer=null;  
  167.                Document doc=null;  
  168.                try {  
  169.                 writer =new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_36,   
  170.                            new StandardAnalyzer(Version.LUCENE_36)));  
  171.                 //writer.deleteAll();  
  172.                 for(int i=0;i<ids.length;i++)  
  173.                 {  
  174.                     doc=new Document();  
  175.                     doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));  
  176.                     doc.add(new Field("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));  
  177.                     doc.add(new Field("contents",contents[i],Field.Store.YES,Field.Index.ANALYZED));  
  178.                     doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));  
  179.                     writer.addDocument(doc);   
  180.                 }  
  181.             } catch (CorruptIndexException e) {  
  182.                 // TODO Auto-generated catch block  
  183.                 e.printStackTrace();  
  184.             } catch (LockObtainFailedException e) {  
  185.                 // TODO Auto-generated catch block  
  186.                 e.printStackTrace();  
  187.             } catch (IOException e) {  
  188.                 // TODO Auto-generated catch block  
  189.                 e.printStackTrace();  
  190.             }  
  191.                finally{  
  192.                    if(writer!=null)  
  193.                    {  
  194.                       try {  
  195.                         writer.close();  
  196.                     } catch (CorruptIndexException e) {  
  197.                         // TODO Auto-generated catch block  
  198.                         e.printStackTrace();  
  199.                     } catch (IOException e) {  
  200.                         // TODO Auto-generated catch block  
  201.                         e.printStackTrace();  
  202.                     }     
  203.                    }  
  204.                }  
  205.         }  
  206.        //更新索引  
  207.         public void update()  
  208.         {  
  209.             /*lucene本身不支持更新 
  210.              *  
  211.              * 通过删除索引然后再建立索引来更新 
  212.              */  
  213.               IndexWriter writer=null;  
  214.                Document doc=null;  
  215.                try {  
  216.                 writer =new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_36,   
  217.                            new StandardAnalyzer(Version.LUCENE_36)));  
  218.                 writer.deleteAll();  
  219.                 for(int i=0;i<ids.length;i++)  
  220.                 {  
  221.                     doc=new Document();  
  222.                     doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));  
  223.                     doc.add(new Field("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));  
  224.                     doc.add(new Field("contents",contents[i],Field.Store.YES,Field.Index.ANALYZED));  
  225.                     doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));  
  226.                     writer.updateDocument(new Term("id","1"), doc);   
  227.                 }  
  228.             } catch (CorruptIndexException e) {  
  229.                 // TODO Auto-generated catch block  
  230.                 e.printStackTrace();  
  231.             } catch (LockObtainFailedException e) {  
  232.                 // TODO Auto-generated catch block  
  233.                 e.printStackTrace();  
  234.             } catch (IOException e) {  
  235.                 // TODO Auto-generated catch block  
  236.                 e.printStackTrace();  
  237.             }  
  238.                finally{  
  239.                    if(writer!=null)  
  240.                    {  
  241.                       try {  
  242.                         writer.close();  
  243.                     } catch (CorruptIndexException e) {  
  244.                         // TODO Auto-generated catch block  
  245.                         e.printStackTrace();  
  246.                     } catch (IOException e) {  
  247.                         // TODO Auto-generated catch block  
  248.                         e.printStackTrace();  
  249.                     }     
  250.                    }  
  251.                }      
  252.         }  
  253. }  
  254.   
  255.   
  256. //测试类  
  257. package org.se.lucene;  
  258.   
  259. import org.junit.Test;  
  260.   
  261. public class test {  
  262.   
  263.     @Test  
  264.     public void testIndex()  
  265.     {  
  266.         lucene_index l_index=new lucene_index();  
  267.         l_index.index();  
  268.     }  
  269.     @Test  
  270.       
  271.     public void testquary()  
  272.     {  
  273.         lucene_index l_index=new lucene_index();  
  274.         l_index.quary();  
  275.     }  
  276.     @Test  
  277.     public void testDelete()  
  278.     {  
  279.         lucene_index l_index=new lucene_index();  
  280.         l_index.delete();  
  281.     }  
  282.     @Test  
  283.     public void testunDelete()  
  284.     {  
  285.         lucene_index l_index=new lucene_index();  
  286.         l_index.undelete();  
  287.     }  
  288.     @Test  
  289.     public void testForceDelete()  
  290.     {  
  291.         lucene_index l_index=new lucene_index();  
  292.         l_index.forceDelete();  
  293.     }  
  294.     @Test  
  295.     public void testmerge()  
  296.     {  
  297.         lucene_index l_index=new lucene_index();  
  298.         l_index.merge();  
  299.     }  
  300.       
  301.     @Test  
  302.     public void upDate()  
  303.     {  
  304.         lucene_index l_index=new lucene_index();  
  305.         l_index.update();  
  306.     }  
  307.       
  308. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值