天天看点

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 {
    /**
     * 创建索引
     */
    public void index() {
        Directory directory = null;
        IndexWriterConfig iwc = null;
        IndexWriter iw = null;
        try {
            // 1!创建DIRECTORY
            // Directory directory=new RAMDirectory();
            directory = FSDirectory.open(new File(
                    "E:/lucenetext"));
            // 2!创建INDEXWRITER
            iwc = new IndexWriterConfig(Version.LUCENE_35,
                    new StandardAnalyzer(Version.LUCENE_35));
            iw = new IndexWriter(directory, iwc);
            // 3!!创建document对象
            Document doc = null;
            // 4!为document添加field
            File f = new File("E:/Workspaces/STS/Lucene/testfile");
            for (File file : f.listFiles()) {
                doc = new Document();
                Field content = new Field("content", new FileReader(file));
                Field filename = new Field("filename", file.getName(),
                        Field.Store.YES, Field.Index.NOT_ANALYZED);
                Field path = new Field("path", file.getAbsolutePath(),
                        Field.Store.YES, Field.Index.NOT_ANALYZED);
                doc.add(content);
                doc.add(filename);
                doc.add(path);
                // 5!把文档doc添加到索引中
                iw.addDocument(doc);
            }
        } 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 {
                iw.close();
            } catch (CorruptIndexException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
/**
*搜索
*/
    public void search(){
                                           
        try {
            //1 在哪搜:创建索引目录
            Directory dir=FSDirectory.open(new File("E:/lucenetext"));
            //2 搜谁:读索引
            IndexReader ir=IndexReader.open(dir);
            //3 开搜准备:根据indexreader创建indexsearch
            IndexSearcher isearch=new IndexSearcher(ir);
            //4 怎么搜:创建搜索query,根据文件域搜索,限定显示条目
            QueryParser parser=new QueryParser(Version.LUCENE_35, "content",new StandardAnalyzer(Version.LUCENE_35));
            //表示搜索域中为java的文档
            Query query=parser.parse("i");
            //5 根据搜索返回topdocs
            TopDocs top=isearch.search(query, 10);
            //6 根据topdocs获取scoredoc对象
            ScoreDoc[] sdoc=top.scoreDocs;
            System.out.println("文件条目"+sdoc.length);
            for(ScoreDoc sd:sdoc){
                //7 获得文档document
                Document doc=isearch.doc(sd.doc);
                //8 显示结果
                System.out.println("文件:");
                System.out.println("文件名:"+doc.get("filename"));
                System.out.println("路径名:"+doc.get("path"));
                                                   
            }
                                           
        ir.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}      
package testfile;
import org.itat.test.HelloLucene;
import org.junit.Test;
public class test {
    @Test
    public void testindex(){
        HelloLucene h=new HelloLucene();
        h.index();
    }
    @Test
    public void testsearch(){
        HelloLucene h=new HelloLucene();
        h.search();
    }
}