LuceneIndexDeleteOptimize.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:1k
源码类别:

搜索引擎

开发平台:

Java

  1. package chapter5;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.lucene.index.Term;
  5. import org.apache.lucene.index.IndexReader;
  6. import org.apache.lucene.index.IndexWriter;
  7. import org.apache.lucene.analysis.Analyzer;
  8. import org.apache.lucene.analysis.SimpleAnalyzer;
  9. public class LuceneIndexDeleteOptimize {
  10. public static void main(String[] args) {
  11. try {
  12. File indexDir = new File("D:\workshop\TextIndex"); 
  13. IndexReader reader = IndexReader.open(indexDir);            
  14. System.out.print("Next  Document ID  before optimize: ");
  15. System.out.println(reader.maxDoc());
  16. System.out.print("Total Document Num before optimize: ");
  17. System.out.println(reader.numDocs());
  18. reader.close();
  19. IndexWriter idxwriter = new IndexWriter("D:\workshop\TextIndex",new SimpleAnalyzer(),false);
  20. Term term = new Term("id","002");
  21. idxwriter.deleteDocuments(term);
  22. idxwriter.optimize();
  23. idxwriter.close();
  24. reader = IndexReader.open(indexDir);            
  25. System.out.print("Next  Document ID  after optimize: ");
  26. System.out.println(reader.maxDoc());
  27. System.out.print("Total Document Num after optimize: ");
  28. System.out.println(reader.numDocs());
  29. reader.close();
  30. }catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }