读书人

应用Lucene进行全文检索(一)-处理索引

发布时间: 2012-11-18 10:51:21 作者: rapoo

使用Lucene进行全文检索(一)---处理索引(转载)

转载:http://www.jscud.com/srun/news/viewhtml/3_2005_8/76.htm

?

首先,基于一个简单的新闻系统,要想做全文检索.新闻系统的管理等在这里不在具体提出,下面列出新闻对象的类:
?
?注:程序用会到一些工具类,不在此列出,用户可以自己实现.
?
?

??package com.jscud.website.newsinfo.bean;
??
??
??import java.sql.timestamp;
??
??import com.jscud.util.datetime;
??import com.jscud.util.stringfunc;
??import com.jscud.website.newsinfo.newsconst;
??
??
??/**
?? * 一个新闻.
?? *?
?? *?
?? */
??public class newsitem
??{
??
????? private int nid; //新闻编号
??
????? private int cid; //类别编号
??
????? private string title;//标题
??
????? private int showtype; //内容类型:目前支持url和html
??
????? private string content;//内容
??
????? private string url;//对应网址,如果内容类型是url的话
??
????? private timestamp addtime; //增加时间
??
????? private int click; //点击数
?????
????? //对应的get,set函数,较多不在列出,可以使用工具生成
????? //......
??
?????
????? /**
?????? * 按照类型格式化
?????? */
????? public string getshowcontent()
????? {
????????? string sres = content;
????????? if(showtype == newsconst.showtype_html)
????????? {
????????? }??
????????? return sres;
????? }
?????
????? public string gettarget()
????? {
????????? if(showtype == newsconst.showtype_url)
????????? {
????????????? return "_blank";
????????? }
????????? else
????????????? return "";???????
????? }
?????
????? /**
?????? * 静态html文件的路径及其名字
?????? */
????? public string gethtmlfilename()
????? {
????????? int nyear = datetime.getyear_date(getaddtime());
????????? int nmonth =? datetime.getmonth_date(getaddtime());
?????????????
????????? string sgenefilename =
????????? ???"/news/" + getcid() + "/" + nyear + "/" + nmonth +"/" + getnid() + ".htm";
?????????
????????? return sgenefilename;
????? }
?????
????? /**
?????? * 静态html文件的路径
?????? */
????? public string gethtmlfilepath()
????? {
????????? int nyear = datetime.getyear_date(getaddtime());
????????? int nmonth =? datetime.getmonth_date(getaddtime());
?????????????
????????? string sgenefilepath =
????????? ???getcid() + "_" + nyear + "_" + nmonth;
?????????
????????? return sgenefilepath;
????? }?????
??}?


