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;

public class HelloLucene
{
    /**
     * 建立索引
     * @throws IOException 
     * @throws CorruptIndexException 
     */
    public void index() throws CorruptIndexException, IOException
    {
        //1. 创建Directory
        Directory directory = new RAMDirectory();//建立在内存中的
        //Directory directory = FSDirectory.open(new File("d:/lucene1/index01"));
        
        //2. 创建indexWriter
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
        IndexWriter writer = null;
        
        try
        {
            writer = new IndexWriter(directory, iwc);
           //3. 创建document对象
            Document doc = null;
            
            //4. 为document添加Field
            File f = new File("D:/lucene");
            
            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)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        catch (LockObtainFailedException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        finally
        {
            if(writer != null)
            {
                writer.close();
            }
        }
    }
    
    @SuppressWarnings("resource")
    public void search() throws ParseException
    {
        
        //1. 创建Directory
        Directory directory;
        IndexReader reader;
        try
        {
            directory = FSDirectory.open(new File("d:/lucene1/index01"));
            //2. 创建indexReader
            reader = IndexReader.open(directory);
            //3. 根据indexReader 创建IndexSearcher
            //4. 创建搜索的Query
            QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
            //创建parser来确定要搜索文件的内容,第二个参数 表示搜索的域
            //创建query,表示搜索域为content中包含java的文档
            Query query = parser.parse("com");
            //5. 根据sea人撤人搜索并且返回TopDocs
            TopDocs tds = new IndexSearcher(reader).search(query, 100);
            //6. 根据TopDocs 获取ScoreDOc对象
            ScoreDoc[] sds = tds.scoreDocs;
            for(ScoreDoc sd : sds)
            {
                //7. 根据searcher和scoreDoc对象获取具体的Document对象
                Document d =  new IndexSearcher(reader).doc(sd.doc);
              //8. 根据Document对象获取需要的值
                System.out.println(d.get("filename") + "[" + d.get("path") + "]");
            }
           
            
            
            
            //9. 关闭reader
            reader.close();

        }
        catch (IOException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

测试类:

package org.itat.test;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.junit.Test;

public class TestLucene
{ 
    @Test 
    public void testIndex() throws CorruptIndexException, IOException, ParseException
    {
        
        HelloLucene hl = new HelloLucene();
        hl.index();
    }
    
    @Test 
    public void testSearcher() throws ParseException
    {
        HelloLucene hl = new HelloLucene();
//      hl.index();
      hl.search();
    }
}


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。