zoukankan      html  css  js  c++  java
  • Lucene中对document(记录)的CURD操作~为分布式全文检索设计

    Lucene.net是.net环境中比较强的全文检索工具,它是从JAVA中转过来的,.net版本的lucene在功能上也豪不逊色于java版的lucene。今天主要来说一下lucene索引文件在更新时的一些方式。

    一 整个索引文件 (cfs文件)覆盖更新;优点:简单,缺点:与服务器没有交互,但在生成索引文件时对IO影响比较大,前台lucene信息显示与数据库不同步。

    二 索引文件按需要更新(对document记录进行curd操作),优点:与数据库同步,缺点:与服务器交互多,对于curd的安全性要重视起来,但这样做是必须的。

    下面主要说一下第二种索引文件按需要更新的情况

    追加document(记录):当数据库表中有insert操作时,这时lucene也应该进行相应的insert操作,这就是追加,在IndexWriter中有AddDocument方法,它没什么好说的,按着方法签名转值即可,注意操作完成后要对IndexWriter进行Optimize和Close

     1        [WebMethod]
     2         public int AppendLuceneDocument(string primaryKey, string id, string name, string info, string categoryName, string propertyName, string module, string passKey)
     3         {
     4             int flag = 0;
     5             try
     6             {
     7                 dirInfo = Directory.CreateDirectory(this.GetIndexPath(ConfigurationManager.AppSettings[module]));
     8                 directory = LuceneIO.FSDirectory.Open(dirInfo);
     9                 IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), false, IndexWriter.MaxFieldLength.UNLIMITED);
    10                 Document doc = new Document();
    11                 doc.Add(new Field("PrimaryKey", primaryKey, Field.Store.YES, Field.Index.ANALYZED));
    12                 doc.Add(new Field("ID", id, Field.Store.YES, Field.Index.NO));
    13                 doc.Add(new Field("Name", name, Field.Store.YES, Field.Index.ANALYZED));
    14                 doc.Add(new Field("Info", info, Field.Store.YES, Field.Index.ANALYZED));
    15                 doc.Add(new Field("CategoryName", categoryName, Field.Store.YES, Field.Index.ANALYZED));
    16                 doc.Add(new Field("PropertyName", propertyName, Field.Store.YES, Field.Index.ANALYZED));
    17                 writer.AddDocument(doc);
    18                 writer.Optimize();
    19                 writer.Close();
    20                 flag = 1;
    21             }
    22             catch (Exception)
    23             {
    24 
    25                 throw;
    26             }
    27             return flag;
    28         }

    删除记录(document):这个操作需要我们注意几点:

    1 要删除的记录的依据应该具有唯一性,这样删除才有意义,并且这个字段在lucene存储时需要是ANALYZED,即可以被检索到

    2 删除时的条件最好使用Query,而不要使用Term,我做过很多测试,结果证明Term条件总是不要使。

    对于删除的代码如下:

     1      [WebMethod]
     2         public int DeleteLuceneDocument(string primaryKey, string module, string passKey)
     3         {
     4             int flag = 0;
     5             try
     6             {
     7                 dirInfo = Directory.CreateDirectory(this.GetIndexPath(ConfigurationManager.AppSettings[module]));
     8                 directory = LuceneIO.FSDirectory.Open(dirInfo);
     9                 IndexWriter writer = new IndexWriter(directory, standardAnalyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
    10                 QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "PrimaryKey", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
    11                 Query query = parser.Parse(primaryKey);
    12                 writer.DeleteDocuments(query);
    13                 writer.Commit();
    14                 writer.Optimize();
    15                 writer.Close();
    16                 flag = 1;
    17             }
    18             catch (Exception)
    19             {
    20 
    21                 throw;
    22             }
    23             return flag;
    24         }

    而更新操作事实上就是先把记录删除,再追加一条新的记录即可,而IndexWriter为我们提供的UpdateDocuments感觉更向是在复制一个,所以不建议使用它,

    而是手动删除和追加来完成这个update操作。

     1      [WebMethod]
     2         public int UpdateLuceneDocument(string primaryKey, string id, string name, string info, string categoryName, string propertyName, string module, string passKey)
     3         {
     4             int flag = 0;
     5             try
     6             {
     7                 dirInfo = Directory.CreateDirectory(this.GetIndexPath(ConfigurationManager.AppSettings[module]));
     8                 directory = LuceneIO.FSDirectory.Open(dirInfo);
     9                 IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), false, IndexWriter.MaxFieldLength.UNLIMITED);
    10                 Document doc = new Document();
    11                 doc.Add(new Field("PrimaryKey", primaryKey, Field.Store.YES, Field.Index.ANALYZED));
    12                 doc.Add(new Field("ID", id, Field.Store.YES, Field.Index.NO));
    13                 doc.Add(new Field("Name", name, Field.Store.YES, Field.Index.ANALYZED));
    14                 doc.Add(new Field("Info", info, Field.Store.YES, Field.Index.ANALYZED));
    15                 doc.Add(new Field("CategoryName", categoryName, Field.Store.YES, Field.Index.ANALYZED));
    16                 doc.Add(new Field("PropertyName", propertyName, Field.Store.YES, Field.Index.ANALYZED));
    17                 QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "PrimaryKey", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
    18                 Query query = parser.Parse(primaryKey);
    19                 writer.DeleteDocuments(query);
    20                 writer.Commit();
    21                 writer.AddDocument(doc);
    22                 writer.Optimize();
    23                 writer.Close();
    24                 flag = 1;
    25             }
    26             catch (Exception)
    27             {
    28 
    29                 throw;
    30             }
    31 
    32             return flag;
    33         }

    OK,这就是对索引文件进行按需的操作,以后我会把我的lucene架构整理成文章,供大家讨论。

  • 相关阅读:
    Android自定义之仿360Root大师水纹效果
    Android之TextView的Span样式源码剖析
    Android之TextView的样式类Span的使用详解
    随着ScrollView的滑动,渐渐的执行动画View
    仿微信主界面导航栏图标字体颜色的变化
    android自定义之 5.0 风格progressBar
    Android性能优化之内存篇
    Android性能优化之运算篇
    How to install Zabbix5.0 LTS version with Yum on the CentOS 7.8 system?
    How to install Zabbix4.0 LTS version with Yum on the Oracle Linux 7.3 system?
  • 原文地址:https://www.cnblogs.com/lori/p/2654275.html
Copyright © 2011-2022 走看看