?
?可以看到,我们需要对标题和内容进行检索,为了这个目的,我们首先需要来研究一下lucene.
?
?在lucene中,如果要进行全文检索,必须要先建立索引然后才能进行检索,当然实际工作中还会有删除索引和更新索引的工作.
?
?在此之前,介绍一个最基本的类(摘抄自http://www.blogjava.net/cap/archive/2005/07/17/7849.html):
?
?analyzer 文件的分析器(听起来别扭,还是叫analyzer好了)的抽象,这个类用来处理分词(对中文尤其重要,转换大小写(computer->computer,实现查询大小写无关),转换词根(computers->computer),消除stop words等,还负责把其他格式文档转换为纯文本等.
?
?在lucene中,一般会使用standardanalyzer来分析内容,它支持中文等多字节语言,当然可以自己实现特殊的解析器.standardanalyzer目前对中文的处理是按照单字来处理的,这是最简单的办法,但是也有缺点,会组合出一些没有意义的结果来.?
?
?
?首先我们来了解建立索引,建立索引包含2种情况,一种是给一条新闻建立索引,另外的情况是在开始或者一定的时间给批量的新闻建立索引,所以为了通用,我们写一个通用的建立索引的函数:
?
?(一般一类的索引都放在一个目录下,这个配置可以在函数中定义,也可以写在配置文件中,通过参数传递给函数.)

??? /**
???? * 生成索引.
???? *
???? * @param doc 目标文档
???? * @param indexdir 索引目录
???? */
??? public static void makeindex(document doc, string indexdir)
??? {
??????? list alist = new arraylist();
??????? alist.add(doc);
??????? makeindex(alist, indexdir);
??? }
?
??? /**
???? * 生成索引.
???? *
???? * @param doc 生成的document.
???? * @param indexdir 索引目录
???? */
??? public static void makeindex(list docs, string indexdir)
??? {
??????? if (null == docs)
??????? {
??????????? return;
??????? }???????
??????? boolean indexexist = indexexist(indexdir);

??????? indexwriter writer = null;
??????? try
??????? {
??????????? standardanalyzer analyzer = new standardanalyzer();
???????????
??????????? //如果索引存在,就追加.如果不存在,就建立新的索引.lucene要是自动判决就好了.
??????????? if(indexexist)
??????????? {
??????????????? writer = new indexwriter(indexdir, analyzer, false);
??????????? }
??????????? else
??????????? {
??????????????? writer = new indexwriter(indexdir, analyzer, true);
??????????? }

??????????? //添加一条文档
??????????? for (int i = 0; i < docs.size(); i++)
??????????? {
??????????????? document doc = (document) docs.get(i);
??????????????? if (null != doc)
??????????????? {
??????????????????? writer.adddocument(doc);
??????????????? }
??????????? }

??????????? //索引完成后的处理
??????????? writer.optimize();
??????? }
??????? catch (ioexception e)
??????? {
??????????? logman.warn("error in make index", e);
??????? }
??????? finally
??????? {
??????????? try
??????????? {
??????????????? if (null != writer)
??????????????? {
??????????????????? writer.close();
??????????????? }
??????????? }
??????????? catch (ioexception e)
??????????? {
??????????????? logman.warn("close writer error");
??????????? }
??????? }
??? }



?可以看到,建立索引用到类是indexwrite,它可以新建索引或者追加索引,但是需要自己判断.判断是通过indexreader这个类来实现的,函数如下:

?

? /**
???? * 检查索引是否存在.
???? * @param indexdir
???? * @return
???? */
??? public static boolean indexexist(string indexdir)
??? {
??????? return indexreader.indexexists(indexdir);
??? }
?


?如果每次都是新建索引的话,会把原来的记录删除,我在使用的时候一开始就没有注意到,后来观察了一下索引文件,才发现这个问题.
?
?
?还可以看到,建立索引是给用户的document对象建立索引,document表示索引中的一条文档记录.那么我们如何建立一个文档那?以新闻系统为例,代码如下:
?

???? /**
????? * 生成新闻的document.
????? *
????? * @param anews 一条新闻.
????? *
????? * @return lucene的文档对象
????? */
???? public static document makenewssearchdocument(newsitem anews)
???? {
???????? document doc = new document();
?
???????? doc.add(field.keyword("nid", string.valueof(anews.getnid())));
?
???????? doc.add(field.text("title", anews.gettitle()));
????????
???????? //对html进行解析,如果不是html,则不需要解析.或者根据格式调用自己的解析方法
???????? string content = parsehtmlcontent(anews.getcontent());
?
???????? doc.add(field.unstored("content", content));
?
???????? doc.add(field.keyword("addtime", anews.getaddtime()));
?
???????? //可以加入其他的内容:例如新闻的评论等
???????? doc.add(field.unstored("other", ""));
?
???????? //访问url
???????? string newsurl = "/srun/news/viewhtml/" + anews.gethtmlfilepath() + "/" + anews.getnid()
???????????????????????? + ".htm";
?
???????? doc.add(field.unindexed("visiturl", newsurl));
?
???????? return doc;
???? }


?
?通过上面的代码,我们把一条新闻转换为lucene的document对象,从而进行索引工作.在上面的代码中,我们又引入了lucene中的field(字段)类.document文档就像数据库中的一条记录,它有很多字段,每个字段是一个field对象.
?
?从别的文章摘抄一段关于field的说明(摘抄自http://www.blogjava.net/cap/archive/2005/07/17/7849.html):
?[quote]
??? 类型?????????????????????????????? analyzed indexed stored 说明
??? field.keyword(string,string/date)? n y y??????????????????? 这个field用来储存会直接用来检索的比如(编号,姓名,日期等)
??? field.unindexed(string,string)???? n n y??????????????????? 不会用来检索的信息,但是检索后需要显示的,比如,硬件序列号,文档的url地址
??? field.unstored(string,string)????? y y n??????????????????? 大段文本内容,会用来检索,但是检索后不需要从index中取内容,可以根据url去load真实的内容
??? field.text(string,string)????????? y y y??????????????????? 检索,获取都需要的内容,直接放index中,不过这样会增大index
??? field.text(string,reader)????????? y y n??????????????????? 如果是一个reader, lucene猜测内容比较多,会采用unstored的策略.
?[/quote]
?
?我们可以看到新闻的编号是直接用来检索的,所以是keyword类型的字段,新闻的标题是需要检索和显示用的,所以是text类型,而新闻的内容因为是html格式的,所以在经过解析器的处理用,使用的unstored的格式,而新闻的时间是直接用来检索的,所以是keyword类型.为了在新闻索引后用户可以访问到完整的新闻页面,还设置了一个unindexed类型的访问地址字段.
?
?(对html进行解析的处理稍后在进行讲解)
?
?为一条新闻建立索引需要两个步骤:获取document,传给makeindex函数,代码如下:

??? public static void makenewsinfoindex(newsitem anews)
??? {
??????? if (null == anews)
??????? {
??????????? return;
??????? }
??????? makeindex(makenewssearchdocument(anews),indexdir);
??? }??


?

?
?
?建立索引的工作就进行完了,只要在增加新闻后调用 makenewsinfoindex(newsitem); 就可以建立索引了.
?
?如果需要删除新闻,那么也要删除对应的索引,删除索引是通过indexreader类来完成的:
?


??? /**
???? * 删除索引.
???? * @param aterm 索引删除条件
???? * @param indexdir 索引目录
???? */
??? public static void deleteindex(term aterm, string indexdir)
??? {
??????? list alist = new arraylist();
??????? alist.add(aterm);
??????? deleteindex(alist, indexdir);
??? }

??? /**
???? * 删除索引.
???? *
???? * @param aterm 索引删除条件.
???? * @param indexdir 索引目录
???? *?
???? */
??? public static void deleteindex(list terms, string indexdir)
??? {
??????? if (null == terms)
??????? {
??????????? return;
??????? }
???????
??????? if(!indexexist(indexdir)) { return; }

??????? indexreader reader = null;
??????? try
??????? {
??????????? reader = indexreader.open(indexdir);
??????????? for (int i = 0; i < terms.size(); i++)
??????????? {
??????????????? term aterm = (term) terms.get(i);
??????????????? if (null != aterm)
??????????????? {
??????????????????? reader.delete(aterm);
??????????????? }
??????????? }
??????? }
??????? catch (ioexception e)
??????? {
??????????? logman.warn("error in delete index", e);
??????? }
??????? finally
??????? {
??????????? try
??????????? {
??????????????? if (null != reader)
??????????????? {
??????????????????? reader.close();
??????????????? }
??????????? }
??????????? catch (ioexception e)
??????????? {
??????????????? logman.warn("close reader error");
??????????? }
??????? }
??? }?


?
?删除索引需要一个条件,类似数据库中的字段条件,例如删除一条新闻的代码如下:
?

???? public static void deletenewsinfoindex(int nid)
???? {
???????? term aterm = new term("nid", string.valueof(nid));
???????? deleteindex(aterm,indexdir);
???? }???




?通过新闻的id,就可以删除一条新闻.
?
?如果需要更新新闻,如何更新索引哪? 更新索引需要先删除索引然后新建索引2个步骤,其实就是把上面的代码组合起来,例如更新一条新闻:

???? public static void updatenewsinfoindex(newsitem anews)
???? {
???????? if (null == anews)
???????? {
???????????? return;
???????? }
???????? deletenewsinfoindex(anews.getnid());
???????? makenewsinfoindex(anews);
???? }?
?



?
?至此,索引的建立更新和删除就告一段落了.其中批量更新新闻的代码如下:
?(批量更新应该在访问人数少或者后台程序在夜间执行)

??? public static void makeallnewsinfoindex(list newslist)
??? {
??????? list terms = new arraylist();
??????? list docs = new arraylist();

??????? for (int i = 0; i < newslist.size(); i++)
??????? {
??????????? newsitem aitem = (newsitem) newslist.get(i);
??????????? if (null != aitem)
??????????? {
??????????????? terms.add(new term("nid", string.valueof(aitem.getnid())));
??????????????? docs.add(makenewssearchdocument(aitem));
??????????? }
??????? }

??????? deleteindex(terms,indexdir);
??????? makeindex(docs,indexdir);
??? }?



?
?
?下一节讲解如何对要建立索引的内容进行解析,例如解析html等内容.

@author scud(飞云小侠) http://www.jscud.com 转贴却不在明显位置注明出处,不太厚道!!!


3 楼 无明 2007-05-14 原文出处呢?转载的话起码要给出原文的链接 4 楼 junjie314 2007-05-14 已给出把原文出处! 谢谢提醒 5 楼 yfmine 2007-05-14 http://www.jscud.com/srun/news/viewhtml/3_2005_8/76.htm
http://www.jscud.com/srun/news/viewhtml/3_2005_8/78.htm
http://www.jscud.com/srun/news/viewhtml/3_2005_8/78.htm
你的原文地址没对吧 6 楼 junjie314 2007-05-14 请看清楚好吗?原文地址写在上面呢
http://www.z6688.com/info/46145-1.htm
http://www.z6688.com/info/46146-1.htm
http://www.z6688.com/info/46147-1.htm
7 楼 yfmine 2007-05-14 junjie314 写道请看清楚好吗?原文地址写在上面呢
http://www.z6688.com/info/46145-1.htm
http://www.z6688.com/info/46146-1.htm
http://www.z6688.com/info/46147-1.htm

...是你自己没看清楚

PS:现在这些网站,转载了也不说个地址,好点的呢写个原文所在的网站,其他的呢就直接来个“网友供稿”、“来源:网络”…… 8 楼 junjie314 2007-05-14 反正我是那里转载过来的,

读书人网 >软件架构设计

热点推荐