Lucene 搜索(小程序)(Lucene3.5)

/*今天看了个搜索的小程序 总结如下

Lucene 执行搜索的步骤:

1...创建Derectory

2...创建IndexReader

3...根据IndexReader创建IndexSearcher

4...创建搜索的Query(查询字符串)

5...根据searcher搜索并且返回TopDocs对象

6...根据TopDocs获取ScoreDoc对象

7....根据searcher和ScoreDoc对象获取具体的Document对象

8...根据Document对象获取需要的值

9...关闭reader

*/

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.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.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;


public class HelloLucene {

	/**
	 * @param args
	 */
	
	
	public static void index()//throws IOException
	{

		
		IndexWriter writer=null;
			
		try {
			//		Directory directory=new RAMDirectory() ;//创建在内存中
			//1.创建Directory
			Directory directory=FSDirectory.open(new File("g:/lucene/first1") );//创建在硬盘中
			//2.创建IndexWriter
		    IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
			writer=new  IndexWriter(directory,iwc);
			//3.创建Document对象
			Document doc=null;
			//4.为Document添加Field
		File f=new File("g:/lucene/testFiles");
			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));
				//通过IndexWriter添加文档到索引中
				writer.addDocument(doc);
				System.out.println(file.getName()+" has Indexed!");
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		finally
		{

			try {
				writer.close();//关闭indextwriter
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public static void searcher()
	{
		
	
		
		try {
			//1.
			Directory directory = FSDirectory.open(new File("g:/lucene/first1") );
			//2.
			IndexReader reader=IndexReader.open(directory);
		    //3.
			IndexSearcher searcher=new IndexSearcher(reader);
			//4.
			QueryParser parser=new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));
			Query query=parser.parse("java");
			//5.
			TopDocs tds=searcher.search(query, 20);
			//6.
			ScoreDoc [] sds=tds.scoreDocs;
			for (ScoreDoc sd:sds)
			{
				//7.
				Document d=searcher.doc(sd.doc);
				//8.
				System.out.println(d.get("filename")+"["+d.get("filepath")+"]");
				
				
			}
			//9.
			reader.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		index();
		searcher();
		
	}

}


 

 

 

 

*/

//不足 感觉自己看的太慢 得抓紧了

你可能感兴趣的:(Lucene 搜索(小程序)(Lucene3.5))