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

搜索引擎

开发平台:

Java

  1. package chapter5;
  2. import java.util.Date;
  3. import java.io.*;
  4. import org.apache.lucene.queryParser.QueryParser;
  5. import org.apache.lucene.search.Hits;
  6. import org.apache.lucene.search.IndexSearcher;
  7. import org.apache.lucene.search.Query;
  8. import org.apache.lucene.search.TermQuery;
  9. import org.apache.lucene.store.Directory; 
  10. import org.apache.lucene.store.FSDirectory;
  11. import org.apache.lucene.document.Field;
  12. import org.apache.lucene.document.Document;
  13. import org.apache.lucene.index.IndexWriter;
  14. import org.apache.lucene.index.IndexReader;
  15. import org.apache.lucene.index.Term;
  16. import org.apache.lucene.analysis.Analyzer;
  17. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  18. import org.apache.lucene.analysis.SimpleAnalyzer;
  19. import org.apache.lucene.queryParser.*;
  20. public class LuceneIndexInteger {
  21. private static String Dest_Index_Path = "D:\workshop\TextIndex1";
  22. private static String Text_File_Path = "D:\workshop\ch2\indexsample.txt";
  23. public static void main(String[] args) {
  24. try {
  25. File file = new File(Text_File_Path);  // 原始文件
  26.         Directory dir = FSDirectory.getDirectory(Dest_Index_Path,false); // 索引目录
  27.         Analyzer TextAnalyzer = new SimpleAnalyzer();                    // 文档分析器
  28.         IndexWriter TextIndex = new IndexWriter(dir,TextAnalyzer,true);  // 生成索引器对象
  29.         TextIndex.setUseCompoundFile(true);
  30. Document document = new Document();                              // 新建空文档
  31. Field field_name = new Field("path1", file.getName(), 
  32. Field.Store.YES,Field.Index.UN_TOKENIZED);
  33. document.add(field_name);                                        // 添加整体文件名域
  34. Field field_name2 = new Field("path2", file.getName(), 
  35. Field.Store.YES,Field.Index.TOKENIZED);
  36. document.add(field_name2);                                      // 添加整体文件名域
  37. TextIndex.addDocument(document);                                // 添加索引文档
  38. TextIndex.optimize();
  39. TextIndex.close();
  40. System.out.println("----------no split-----------------");
  41. display(file.getName(),true);                                   // 输出替换前结果
  42. System.out.println("----------  split  ----------------");
  43. display(file.getName(),false);                                   // 输出替换前结果
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. public static void display(String words,boolean bsplit) throws IOException
  49. { // 显示结果
  50. Query query ,query1;
  51. try {
  52. IndexSearcher searcher = new IndexSearcher( Dest_Index_Path ); // 检索器
  53. if(bsplit)
  54. {
  55. Term term = new Term("path2", words);                      // 单词项
  56. query = new TermQuery(term);                               // 检索单元 
  57. Term term1 = new Term("path1", words);                      // 单词项
  58. query1 = new TermQuery(term1);                              // 检索单元 
  59. } else {
  60.     QueryParser parser = new QueryParser("path2",new StandardAnalyzer()); 
  61.     query = parser.parse(words);
  62.     QueryParser parser1 = new QueryParser("path1",new StandardAnalyzer()); 
  63.     query1 = parser1.parse(words);
  64. }
  65. System.out.println("Query  words:");
  66. System.out.println("  " + query.toString());
  67. Hits hits = searcher.search(query );                           // 提交检索
  68. System.out.println("Search result:");
  69. for(int i=0; i < hits.length(); i++)                           // 输出结果
  70. {
  71. System.out.println("  path2: " + hits.doc(i).getField("path2").stringValue());
  72. System.out.println("   Path1 : " + hits.doc(i).getField("path1").stringValue());
  73. }
  74. System.out.println("Query  words:");
  75. System.out.println("  " + query1.toString());
  76. hits = searcher.search(query1 );                               // 提交检索
  77. System.out.println("Search result:");
  78. for(int i=0; i < hits.length(); i++)                           // 输出结果
  79. {
  80. System.out.println("  path2: " + hits.doc(i).getField("path2").stringValue());
  81. System.out.println("   Path1 : " + hits.doc(i).getField("path1").stringValue());
  82. }
  83. } catch (ParseException e)
  84. {
  85. e.printStackTrace();
  86. } catch (IOException e)
  87. {
  88. e.printStackTrace();
  89. }
  90. }
  91. }