Lucene.net 实现全文检索

Lucene.Net 是什么?Lucene.Net 能作什么?以及怎么做的问题?最后给出 Lucene.Net 实现全文搜索的一个示例

1、Lucene.Net 是什么?

Lucene.net 起初是一个开源项目然后转向商业化,也在Lucene.net 2.0已经发布,不过是要money D ,Lucene.net的命运有点类似于FreeTextBox ,它在 1.6.5 版本之后发布的 2.0 开始了商业路线,2.0 提供了 DLL 方式的免费版本,源代码版本则必须购买商业的许可 licence;不过它留下了 1.6.5 版本的源代码,还是可以看到大部分的内部细节,但 2.0 版本中添加的对 Mozilla 浏览器的支持部分只有通过它生成的 HTML 和 JavaScript 脚本去窥测。

Lucene 是 Java 世界中常用的索引 API,使用它提供的方法可以为文本资料创建索引,并提供检索。(参考:NLucene 和 Lucene .NET)NLucene 是第一个的 .net 移植,也是一个有 .net 风格的版本,使用 .net 的命名规范和类库设计。不过 NLucene 项目的 leader 由于精力原因,只发布了 1.2beta 版本。Lucene.NET 项目出现后,NLucene 就没有新的计划了。

Lucene.NET 当初号称要做 up-to-date 的 .net Lucene 移植,它只在命名方面采纳了 .net 的建议,主要目标倾向于和 Java Lucene 兼容:一个是索引格式兼容,达到可以共同工作的目的;一个是命名接近(只相差很少,比如大小写等),目的是可以方便开发者使用 Java Lucene 相关的代码和资料。

不知什么时候 Lucene.NET 项目已经放弃了开源计划,转向了商业。它居然把 SourceForge 上已经开源的文件也删除了。与此同时,SourceForge 上又出现了 dotLucene 项目,出于对 Lucene.NET 的抗议,dotLucene 几乎将 Lucene.NET 的代码原封不动放在上面作为他们的起点。(https://sourceforge.net/forum/forum.php?thread_id=1153933&forum_id=408004)。

说白了Lucene.Net就是是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能.

Lucene的使用者不必深入了解有关全文检索的知识,仅仅学会使用库中的几个类,知道怎么调用Library中的函数,就可以为你的应用实现全文检索的功能.

不过千万别期望Lucene是一个象google和百度那样的搜索引擎,它仅仅是一个工具,一个Library.你也可以把它理解为一个将索引,搜索功能封装的很好的一套简单易用的API.利用这套API你可以做很多有关搜索的事情,而且很方便,它可以满足你对一个应用做简单的全文搜索,作为应用的开发者(非专业搜索引擎开发者)来说,它的功能足以满足你。

2、Lucene.Net 可以作什么?

Lucene可以对任何的数据做索引和搜索. Lucene不管数据源是什么格式,只要它能被转化为文字的形式,就可以被Lucene所分析利用.也就是说不管是MS word, Html ,pdf还是其他什么形式的文件只要你可以从中抽取出文字形式的内容就可以被Lucene所用.你就可以用Lucene对它们进行索引以及搜索.

3、使用 Lucene.Net 怎么做?

简单的归结为:创建索引,和使用索引,其中创建索引就是将要搜索的数据源的那些信息作为我们的关键信息来存储或者是分析,为搜索留下标记就象Word里面创建目录(个人理解),使用索引就是在搜索的时候根据索引的信息来分析数据源将我们需要的信息提取出来。

具体请看一下示例:

创建索引的类


csharp 代码
  1. public class IntranetIndexer
  2.  {
  3.  /**/////索引写入器
  4.  private IndexWriter writer;
  5. 
  6.  //要写入索引的文件的根目录
  7.  private string docRootDirectory;
  8. 
  9.  //要匹配的文件格式
  10.  private string[] pattern;
  11. 
  12.  /**//// 
  13.  /// 初始化一个索引写入器writer,directory为创建索引的目录,true代表如果不存在索引文件将重新创建索引文件,如果已经存在索引文件将覆写索引文件
  14.  /// 
  15.  /// 
  16. 传入的要创建索引的目录,注意是字符串值,如果目录不存在,他将会被自动创建
  17.  public IntranetIndexer(string directory)
  18.  {
  19.  writer = new IndexWriter(directory, new StandardAnalyzer(), true);
  20.  writer.SetUseCompoundFile(true);
  21.  }
  22.  
  23.  public void AddDirectory(DirectoryInfo directory, string [] pattern)
  24.  {
  25.  this.docRootDirectory = directory.FullName;
  26.  this.pattern = pattern;
  27.  addSubDirectory(directory);
  28.  }
  29.  
  30.  private void addSubDirectory(DirectoryInfo directory)
  31.  {
  32.  for(int i=0;i
  33.  {
  34.  foreach (FileInfo fi in directory.GetFiles(pattern[i]))
  35.  {
  36.  AddHtmlDocument(fi.FullName);
  37.  }
  38.  }
  39.  foreach (DirectoryInfo di in directory.GetDirectories())
  40.  {
  41.  addSubDirectory(di);
  42.  }
  43.  }
  44.  
  45.  public void AddHtmlDocument(string path)
  46.  {
  47.  string exname=Path.GetExtension (path);
  48.  Document doc = new Document(); 
  49.  
  50.  string html; 
  51.  if(exname.ToLower ()==".html" ||exname .ToLower ()==".htm"||exname .ToLower ()==".txt")
  52.  {
  53.  using(StreamReader sr=new StreamReader (path,System .Text .Encoding .Default ))
  54.  { 
  55.  html = sr.ReadToEnd();
  56.  }
  57.  }
  58.  else
  59.  {
  60.  using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Unicode ))
  61.  {
  62.  html = sr.ReadToEnd();
  63.  }
  64.  }
  65. 
  66.  int relativePathStartsAt = this.docRootDirectory.EndsWith("\\") ? this.docRootDirectory.Length : this.docRootDirectory.Length + 1;
  67.  string relativePath = path.Substring(relativePathStartsAt);
  68.  string title=Path.GetFileName(path);
  69.  
  70.  //判断若是网页则去标签否则不用
  71.  if(exname.ToLower ()==".html" ||exname .ToLower ()==".htm")
  72.  {
  73.  doc.Add(Field.UnStored("text", parseHtml(html)));
  74.  }
  75.  else
  76.  {
  77.  doc.Add (Field .UnStored ("text",html));
  78.  }
  79.  doc.Add(Field.Keyword("path", relativePath));
  80.  //doc.Add(Field.Text("title", getTitle(html)));
  81.  doc.Add (Field .Text ("title",title));
  82.  writer.AddDocument(doc);
  83.  } 
  84.  /**//// 
  85.  /// 去除网页中的标签
  86.  /// 
  87.  /// 
  88. 网页
  89.  /// 
  90. 返回去除后的网页文本
  91.  private string parseHtml(string html)
  92.  {
  93.  string temp = Regex.Replace(html, "<[^>]*>""");
  94.  return temp.Replace(" "" ");
  95.  }
  96.  /**//// 
  97.  /// 获取网页标题
  98.  /// 
  99.  /// 
  100.  /// 
  101.  private string getTitle(string html)
  102.  {
  103.  Match m = Regex.Match(html, "");
  104.  if (m.Groups.Count == 2)
  105.  return m.Groups[1].Value;
  106.  return "文档标题未知";
  107.  }
  108.  /**//// 
  109.  /// 优化索引并关闭写入器
  110.  /// 
  111.  public void Close()
  112.  {
  113.  writer.Optimize();
  114.  writer.Close();
  115.  }
  116.  }



