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

搜索引擎

开发平台:

Java

  1. package chapter5;
  2. import java.util.Date;
  3. import java.io.*;
  4. import org.apache.lucene.search.Hits;
  5. import org.apache.lucene.search.IndexSearcher;
  6. import org.apache.lucene.search.Query;
  7. import org.apache.lucene.search.TermQuery;
  8. import org.apache.lucene.store.Directory; 
  9. import org.apache.lucene.store.FSDirectory;
  10. import org.apache.lucene.document.Field;
  11. import org.apache.lucene.document.Document;
  12. import org.apache.lucene.index.IndexWriter;
  13. import org.apache.lucene.index.IndexReader;
  14. import org.apache.lucene.index.Term;
  15. import org.apache.lucene.analysis.Analyzer;
  16. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  17. import org.apache.lucene.analysis.SimpleAnalyzer;
  18. public class LuceneIndexUpdate {
  19. private static String Dest_Index_Path = "D:\workshop\TextIndex1";
  20. private static String Text_File_Path = "D:\workshop\ch2\indexsample.txt";
  21. private static String Text_update_Path = "D:\workshop\ch2\indexsample2.txt";
  22. public static void main(String[] args) {
  23. try {
  24. File file = new File(Text_File_Path);  // 原始文件
  25.         Directory dir = FSDirectory.getDirectory(Dest_Index_Path,false); // 索引目录
  26.         Analyzer TextAnalyzer = new SimpleAnalyzer();                    // 文档分析器
  27.         IndexWriter TextIndex = new IndexWriter(dir,TextAnalyzer,true);  // 生成索引器对象
  28.         TextIndex.setUseCompoundFile(true);
  29. Document document = new Document();                              // 新建空文档
  30. Field field_name = new Field("path", file.getName(), 
  31. Field.Store.YES,Field.Index.UN_TOKENIZED);
  32. document.add(field_name);                                        // 添加文件名域
  33. FileInputStream inputfile=new FileInputStream(file);             // 文件输入流
  34. int len=inputfile.available();
  35. byte[] buffer = new byte[len]; 
  36. inputfile.read(buffer);                                          // 读取文件内容
  37. inputfile.close();
  38. String contentext = new String(buffer);
  39. Field field_content = new Field( "content", contentext,
  40.                          Field.Store.YES,Field.Index.TOKENIZED );
  41. document.add(field_content);                                    // 添加文件内容域
  42. TextIndex.addDocument(document);                                // 添加索引文档
  43. TextIndex.optimize();
  44. TextIndex.close();
  45. display(file.getName());                                        // 输出替换前结果
  46.         IndexWriter TextIndex2 = new IndexWriter(dir,TextAnalyzer,true); // 重建索引器
  47.         TextIndex2.setUseCompoundFile(true); 
  48. File file2 = new File(Text_update_Path);
  49. Document document2 = new Document();                             // 生成空文档对象
  50. Field field_name2 = new Field("path", file2.getName(),           // 生成文件名域 
  51. Field.Store.YES,Field.Index.UN_TOKENIZED);
  52. document2.add(field_name2);
  53. FileInputStream inputfile2=new FileInputStream(file2);           // 读取文件内容
  54. int len2=inputfile2.available();
  55. byte[] buffer2 = new byte[len2]; 
  56. inputfile2.read(buffer2);
  57. inputfile2.close();
  58. String contentext2 = new String(buffer2);
  59. Field field_content2 = new Field( "content", contentext2,
  60.                          Field.Store.YES,Field.Index.TOKENIZED );
  61. document2.add(field_content2);                                  // 添加文件内容域
  62. //Term term = new Term("path", file.getName() );                  // 新建语汇单元
  63. Term term = new Term("content", "Lucene" );                  // 新建语汇单元
  64. TextIndex2.updateDocument(term, document2);                     // 替换原有匹配文档
  65. TextIndex2.optimize();
  66. TextIndex2.close();
  67. display(file2.getName());                                       // 输出替换后结果
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. public static void display(String words) throws IOException
  73. { // 显示结果
  74. try {
  75. IndexSearcher searcher = new IndexSearcher( Dest_Index_Path ); // 检索器
  76. Term term = new Term("path", words );                          // 单词项
  77. Query query = new TermQuery(term);                             // 检索单元 
  78. System.out.println("Query  words:");
  79. System.out.println("  " + query.toString());
  80. Hits hits = searcher.search(query);                            // 提交检索
  81. System.out.println("Search result:");
  82. for(int i=0; i < hits.length(); i++)                           // 输出结果
  83. {
  84. if( hits.doc(i).getField("content")!= null)
  85.  System.out.println("  Content: " + hits.doc(i).getField("content").stringValue());
  86. System.out.println("   Path : " + hits.doc(i).getField("path").stringValue());
  87. }
  88. } catch (IOException e)
  89. {
  90. e.printStackTrace();
  91. }
  92. }
  93. }