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;

你可能感兴趣的:(Practice Every Day_7(Lucene3.5小练习))