• Lucene.net入门学习系列(2)


    Lucene.net入门学习系列(2)

          Lucene.net入门学习系列(1)-分词

      Lucene.net入门学习系列(2)-创建索引

      Lucene.net入门学习系列(3)-全文检索

      

      在使用Lucene.net进行全文检索之前,需要写入索引,然后对索引进行检索。下面我们来看看如何建立索引。

      具体步骤如下:

      1.使用FSDirectory类打开一个索引文件

      2.使用IndexWriter类写来写索引

      3.关闭IndexWriter  

    复制代码
     1         /// <summary>
     2         /// 创建索引
     3         /// </summary>
     4         private void CreateIndex()
     5         {
     6             //索引的文件存放的路径
     7             string indexPath = @"Lucene";
     8 
     9             //FSDirectory是用于对文件系统目录的操作的类
    10             FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
    11             //检查目录是否存在
    12             bool isUpdate = IndexReader.IndexExists(directory);
    13 
    14             if (isUpdate)
    15             {
    16                 //目录存在则判断目录是否被锁定,被锁定就解锁
    17                 if (IndexWriter.IsLocked(directory))
    18                 {
    19                     IndexWriter.Unlock(directory);
    20                 }
    21             }
    22             //IndexWriter主要用于写索引
    23             //方法签名:public IndexWriter(Directory d,Analyzer a,boolean create,IndexWriter.MaxFieldLength mfl)
    24             //第一个参数是 (Directory d):索引的目录(前面的FSDirectory类的对象)
    25             //第二个参数是 (Analyzer a):分析器(这里我们用盘古分词的分析器)
    26             //第三个参数是 (boolean create):是否创建目录
    27             //第四个参数是 (IndexWriter.MaxFieldLength):最大长度
    28             IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate,
    29                 IndexWriter.MaxFieldLength.UNLIMITED);
    30 
    31             //BLL层的一个类,用于对表T_Article进行操作
    32             //T_Article表中有三个字段: Id   Title  Message
    33             T_ArticleBLL bll = new T_ArticleBLL();
    34 
    35             //遍历T_Article表中的内容
    36             foreach (T_Articles art in bll.GetAll())
    37             {
    38                 writer.DeleteDocuments(new Term("id", art.ID.ToString()));
    39 
    40                 //Document文档对象
    41                 Document document = new Document();
    42 
    43                 //将T_Articles表中的内容写入索引
    44                 document.Add(new Field("id", art.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    45 
    46                 document.Add(new Field("title", art.Title, Field.Store.YES, Field.Index.ANALYZED,
    47                     Field.TermVector.WITH_POSITIONS_OFFSETS));
    48 
    49                 document.Add(new Field("msg", art.Message, Field.Store.YES, Field.Index.ANALYZED,
    50                     Field.TermVector.WITH_POSITIONS_OFFSETS));
    51                 writer.AddDocument(document);
    52             }
    53             //要记得关闭
    54             writer.Close();
    55             directory.Close();
    56         }
    复制代码

     在上面的例子中,我们使用FSDirectory类来对索引文件进行操作,要注意的是索引不光可以写到文件中,索引也可以写到内存(使用RAMDirectory类)中。

       索引创建好了之后,我们还可以根据需求来对索引进行不同的优化,以达到更好的检索效果。

     
     
    分类: c#Lucene.net
  • 相关阅读:
    Linux 安全工具之extundelete误删除恢复
    Linux安全工具之fail2ban防爆力破解
    构建高效安全的Nginx Web服务器
    必读说明
    U盘因格式化 NTFS 中断造成无法识别,生产平台同样无法识别的修复处理方案
    计算机等级考试【二级C语言程序设计】知识点整理
    MD 使用 i5ting_toc 转换成 HTML
    Windows10 图标变白修复
    【已解决】老型号电脑需要按F1键才能进入系统
    【已解决】什么是心跳包?
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3253005.html
Copyright © 2020-2023  润新知