luncene

一、信息检索和全文检索

信息检索就是从信息集合中找出与用户需求相关的信息。被检索的信息除了文本外,还有图像、音频、视频等多媒体信息。我们只关注文本的检索,把用户的查询请求和全文中的每一个词进行比较,不考虑查询请求与文本语义上的匹配,这叫做全文检索。在信息检索工具中,全文检索是最具通用性和实用性的。例如,使用百度从一大堆网页中搜出与“传智播客”相关的网页。

我们简单看一下检索技术的流程:

请求

clip_image001

上面可见索引数据库是十分重要的,简单的说。全文检索系统,将网络上的数据通过某种格式保存到索引库中。当用户发送查询请求时,实质上就是向索引库查询。全文检索引擎负责处理用户的请求用索引库的更新等。

小时候查的汉语字典、英语词典…我们是如何查询的?当然不是一页一页的翻了,靠的是字典的目录。Lucene的检索方式正是使用了此技术,lucene的索引库格式:

检索目录

数据

索引

关键字

索引

Documents…

1

C、汇编

1

汇编比机器在语言高级…C比汇编高级

2

C++

2

C++比C高级..

3

Java、J2E

3

JAVA应用级,最为优秀的语言

4

WEB

4

WEB是真正的计算机…

5

你好

5

Hi,你好啊!

上面的表只是简单说明lucene索引库的存储格式。我们通过lucene提供的类可以向索引库添加、修改、查询、删除操作…。

关键字是通过分词器分析出来的,一般情况下各有语言有各字的分词器。分词器的强大提高了查询数据的接近性。

二、lucene操作

我们编写一个对Article对象向lucene索引库的添加、修改、查询、删除操作:

import java.util.ArrayList;

import java.util.List;

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.Term;

import org.apache.lucene.index.IndexWriter.MaxFieldLength;

import org.apache.lucene.queryParser.MultiFieldQueryParser;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.TopDocs;

import cn.itcast.cc.lucene.helloword.Article;

import cn.itcast.cc.lucene.helloword.utils.ArticleDocUtils;

publicclass IndexDao {

// 索引目录

private String indexPath = "./index";

// 分词器

private Analyzer analyzer = new StandardAnalyzer();

/**

*保存记录

*

*@paramart

*/

publicvoid save(Article art) {

// lucene的写出索引类

IndexWriter indexWriter = null;

try {

indexWriter = new IndexWriter(this.indexPath, this.analyzer,

MaxFieldLength.LIMITED);

// 添加到索引库

indexWriter.addDocument(ArticleDocUtils.Article2Doc(art));

} catch (Exception e) {

e.printStackTrace();

}

// 释放indexWriter

if (indexWriter != null) {

try {

// 使用后一定要关闭

indexWriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

*更新记录

*

*@paramart

*/

publicvoid update(Article art) {

IndexWriter indexWriter = null;

try {

indexWriter = new IndexWriter(this.indexPath, this.analyzer,

MaxFieldLength.LIMITED);

Term term = new Term("id", art.getId() + "");

// 更新

indexWriter.updateDocument(term, ArticleDocUtils.Article2Doc(art));

} catch (Exception e) {

e.printStackTrace();

}

// 释放indexWriter

if (indexWriter != null) {

try {

indexWriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

*删除记录

*

*@paramid

*/

publicvoid delete(int id) {

IndexWriter indexWriter = null;

try {

indexWriter = new IndexWriter(this.indexPath, this.analyzer,

MaxFieldLength.LIMITED);

Term term = new Term("id", id + "");

// 删除

indexWriter.deleteDocuments(term);

} catch (Exception e1) {

e1.printStackTrace();

}

// 释放indexWriter

if (indexWriter != null) {

try {

indexWriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

*查询记录(具有分页功能)

*

*@paramqueryString

*@paramstartIndex

*@paramrecordCount

*@return

*/

public List search(String queryString, int startIndex, int recordCount) {

List result = new ArrayList();

IndexSearcher indexSearcher = null;

try {

indexSearcher = new IndexSearcher(this.indexPath);

String[] fields = new String[] {"title","content"};

// 分析查询条件

QueryParser queryParser = new MultiFieldQueryParser(fields,

this.analyzer);

// 生成查询对象

Query query = queryParser.parse(queryString);

int findTotalRecord = startIndex + recordCount;

// 查询

TopDocs topDocs = indexSearcher.search(query, null, findTotalRecord);

//获取分页数据

int endIndex = Math.min(startIndex+recordCount,topDocs.totalHits);

for(int i=startIndex; i<endIndex;i++){

result.add(indexSearcher.doc(topDocs.scoreDocs[i].doc));

}

} catch (Exception e1) {

e1.printStackTrace();

}

// 释放indexWriter

if (indexSearcher != null) {

try {

indexSearcher.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return result;

}

}

其中使用到的“ArticleDocUtils”如下:

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.document.Field.Index;

import org.apache.lucene.document.Field.Store;

import cn.itcast.cc.lucene.helloword.Article;

publicclass ArticleDocUtils {

publicstatic Document Article2Doc(Article art){

// lucene索引库中的document对象

Document doc = new Document();

// 添加到document中的都是field对象,注册其中的key,检索时会用到。

// Store是否存储,决定对象是临时性还是持久性的

// Index在库中建立什么样的索引

doc.add(new Field("id", art.getId()+"", Store.YES, Index.NOT_ANALYZED));

doc.add(new Field("title", art.getTitle(), Store.YES, Index.ANALYZED));

doc.add(new Field("content", art.getContent(), Store.YES, Index.ANALYZED));

return doc;

}

publicstatic Article Doc2Article(Document doc){

Article art = new Article();

art.setId(Integer.parseInt(doc.getField("id").stringValue()));

art.setTitle(doc.getField("title").stringValue());

art.setContent(doc.getField("content").stringValue());

return art;

}

需要导入的jar包:lucene-analyzers-2.4.0.jar、lucene-core-2.4.0.jar、lucene-highlighter-2.4.0.jar。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值