`
iluoxuan
  • 浏览: 572308 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

lucene4.0入门实例

 
阅读更多

1:以前用3.5的时候,到现在也差不多忘了,重新看了下文档,写个简单的例子

 

lucene4.0中有很多新的东西,其中Field类主要不能new Field()要通过其子类去实现比如new StringField()等,对分词等参数也有部分变化。

 

创建索引的代码如下:

 

 

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
  • 大小: 7 KB
分享到:
评论
10 楼 2047699523 2015-04-02  
apache lucene开源框架demo使用实例教程源代码下载:http://www.zuidaima.com/share/klucene-p1-s1.htm
9 楼 u012046823 2014-12-11  
8 楼 jwc19890114 2014-11-08  
想问一下,我现在已经创建好了索引,想统计索引中所有词汇出现的次数,这个需要使用查询吗?
7 楼 高军威 2013-09-28  
嗯 嗯 楼主 排序sort分页 没有吗 分享下把
6 楼 tianmacgw 2013-04-23  
写的很好,学习了
5 楼 LovingBaby 2013-04-16  
跑了一下,确实是入门的好例子,不过应该搭配一下基础的流程介绍,会好一些~期待佳作!
4 楼 w120435999 2013-02-21  
   
3 楼 xmtopaz 2013-01-30  
    
2 楼 53873039oycg 2013-01-22  
不错的例子!
1 楼 whjpyyyy 2013-01-04  
good。。

相关推荐

Global site tag (gtag.js) - Google Analytics