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
= "索引添加成功";
   }
 

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

原文地址:https://www.cnblogs.com/joylee/p/1610936.html