Lucene小练6(复习searcher)

//主类
package org.se.lucene;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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.QueryParser;
import org.apache.lucene.search.IndexSearcher;
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 org.apache.lucene.search.Query;
public class hellowLucene {


	public void index()
	{
		IndexWriter writer=null;
		try {
			//1.创建directory
			Directory directory=FSDirectory.open(new File("f:/lucene/Index03"));
			//2.创建IndexWriter
			 writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,
					new StandardAnalyzer(Version.LUCENE_35)));
			//3.创建Document对象
			Document document=null;
			//4.为Document添加Field
			File f=new File("f:/lucene/lucenes");
			
			   for(File file:f.listFiles())
			   {
				   document=new Document();
				   
				   document.add(new Field("content",new FileReader(file)));
				   document.add(new Field("filename",file.getName(),Field.Store.YES,
						   Field.Index.NOT_ANALYZED));
				   document.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,
						   Field.Index.NOT_ANALYZED));
				   //5.通过IndexWriter添加文档到索引中
				   writer.addDocument(document);
			   }
		
			 
		}catch (CorruptIndexException e) {
			e.printStackTrace();
			// TODO: handle exception
		}catch (LockObtainFailedException e) {
			e.printStackTrace();
			// TODO: handle exception
		}catch (IOException e) {
			e.printStackTrace();
			// TODO: handle exception
		}finally{
			try {
				if(writer!=null)
					writer.close();
				 
			    }catch (CorruptIndexException e2) {
					e2.printStackTrace();
					// TODO: handle exception
				}catch (IOException e2) {
					e2.printStackTrace();
					// TODO: handle exception
				}
		}
	}
	public void search()
	{
		try {
			//1.创建Directroy
			Directory directory = FSDirectory
					.open(new File("f:/lucene/Index03"));
			//2.创建IndexReader
			IndexReader reader = IndexReader.open(directory);
			//3.根据IndexReader创建IndexSearcher
			IndexSearcher searcher = new IndexSearcher(reader);
			//4.创建搜索的Query
			QueryParser parser = new QueryParser(Version.LUCENE_35, "content",
					new StandardAnalyzer(Version.LUCENE_35));
		   Query query=parser.parse("ask");
			//5.根据qurey搜索并返回TopDocs
			TopDocs tds = searcher.search(query, 10);
			//6.根据TopDocs获取Scoredoc对象
			ScoreDoc[] sDoc = tds.scoreDocs;
			for (ScoreDoc sds : sDoc) {
				//7.根据searcher和scoredoc对象获取具体Document
				Document document = searcher.doc(sds.doc);
				//8.根据Document获取具体的值
				System.out.println(document.get("filename") + "["
						+ document.get("path") + "]");
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
//测试类
package org.se.lucene;

import org.junit.Test;

public class LuceneTest {
	@Test
	public void test_Index()
	{
		hellowLucene hLucene=new hellowLucene();
		hLucene.index();
	}
	@Test
	public void test_Search()
	{
		hellowLucene hLucene=new hellowLucene();
		hLucene.search();
	}

}
Lucene小练6(复习searcher)

你可能感兴趣的:(Lucene)