Practice Every Day_7(Lucene3.5小练习)

   今天看了看lucene3.5的视频,敲了老师讲的例子,建立索引和搜索,建立索引没问题了,可不知道为啥就搜不出东西来呢?

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import java.io.*;

 

 

public class HellowLucene {

 public static void Index()
 {  
  IndexWriter writer=null;
   
     try
     {
      IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
      Directory directory=FSDirectory.open(new File("f:/lucene/index_01") );
     writer=new  IndexWriter(directory,iwc);
     Document doc=null;
     File f=new File("f:/lucene/First");
      for (File file:f.listFiles())
      {
       doc=new Document();
       doc.add(new Field("content",new FileReader(file)));
       doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
       doc.add(new Field("filepath",file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
       writer.addDocument(doc);
      
      
      }
     }
     catch (CorruptIndexException e)
     {
      e.printStackTrace();
     }
     catch (LockObtainFailedException e)
     {
      e.printStackTrace();
     }
     catch (IOException e)
     {
      e.printStackTrace();
     }
     finally
     {
     try{
      if (writer!=null)     
         writer.close();
        }
     catch (CorruptIndexException e)
     {
      e.printStackTrace();
     }
     catch (IOException e)
     {
      e.printStackTrace();
     }
   }
 }
 public static void searcher()
 {
  try
  {
   Directory directory=FSDirectory.open(new File("f:/lucene/index_01") ); 
   IndexReader reader= IndexReader.open(directory);
   IndexSearcher searcher=new IndexSearcher(reader);
   QueryParser parser=new QueryParser(Version.LUCENE_35,"filename",new StandardAnalyzer(Version.LUCENE_35));
   Query query=parser.parse("java");
   TopDocs tds=searcher.search(query, 4);
   ScoreDoc[]sds=tds.scoreDocs;
   for(ScoreDoc sd:sds)
   {
    Document d=searcher.doc(sd.doc);
    System.out.println(d.get("filename"+"["+d.get("path")+"]"));
    
   }    
   reader.close();
  }  
      catch (CorruptIndexException e)
      {
       e.printStackTrace();
      }
      catch (IOException e)
      {
       e.printStackTrace();
      }
     catch (ParseException e)
     {
    e.printStackTrace();
   }
  
 }  
    public static void main(String[] args)
    {

         Index();
      searcher();
    }       
}

 

总结一下建立索引和搜素的步骤,

建立索引分为五步:

1、创建Directory;

2、创建IndexWriter;

3、创建Document;

4、为Document添加Field;

5、通过IndexWriter添加文档到索引中。

搜素分为九步:

1、创建Directory;

2、创建IndexReader;

3、根据IndexReader创建IndexSearcher;

4、创建搜素Query;

5、根据searcher搜索并返回TopDocs;

6、根据TopDocs获取ScoreDoc对象;

7、根据Searcher和ScoreDoc获取具体的Document对象;

8、根据Document对象获取所需的值;

9、关闭Reader;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值