Răsfoiți Sursa

Merge pull request #21 from yangbocheng/master

修复索引删除失败的问题; 并添加IndexId的生成函数用于如实体是guid类型的主键,通过该生成函数可去除IndexId的TypeName前缀
懒得勤快 3 ani în urmă
părinte
comite
c8778367a0

+ 20 - 1
Masuit.LuceneEFCore.SearchEngine/LuceneIndexableBaseEntity.cs

@@ -41,7 +41,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         [NotMapped, JsonIgnore]
         public string IndexId
         {
-            get => GetType().Name + ":" + Id;
+            get => LuceneIndexerOptions.IndexIdGenerator(GetType(), Id);
 
             set
             {
@@ -71,6 +71,25 @@ namespace Masuit.LuceneEFCore.SearchEngine
                     continue;
                 }
 
+                {
+                    //1. 该处修复用IndexId去删除索引无效的问题
+                    if (propertyInfo.Name == nameof(LuceneIndexableBaseEntity.IndexId))
+                    {
+                        var filed = new Field(propertyInfo.Name, propertyValue.ToString(), new FieldType()
+                        {
+                            IsStored = true,
+                            IsIndexed = true,
+                            IsTokenized = false
+                        });
+                        doc.Add(filed);
+                        continue;
+                    }
+
+                    //2. 以Id为目标的删除放在其他处: 也利用到了IndexId
+
+
+                }
+
                 var attrs = propertyInfo.GetCustomAttributes<LuceneIndexAttribute>();
                 foreach (var attr in attrs)
                 {

+ 3 - 3
Masuit.LuceneEFCore.SearchEngine/LuceneIndexer.cs

@@ -132,7 +132,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
         }
 
         /// <summary>
-        /// 更新索引
+        /// 更新索引-删除索引时仅利用IndexId去删除
         /// </summary>
         /// <param name="changeset">实体</param>
         public void Update(LuceneIndexChangeset changeset)
@@ -144,13 +144,13 @@ namespace Masuit.LuceneEFCore.SearchEngine
                 switch (change.State)
                 {
                     case LuceneIndexState.Removed:
-                        writer.DeleteDocuments(new Term("Id", change.Entity.Id.ToString()));
+                        //writer.DeleteDocuments(new Term("Id", change.Entity.Id.ToString()));
                         writer.DeleteDocuments(new Term("IndexId", change.Entity.IndexId));
                         break;
 
                     case LuceneIndexState.Added:
                     case LuceneIndexState.Updated:
-                        writer.DeleteDocuments(new Term("Id", change.Entity.Id.ToString()));
+                        //writer.DeleteDocuments(new Term("Id", change.Entity.Id.ToString()));
                         writer.DeleteDocuments(new Term("IndexId", change.Entity.IndexId));
                         writer.AddDocument(change.Entity.ToDocument());
                         break;

+ 9 - 1
Masuit.LuceneEFCore.SearchEngine/LuceneIndexerOptions.cs

@@ -1,4 +1,6 @@
-namespace Masuit.LuceneEFCore.SearchEngine
+using System;
+
+namespace Masuit.LuceneEFCore.SearchEngine
 {
     /// <summary>
     /// 索引器选项
@@ -9,5 +11,11 @@
         /// 索引路径
         /// </summary>
         public string Path { get; set; }
+
+        /// <summary>
+        /// 索引列IndexId的生成函数,(Type EntityType, any IdValue) => string IndexId
+        /// </summary>
+        public static Func<Type, object, string> IndexIdGenerator = (type, id) => $"{type.Name}:{id}";
+
     }
 }