Prechádzať zdrojové kódy

优化结果集去重

懒得勤快 1 rok pred
rodič
commit
28338e94b1

+ 86 - 63
Masuit.LuceneEFCore.SearchEngine/Extensions/DocumentExtension.cs

@@ -1,79 +1,102 @@
 using Lucene.Net.Documents;
 using Lucene.Net.Documents;
 using Newtonsoft.Json;
 using Newtonsoft.Json;
 using System;
 using System;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Globalization;
 using System.Globalization;
+using System.Linq;
 
 
 namespace Masuit.LuceneEFCore.SearchEngine.Extensions
 namespace Masuit.LuceneEFCore.SearchEngine.Extensions
 {
 {
-    public static class DocumentExtension
-    {
-        /// <summary>
-        /// 获取文档的值
-        /// </summary>
-        /// <param name="doc">Lucene文档</param>
-        /// <param name="key">键</param>
-        /// <param name="t">类型</param>
-        /// <returns></returns>
-        internal static object Get(this Document doc, string key, Type t)
-        {
-            string value = doc.Get(key);
-            return t switch
-            {
-                _ when t.IsAssignableFrom(typeof(string)) => value,
-                _ when t.IsValueType => ConvertTo(value, t),
-                _ => JsonConvert.DeserializeObject(value, t)
-            };
-        }
+	public static class DocumentExtension
+	{
+		/// <summary>
+		/// 获取文档的值
+		/// </summary>
+		/// <param name="doc">Lucene文档</param>
+		/// <param name="key">键</param>
+		/// <param name="t">类型</param>
+		/// <returns></returns>
+		internal static object Get(this Document doc, string key, Type t)
+		{
+			string value = doc.Get(key);
+			return t switch
+			{
+				_ when t.IsAssignableFrom(typeof(string)) => value,
+				_ when t.IsValueType => ConvertTo(value, t),
+				_ => JsonConvert.DeserializeObject(value, t)
+			};
+		}
 
 
-        /// <summary>
-        /// 类型直转
-        /// </summary>
-        /// <param name="value"></param>
-        /// <param name="type">目标类型</param>
-        /// <returns></returns>
-        private static object ConvertTo(string value, Type type)
-        {
-            if (value == null)
-            {
-                return default;
-            }
+		/// <summary>
+		/// 类型直转
+		/// </summary>
+		/// <param name="value"></param>
+		/// <param name="type">目标类型</param>
+		/// <returns></returns>
+		private static object ConvertTo(string value, Type type)
+		{
+			if (value == null)
+			{
+				return default;
+			}
 
 
-            if (value.GetType() == type)
-            {
-                return value;
-            }
+			if (value.GetType() == type)
+			{
+				return value;
+			}
 
 
-            if (type.IsEnum)
-            {
-                return Enum.Parse(type, value.ToString(CultureInfo.InvariantCulture));
-            }
+			if (type.IsEnum)
+			{
+				return Enum.Parse(type, value.ToString(CultureInfo.InvariantCulture));
+			}
 
 
-            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
-            {
-                var underlyingType = Nullable.GetUnderlyingType(type);
-                return underlyingType!.IsEnum ? Enum.Parse(underlyingType, value.ToString(CultureInfo.CurrentCulture)) : Convert.ChangeType(value, underlyingType);
-            }
+			if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
+			{
+				var underlyingType = Nullable.GetUnderlyingType(type);
+				return underlyingType!.IsEnum ? Enum.Parse(underlyingType, value.ToString(CultureInfo.CurrentCulture)) : Convert.ChangeType(value, underlyingType);
+			}
 
 
-            var converter = TypeDescriptor.GetConverter(value);
-            if (converter != null)
-            {
-                if (converter.CanConvertTo(type))
-                {
-                    return converter.ConvertTo(value, type);
-                }
-            }
+			var converter = TypeDescriptor.GetConverter(value);
+			if (converter != null)
+			{
+				if (converter.CanConvertTo(type))
+				{
+					return converter.ConvertTo(value, type);
+				}
+			}
 
 
-            converter = TypeDescriptor.GetConverter(type);
-            if (converter != null)
-            {
-                if (converter.CanConvertFrom(value.GetType()))
-                {
-                    return converter.ConvertFrom(value);
-                }
-            }
+			converter = TypeDescriptor.GetConverter(type);
+			if (converter != null)
+			{
+				if (converter.CanConvertFrom(value.GetType()))
+				{
+					return converter.ConvertFrom(value);
+				}
+			}
 
 
-            return Convert.ChangeType(value, type);
-        }
-    }
+			return Convert.ChangeType(value, type);
+		}
+
+#if NET6_0_OR_GREATER
+#else
+
+		/// <summary>
+		/// 按字段去重
+		/// </summary>
+		/// <typeparam name="TSource"></typeparam>
+		/// <typeparam name="TKey"></typeparam>
+		/// <param name="source"></param>
+		/// <param name="keySelector"></param>
+		/// <returns></returns>
+		public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
+		{
+			if (source == null) throw new ArgumentNullException(nameof(source));
+			if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
+			var set = new HashSet<TKey>();
+			return source.Where(item => set.Add(keySelector(item)));
+		}
+
+#endif
+	}
 }
 }

+ 2 - 2
Masuit.LuceneEFCore.SearchEngine/LuceneIndexSearcher.cs

@@ -174,7 +174,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
 				where = where.And(m => options.Type.AssemblyQualifiedName == searcher.Doc(m.Doc).Get("Type"));
 				where = where.And(m => options.Type.AssemblyQualifiedName == searcher.Doc(m.Doc).Get("Type"));
 			}
 			}
 
 
-			var matches = searcher.Search(query, options.Filter, options.MaximumNumberOfHits, sort, true, true).ScoreDocs.Where(where.Compile());
+			var matches = searcher.Search(query, options.Filter, options.MaximumNumberOfHits, sort, true, true).ScoreDocs.Where(where.Compile()).DistinctBy(m => searcher.Doc(m.Doc).Get("IndexId"));
 			results.TotalHits = matches.Count();
 			results.TotalHits = matches.Count();
 
 
 			// 分页处理
 			// 分页处理
@@ -256,4 +256,4 @@ namespace Masuit.LuceneEFCore.SearchEngine
 			return ScoredSearch(options);
 			return ScoredSearch(options);
 		}
 		}
 	}
 	}
-}
+}

+ 2 - 2
Masuit.LuceneEFCore.SearchEngine/Masuit.LuceneEFCore.SearchEngine.csproj

@@ -8,8 +8,8 @@
         <Description>基于EntityFrameworkCore和Lucene.NET实现的全文检索搜索引擎</Description>
         <Description>基于EntityFrameworkCore和Lucene.NET实现的全文检索搜索引擎</Description>
         <Copyright>懒得勤快</Copyright>
         <Copyright>懒得勤快</Copyright>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.LuceneEFCore.SearchEngine</PackageProjectUrl>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.LuceneEFCore.SearchEngine</PackageProjectUrl>
-        <PackageId>Masuit.LuceneEFCore.SearchEngine_int</PackageId>
-        <Version>1.2.4</Version>
+        <PackageId>Masuit.LuceneEFCore.SearchEngine_guid</PackageId>
+        <Version>24.1</Version>
         <Configurations>Debug;Release;String版本;Guid版本;Long版本</Configurations>
         <Configurations>Debug;Release;String版本;Guid版本;Long版本</Configurations>
         <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
         <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
         <RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
         <RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>