首先建立Document对象,然后为Document对象添加一些属性Field.你可以把Document对象看成是虚拟文件,将来将从此获取信息.而Field则看成是描述此虚拟文件的元数据(metadata).其中Field包括四个类型: Keywork

 该类型的数据将不被分析,而会被索引并保存保存在索引中.

UnIndexed
 该类型的数据不会被分析也不会被索引,但是会保存在索引.

UnStored
 和UnIndexed刚好相反,被分析被索引,但是不被保存.

Text
 和UnStrored类似.如果值的类型为string还会被保存.如果值的类型为Reader就不会被保存和UnStored一样.


最后将每一个Document添加到索引当中。

下面是对索引进行搜索



csharp 代码
  1. //创建一个索引器
  2.  IndexSearcher searcher = new IndexSearcher(indexDirectory); 
  3.  //解析索引的text字段以便搜索
  4.  Query query = QueryParser.Parse(this.Q, "text"new StandardAnalyzer()); 
  5.  //将搜索结果放在hits中
  6.  Hits hits = searcher.Search(query); 
  7.  //统计搜索的总记录数
  8.  this.total = hits.Length(); 
  9.  //高亮显示
  10.  QueryHighlightExtractor highlighter = new QueryHighlightExtractor(query, new StandardAnalyzer(), "
  11. ", "
  12. ");


第一步利用IndexSearcher打开索引文件用于后面搜索,其中的参数是索引文件的路径.

第二步使用QueryParser将可读性较好的查询语句(比如查询的词lucene ,以及一些高级方式lucene AND .net)转化为Lucene内部使用的查询对象.

第三步执行搜索.并将结果返回到hits集合.需要注意的是Lucene并不是一次将所有的结果放入hits中而是采取一次放一部分的方式.出于空间考虑.

然后将搜索的结果进行处理并在页面上显示出来:


csharp 代码
  1. for (int i = startAt; i < resultsCount; i++) 
  2.  {
  3.  
  4.  Document doc = hits.Doc(i); 
  5.  
  6.  string path = doc.Get("path"); 
  7. 
  8.  string location =Server.MapPath("documents")+"\\"+path;
  9.  string exname=Path.GetExtension (path);
  10.  
  11.  string plainText ;
  12.  string str=doc.Get ("title");
  13.  if(exname==".html" || exname ==".htm" || exname ==".txt")
  14.  {
  15.  using (StreamReader sr = new StreamReader(location, System.Text.Encoding.Default))
  16.  {
  17.  plainText = parseHtml(sr.ReadToEnd());
  18.  }
  19.  }
  20.  else
  21.  {
  22.  using (StreamReader sr = new StreamReader(location, System.Text.Encoding.Unicode ))
  23.  {
  24.  plainText = sr.ReadToEnd();
  25.  }
  26.  }
  27.  
  28.  //DataTable 添加行 
  29.  DataRow row = this.Results.NewRow();
  30.  row["title"] = doc.Get("title");
  31.  string IP=Request.Url.Host;//获取服务器IP
  32.  //Request.Url.Port; 
  33.  row["path"]=@"http://"+IP+"/WebUI/Search/documents/"+path;
  34.  row["sample"] = highlighter.GetBestFragments(plainText, 80, 2, ""); 
  35.  this.Results.Rows.Add(row);
  36.  } 
  37.  searcher.Close();//关闭搜索器

 

posted @ 2008-04-12 21:47  yuanyue82  阅读(325)  评论(0编辑  收藏  举报