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

搜索引擎

开发平台:

Java

  1. package chapter5;
  2. import java.util.Date;
  3. import java.io.IOException;
  4. import org.apache.lucene.document.Field;
  5. import org.apache.lucene.document.Document;
  6. import org.apache.lucene.index.IndexWriter;
  7. import org.apache.lucene.analysis.Analyzer;
  8. import org.apache.lucene.analysis.SimpleAnalyzer;
  9. public class LuceneIndexText {
  10. private static String Dest_Index_Path = "D:\workshop\TextIndex";
  11. static protected String[] keywords = {"001","002","003"};
  12. static protected String[] textdetail = {"记录一","记录二", "记录三"} ;
  13. public static void main(String[] args) {
  14. try {
  15. Date start = new Date();
  16. Analyzer TextAnalyzer = new SimpleAnalyzer();
  17. IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
  18.         TextIndex.setUseCompoundFile(true);
  19. for(int i = 0; i < 3 ; i++){
  20. Document document = new Document();
  21. Field field_id = new Field("id", keywords[i], 
  22. Field.Store.YES,Field.Index.UN_TOKENIZED);
  23. document.add(field_id);
  24. Field field_content = new Field("content", textdetail[i], 
  25. Field.Store.YES,Field.Index.TOKENIZED);
  26. document.add(field_content);
  27. TextIndex.addDocument(document);
  28. }
  29. TextIndex.optimize();
  30. TextIndex.close();
  31. Date end = new Date();
  32. long tm_index = end.getTime() - start.getTime(); 
  33. System.out.println("Total Time:(ms)");
  34. System.out.println(tm_index);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. System.out.println("Index success");
  39. }
  40. }