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

搜索引擎

开发平台:

Java

  1. package chapter5;
  2. import java.io.IOException;
  3. import org.apache.lucene.document.Field;
  4. import org.apache.lucene.document.Document;
  5. import org.apache.lucene.index.IndexWriter;
  6. import org.apache.lucene.analysis.Analyzer;
  7. import org.apache.lucene.analysis.SimpleAnalyzer;
  8. public class LuceneIndexParam {
  9. private static String Dest_Index_Path = "D:\workshop\TextIndex";
  10. static protected String[] keywords = {"001","002","003"};
  11. static protected String[] textdetail = {  "记录一 子项一 子项二 子项三"
  12.                                     , "记录二 子项一 子项二 子项三"
  13.                                     , "记录三 子项一 子项二 子项三"} ;
  14. public static void main(String[] args) {
  15. try {
  16. Analyzer TextAnalyzer = new SimpleAnalyzer();
  17. IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
  18.         TextIndex.setUseCompoundFile(true);
  19.         
  20. System.out.print("Default MergeFactor: ");
  21. System.out.println(TextIndex.getMergeFactor());
  22. System.out.print("Default MaxBufferedDocs: ");
  23. System.out.println(TextIndex.getMaxBufferedDocs());
  24. System.out.print("Default TermIndexInterval: ");
  25. System.out.println(TextIndex.getTermIndexInterval());
  26. System.out.print("Default MaxBufferedDeleteTerms: ");
  27. System.out.println(TextIndex.getMaxBufferedDeleteTerms());
  28.         TextIndex.setMergeFactor(20);
  29.         TextIndex.setMaxBufferedDocs(10);
  30.         TextIndex.setMaxBufferedDeleteTerms(10);
  31.         TextIndex.setTermIndexInterval(10);
  32.         
  33. for(int i = 0; i < 3 ; i++){
  34. TextIndex.setMaxFieldLength(i);
  35. Document document = new Document();
  36. Field field_id = new Field("id", keywords[i], 
  37. Field.Store.YES,Field.Index.UN_TOKENIZED);
  38. document.add(field_id);
  39. Field field_content = new Field("content", textdetail[i], 
  40. Field.Store.YES,Field.Index.TOKENIZED);
  41. document.add(field_content);
  42. TextIndex.addDocument(document);
  43. }
  44. System.out.println("Change Param... ");
  45. System.out.print("Current MergeFactor: ");
  46. System.out.println(TextIndex.getMergeFactor());
  47. System.out.print("Current MaxBufferedDocs: ");
  48. System.out.println(TextIndex.getMaxBufferedDocs());
  49. System.out.print("Current TermIndexInterval: ");
  50. System.out.println(TextIndex.getTermIndexInterval());
  51. System.out.print("Current MaxBufferedDeleteTerms: ");
  52. System.out.println(TextIndex.getMaxBufferedDeleteTerms());
  53. TextIndex.optimize();
  54. TextIndex.close();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }