scud(飞云小侠) http://www.jscud.com 转载请注明来源/作者
关键字:lucene,html parser,全文检索,IndexReader,Document,Field,IndexWriter,Term,HTMLPAGE
 Lucene是一个全文检索的引擎,目前有Java和.Net 等几个版本.Java版本的网址是http://lucene.apache.org.相关的一个项目是车东的WebLucene: http://sourceforge.net/projects/weblucene.
 首先,基于一个简单的新闻系统,要想做全文检索.新闻系统的管理等在这里不在具体提出,下面列出新闻对象的类:
 
 注:程序用会到一些工具类,不在此列出,用户可以自己实现.
 
 
| 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;
 
 
 /**
 * 一个新闻.
 *
 * @author scud(飞云小侠) http://www.jscud.com
 *
 */
 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等内容.