Lucene.net搜索——创建索引

介绍lucene的索引工具前我先介绍两个重要的非常重要的类:DocumentField。

Document和真实存在硬盘中的文件是没有任何关系的,它只是向Lucene提供原始的要索引的文件的内容,也是就索引的操作都是基于Document来操作的。

我个人有个不太恰当但是非常好记忆的方法,把Document当作一个数据库,而其中的Field当作是数据中的某个字段。

现在有个商品管理系统,存放商品的名称,价格,发布时间,详情等,那么我们的Document的应该如下建立:
    

  Document doc = new Document();
  doc.Add(
new Field("goodsname", content, Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(
new Field("createdate", content, Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(
new Field("price", content, Field.Store.YES, Field.Index.TOKENIZED));
  doc.Add(
new Field("detail", content, Field.Store.YES, Field.Index.TOKENIZED));

 

对于上面这段代码我主要看Document的操作,主要是Field类。

new Field("goodsname", content, Field.Store.YES, Field.Index.TOKENIZED)

四个参数的意思分别是:field的名称,添加到Field的详细内容,是否存储,是否分词

为了提高运行的速度,我们就不需要对商品的发布时间和价格进行分词,不需要存储商品的详情。

经过上面的一个步骤我们就可以理解为我现在建了一个数据库,就是我上面的doc,这个数据库中存放了以下的几个字段goodsname,createdate,price,detail。

 

索引的格式我们通过Document建立好,那么我们怎么来建立我们的索引呢,这就用到我们非常重要的一个工具IndexWriter.

IndexWriter作用:创建索引、合并索引、控制索引相关的各方面

我们先看下IndexWriter的构造函数

 

复制代码
public IndexWriter(Directory d, Analyzer a);
public IndexWriter(FileInfo path, Analyzer a);
public IndexWriter(string path, Analyzer a);
public IndexWriter(Directory d, Analyzer a, bool create);
public IndexWriter(Directory d, bool autoCommit, Analyzer a);
public IndexWriter(FileInfo path, Analyzer a, bool create);
public IndexWriter(string path, Analyzer a, bool create);
public IndexWriter(Directory d, bool autoCommit, Analyzer a, bool create);
public IndexWriter(Directory d, bool autoCommit, Analyzer a, IndexDeletionPolicy deletionPolicy);
public IndexWriter(Directory d, bool autoCommit, Analyzer a, bool create, IndexDeletionPolicy deletionPolicy);
复制代码

 

其中第一个参数都是表示索引存放的位置,这个容易理解。

Analyzer表示用哪个分词类来建立索引,bool create表示是否要重新建立索引,一般情况下第一次建立索引的时候为True,追加建立索引的时候为False.

初始化索引以后我们要向索引中添加Document,用到的方法是

public virtual void AddDocument(Document doc);

Document表示我们刚才建立的文档。

 

完整的代码

复制代码
   /// <summary>
  
/// 索引文件存放路径
  
/// </summary>

   protected string indexpath = HttpContext.Current.Server.MapPath("/indexdir/");
  
/// <summary>
  
/// 建立索引
  
/// </summary>
  
/// <param name="content">建立索引的内容(需要被搜索的内容)</param>

   protected void CreateIndex(string title,string createdate,string price,string detail)
  
{
       IndexWriter indexwrite
= new IndexWriter(indexpath,new StandardAnalyzer());//索引文件存储的路径
       Document doc = new Document();
       doc.Add(
new Field("goodsname", title, Field.Store.YES, Field.Index.TOKENIZED));
       doc.Add(
new Field("createdate", createdate, Field.Store.YES, Field.Index.TOKENIZED));
       doc.Add(
new Field("price", price, Field.Store.YES, Field.Index.TOKENIZED));
       doc.Add(
new Field("detail", detail, Field.Store.YES, Field.Index.TOKENIZED));
       indexwrite.AddDocument(doc);
       indexwrite.Close();
   }

  
protected void btnCreateindex_Click(object sender, EventArgs e)
  
{
      
//为索引添加多个内容
       CreateIndex("诺基亚手机","2009-11-24","2100","这里是诺基亚手机的详细内容");
       CreateIndex(
"笔记本电脑", "2009-11-24", "2100", "这里是笔记本电脑的详细内容");
       CreateIndex(
"最新PSP", "2009-11-24", "2100", "这里是psp内容");
       lbmsg.Text
= "索引添加成功";
   }
 
复制代码

下篇文章会介绍索引的优化、查看、删除等,会一起附上源代码

作者:joylee

出处:https://www.cnblogs.com/joylee/archive/2009/11/25/1610936.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   IT米粉  阅读(798)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题