Lucene 검색(애플릿)(Lucene3.5)

4161 단어
/*오늘 검색한 애플릿을 봤는데 요약은 다음과 같다.
Lucene에서 검색을 수행하려면 다음과 같이 하십시오.
1...Derectory 만들기
2...IndexReader 작성
3...IndexReader를 기반으로 IndexSearcher 작성
4...검색을 위한 Query 만들기
5...검색어에서 TopDocs 객체 검색 및 반환
6...TopDocs에서 ScoreDoc 객체 가져오기
7....검색어와 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();
		
	}

}

 
 
 
 
*/
//부족해 내가 너무 느리게 보는 것 같아 꽉 잡아

좋은 웹페이지 즐겨찾기