用Java lucene 寫的一個搜索引擎 .

package org.itat.test;  
  
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.LockObtainFailedException;  
//import org.apache.lucene.store.RAMDirectory;   
import org.apache.lucene.util.Version;  
  
/** 
*lucene3.5中主要包含以下三部分: 
*一、建立索引 
*二、分詞部分 
*三、搜索部分 
**/  
public class hellolucene {  
    public hellolucene(StringBuffer sb){  
        //index();   
        searcher(sb);  
    }  
    /** 
     * 建立索引 
     */  
    public void index(){  
        IndexWriter writer=null;  
        try{  
            //1、創建Directory   
            //Directory directory=new RAMDirectory();將索引建立在內存中   
            Directory directory=FSDirectory.open(new File("D:\\lucene\\aaa"));//將索引建立在硬盤中   
            //2、創建IndexWriter writer   
            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("d:/kankan");//指定搜索路徑   
            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("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));  
                //5、通過IndexWriter添加文檔到索引中   
                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();  
            }  
        }  
    }  
    /** 
     * 搜索 
     * @throws ParseException  
     */  
    public StringBuffer searcher(StringBuffer sb){  
        try{  
        //1.創建Directory   
        Directory directory=FSDirectory.open(new File("d:/lucene/aaa"));  
        //2.創建IndexReader   
        IndexReader reader = IndexReader.open(directory);  
        //3.根據IndexReader創建IndexSearcher   
        IndexSearcher searcher = new IndexSearcher(reader);  
        //4.創建搜索的Query   
        //創建parser來確定搜索的內容   
        QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));  
        Query query = parser.parse("一羣大雁");  
        //5.根據search搜索並且返回TopDocs   
        TopDocs tds = searcher.search(query, 10);  
        //6.根據TopDocs獲取ScoreDoc對象   
        ScoreDoc[] sds=tds.scoreDocs;  
        for(ScoreDoc sd:sds){  
        //7.根據search和ScordDoc對象獲取具體的Document對象   
            Document d=searcher.doc(sd.doc);  
        //8.根據Document對象獲取需要的值     
            System.out.println(d.get("filename")+"["+d.get("path")+"]");  
            sb.append(d.get("filename")+"["+d.get("path")+"]");  
            return sb;  
        }//關閉reader   
           reader.close();  
        } catch(CorruptIndexException e){  
            e.printStackTrace();  
        } catch(IOException e){  
            e.printStackTrace();  
        } catch (ParseException e) {  
            // TODO Auto-generated catch block   
            e.printStackTrace();  
        }  
        return sb;  
    }  
    public static void main (String args[])  
    {  
        StringBuffer sb = new StringBuffer();  
        new hellolucene(sb);  
          
    }  
}


 

 

 

 

http://blog.csdn.net/karldoenitz/article/details/7913505

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章