lucene简单实例

创建索引的代码如下:

package com.search.lucene;

import java.io.File;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Before;
import org.junit.Test;

public class IndexFile {

    protected String[] ids={"1", "2"};

    protected String[] content={"Amsterdam has lost of add  cancals", "i love  add this girl"};

    protected String[] city={"Amsterdam", "Venice"};

    private Directory dir;

    /**
     * 初始添加文档
     * @throws Exception
     */
    @Test
    public void init() throws Exception {
        String pathFile="D://lucene/index";
        dir=FSDirectory.open(new File(pathFile));
        IndexWriter writer=getWriter();
        for(int i=0; i < ids.length; i++) {
            Document doc=new Document();
            doc.add(new StringField("id", ids[i], Store.YES));
            doc.add(new TextField("content", content[i], Store.YES));
            doc.add(new StringField("city", city[i], Store.YES));
            writer.addDocument(doc);
        }
        System.out.println("init ok?");
        writer.close();
    }

    /**
     * 获得IndexWriter对象
     * @return
     * @throws Exception
     */
    public IndexWriter getWriter() throws Exception {
        Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_40);
        IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_40, analyzer);
        return new IndexWriter(dir, iwc);
    }

}

 说明下:lucene4.0中有核心包和其他包:我导入

package com.search.lucene;

import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

public class IndexSearch {

    /**
     * 查询
     * @throws Exception
     */
    @Test
    public void search() throws Exception {
        String filePath="D://lucene/index";
        Directory dir=FSDirectory.open(new File(filePath));
        IndexReader reader=DirectoryReader.open(dir);
        IndexSearcher searcher=new IndexSearcher(reader);
        Term term=new Term("content", "add");
        TermQuery query=new TermQuery(term);
        TopDocs topdocs=searcher.search(query, 5);
        ScoreDoc[] scoreDocs=topdocs.scoreDocs;
        System.out.println("查询结果总数---" + topdocs.totalHits+"最大的评分--"+topdocs.getMaxScore());
        for(int i=0; i < scoreDocs.length; i++) {
            int doc = scoreDocs[i].doc;
            Document document = searcher.doc(doc);
            System.out.println("content===="+document.get("content"));
            System.out.println("id--" + scoreDocs[i].doc + "---scors--" + scoreDocs[i].score+"---index--"+scoreDocs[i].shardIndex);
        }
        reader.close();
    }
}

 

result:

  查询结果总数---2最大的评分--0.2972674

content====i love  add this girl
id--1---scors--0.2972674---index---1
content====Amsterdam has lost of add  cancals
id--0---scors--0.26010898---index---1

 

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