基于java的全文检索接口lucene2.4测试一

  1. Indexer.java
  2. package lucene.main.test;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Date;
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.document.Field;
  10. import org.apache.lucene.index.IndexWriter;
  11. import org.apache.lucene.index.IndexWriter.MaxFieldLength;
  12. public class Indexer{
  13.  /*
  14.   * @param args
  15.   * @throws Exception
  16.   * @author rush 
  17.   */
  18.  public static void main(String[] args) throws Exception{
  19.   File indexDir=new File("D://lucene//temp//Text_Index_Path");//存放索引目录 
  20.   File dataDir=new File("D://lucene//temp//TextPath//");//被索引目录
  21.   
  22.   long start=new Date().getTime();
  23.   int numIndexed=index(indexDir,dataDir);
  24.   long end=new Date().getTime();
  25.   
  26.   System.out.println("Indexing "+numIndexed+" file took "+(end-start)+" milliseconds");
  27.   System.out.println(numIndexed);
  28.   
  29.   
  30.  }
  31.  private static int index(File indexDir, File dataDir) throws IOException {
  32.   // TODO Auto-generated method stub
  33.   if(!dataDir.exists()||!dataDir.isDirectory())
  34.   {
  35.    throw new IOException(dataDir+"does not exit or is not a directory");
  36.   }
  37.   
  38.   IndexWriter writer=new IndexWriter(indexDir,new StandardAnalyzer(),true,MaxFieldLength.LIMITED);
  39.   writer.setUseCompoundFile(false);//默认创建复合式索引,false:多文件索引 true 复合式索引
  40.   indexDirectory(writer,dataDir);
  41.   
  42.   int numIndexed=writer.maxDoc();//返回在指定索引中
  43.   writer.optimize();//优化
  44.   writer.close();
  45.   return numIndexed;
  46.  }
  47.  private static void indexDirectory(IndexWriter writer, File dir) throws IOException {
  48.   // TODO Auto-generated method stub
  49.   File[] files=dir.listFiles();//返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件和目录。
  50.   
  51.   for(int i=0;i<files.length;i++)
  52.   {
  53.    File file=files[i];
  54.    if(file.isDirectory())
  55.    {
  56.     indexDirectory(writer,file);
  57.    }else if(file.getName().endsWith(".txt")){
  58.     indexFile(writer,file);
  59.    }
  60.   }
  61.   
  62.  }
  63.  private static void indexFile(IndexWriter writer, File file) throws IOException {
  64.   // TODO Auto-generated method stub
  65.   if(file.isHidden()||!file.canRead()||!file.exists())
  66.   {
  67.    return;
  68.   }
  69.   //getCanonicalPath()返回文件路径名的规范形式
  70.   System.out.println("Indexing "+file.getCanonicalPath());
  71.   
  72.   Document doc=new Document();
  73.   
  74.   Field contents=new Field("contents",new FileReader(file));
  75.   Field filename=new Field("filename",file.getCanonicalPath(),Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.NO);
  76.   doc.add(contents);
  77.   doc.add(filename);
  78.   writer.addDocument(doc);
  79.  }
  80. }
  81. Searcher.java
  82. package lucene.main.test;
  83. import java.io.File;
  84. import java.io.IOException;
  85. import java.util.Date;
  86. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  87. import org.apache.lucene.document.Document;
  88. import org.apache.lucene.queryParser.ParseException;
  89. import org.apache.lucene.queryParser.QueryParser;
  90. import org.apache.lucene.search.IndexSearcher;
  91. import org.apache.lucene.search.Query;
  92. import org.apache.lucene.search.ScoreDoc;
  93. import org.apache.lucene.search.TopDocCollector;
  94. import org.apache.lucene.store.Directory;
  95. import org.apache.lucene.store.FSDirectory;
  96. /*
  97.  * @param args
  98.  * @throws Exception
  99.  * @author rush 
  100.  */
  101. public class Searcher {
  102.   public static void main(String[] args) throws Exception{
  103.    File indexDir=new File("D://lucene//temp//Text_Index_Path");
  104.    String keywords="love";
  105.    
  106.    if(!indexDir.exists()||!indexDir.isDirectory())
  107.    {
  108.     throw new Exception(indexDir+"does not exits or is not a directory.");
  109.    }
  110.    
  111.    search(indexDir,keywords);
  112.   }
  113. private static void search(File indexDir, String keywords) throws IOException, ParseException {
  114.  // TODO Auto-generated method stub
  115.  Directory fsDir=FSDirectory.getDirectory(indexDir);//获取索引目录
  116.  IndexSearcher searcher=new IndexSearcher(fsDir);//创建索引查找器
  117.  //创建解析器(查询字符串的解析),指定域名和分析器
  118.  QueryParser qp=new QueryParser("contents",new StandardAnalyzer());
  119.  //指定查找关键字
  120.  Query query=qp.parse(keywords);
  121.  //指定返回的结果集数
  122.  int hitsPerPage=100;
  123.  TopDocCollector collector=new TopDocCollector(hitsPerPage);
  124.  long start=new Date().getTime();
  125.  //开始查找,传入查找的关键字参数和返回结果数参数
  126.  searcher.search(query,collector);
  127.  ScoreDoc[] hits=collector.topDocs().scoreDocs;
  128.  long end=new Date().getTime();
  129.  System.out.println("Found "+hits.length+" documents in "+(end-start)+" milliseconds that matched query '"+keywords+"':");
  130.  for(int i=0;i<hits.length;i++){
  131.   int docId=hits[i].doc;
  132.   Document doc=searcher.doc(docId);
  133.   System.out.println(docId+":"+doc.get("filename"));
  134.  }
  135. }
  136. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值