DynamicSearchService.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. using SqlSugar;
  2. using System.Diagnostics;
  3. using System.DirectoryServices;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. namespace OASystem.API.OAMethodLib.GenericSearch
  7. {
  8. /// <summary>
  9. /// 动态检索服务
  10. /// 支持动态字段权重配置、返回字段筛选、智能搜索等功能
  11. /// </summary>
  12. /// <typeparam name="T">实体类型</typeparam>
  13. public class DynamicSearchService<T> where T : class, new()
  14. {
  15. private readonly SqlSugarClient _db;
  16. private readonly ILogger<DynamicSearchService<T>> _logger;
  17. public DynamicSearchService(SqlSugarClient db, ILogger<DynamicSearchService<T>> logger)
  18. {
  19. _db = db;
  20. _logger = logger;
  21. }
  22. /// <summary>
  23. /// 执行动态搜索(应用层统计匹配度)
  24. /// </summary>
  25. /// <param name="request">搜索请求参数</param>
  26. /// <returns>包含搜索结果和匹配度信息的结果对象</returns>
  27. public async Task<SearchResult<T>> SearchAsync(DynamicSearchRequest request)
  28. {
  29. var resultView = new SearchResult<T>() { Success = false, Message = "异常错误" };
  30. var stopwatch = Stopwatch.StartNew();
  31. var searchId = Guid.NewGuid().ToString("N")[..8];
  32. try
  33. {
  34. List<T> data;
  35. int totalCount;
  36. // 使用原生SQL方式构建查询
  37. if (!string.IsNullOrWhiteSpace(request.Keyword))
  38. {
  39. var result = await SearchWithNativeSqlAsync(request);
  40. data = result.Data;
  41. totalCount = result.TotalCount;
  42. }
  43. else
  44. {
  45. // 无关键词时使用简单查询
  46. var query = BuildBaseQuery(request);
  47. totalCount = await query.CountAsync();
  48. data = await query.ToPageListAsync(request.PageIndex, request.PageSize);
  49. }
  50. // 在应用层计算匹配度
  51. var scoredItems = CalculateMatchScore(data, request);
  52. stopwatch.Stop();
  53. return new SearchResult<T>
  54. {
  55. Message = $"搜索成功!耗时:{stopwatch.ElapsedMilliseconds}ms",
  56. Items = scoredItems,
  57. TotalCount = totalCount,
  58. Keyword = request.Keyword,
  59. FieldWeights = request.FieldWeights,
  60. ReturnFields = request.ReturnFields,
  61. PageIndex = request.PageIndex,
  62. PageSize = request.PageSize,
  63. ResponseTime = stopwatch.ElapsedMilliseconds,
  64. SearchId = searchId
  65. };
  66. }
  67. catch (Exception ex)
  68. {
  69. stopwatch.Stop();
  70. resultView.Message = string.Format("【{SearchId}】动态搜索失败: {ErrorMessage}", searchId, ex.Message);
  71. return resultView;
  72. }
  73. }
  74. /// <summary>
  75. /// 轻量级搜索 - 只返回指定字段,提升性能(应用层统计匹配度)
  76. /// </summary>
  77. /// <typeparam name="TResult">返回的结果类型</typeparam>
  78. /// <param name="request">搜索请求参数</param>
  79. /// <param name="selector">字段选择表达式</param>
  80. /// <returns>包含指定字段和匹配度信息的搜索结果</returns>
  81. public async Task<SearchResult<TResult>> LightweightSearchAsync<TResult>(
  82. DynamicSearchRequest request,
  83. Expression<Func<T, TResult>> selector) where TResult : class, new()
  84. {
  85. var stopwatch = Stopwatch.StartNew();
  86. var searchId = Guid.NewGuid().ToString("N")[..8];
  87. _logger.LogInformation("【{SearchId}】开始轻量级搜索: 实体{Entity}, 返回类型{ResultType}",
  88. searchId, typeof(T).Name, typeof(TResult).Name);
  89. try
  90. {
  91. // 构建基础查询
  92. var baseQuery = _db.Queryable<T>();
  93. // 应用过滤条件
  94. baseQuery = ApplyFilters(baseQuery, request.Filters);
  95. // 应用搜索条件
  96. if (!string.IsNullOrWhiteSpace(request.Keyword))
  97. {
  98. var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  99. if (searchAnalysis.HasSearchContent)
  100. {
  101. var searchFields = request.FieldWeights?.Keys.ToList() ?? GetDefaultSearchFields();
  102. var searchConditions = BuildSearchConditions(searchAnalysis, searchFields);
  103. if (searchConditions.Any())
  104. {
  105. baseQuery = baseQuery.Where(searchConditions);
  106. }
  107. }
  108. }
  109. // 应用字段选择 - 在数据库层面进行字段选择
  110. var finalQuery = baseQuery.Select(selector);
  111. // 应用排序
  112. finalQuery = ApplyOrderByForLightweight(finalQuery, request.OrderBy, request.IsDescending);
  113. // 执行查询获取轻量级数据
  114. var totalCount = await finalQuery.CountAsync();
  115. var lightweightData = await finalQuery.ToPageListAsync(request.PageIndex, request.PageSize);
  116. // 为了计算匹配度,需要查询完整的实体数据
  117. List<T> fullDataForScoring;
  118. if (!string.IsNullOrWhiteSpace(request.Keyword))
  119. {
  120. var fullResult = await SearchWithNativeSqlAsync(request);
  121. fullDataForScoring = fullResult.Data;
  122. }
  123. else
  124. {
  125. var fullQuery = BuildBaseQuery(request);
  126. fullDataForScoring = await fullQuery.ToPageListAsync(request.PageIndex, request.PageSize);
  127. }
  128. // 计算匹配度
  129. var scoredItems = CalculateMatchScore(fullDataForScoring, request);
  130. // 将匹配度信息与轻量级数据关联
  131. var lightweightItems = AssociateMatchScores(lightweightData, scoredItems, selector);
  132. stopwatch.Stop();
  133. _logger.LogInformation("【{SearchId}】轻量级搜索完成: 找到 {Count} 条记录, 耗时 {TotalTime}ms",
  134. searchId, lightweightItems.Count, stopwatch.ElapsedMilliseconds);
  135. return new SearchResult<TResult>
  136. {
  137. Items = lightweightItems,
  138. TotalCount = totalCount,
  139. Keyword = request.Keyword,
  140. FieldWeights = request.FieldWeights,
  141. PageIndex = request.PageIndex,
  142. PageSize = request.PageSize,
  143. ResponseTime = stopwatch.ElapsedMilliseconds,
  144. SearchId = searchId
  145. };
  146. }
  147. catch (Exception ex)
  148. {
  149. stopwatch.Stop();
  150. _logger.LogError(ex, "【{SearchId}】轻量级搜索失败", searchId);
  151. throw;
  152. }
  153. }
  154. /// <summary>
  155. /// 将匹配度信息与轻量级数据关联
  156. /// </summary>
  157. private List<SearchResultItem<TResult>> AssociateMatchScores<TResult>(
  158. List<TResult> lightweightData,
  159. List<SearchResultItem<T>> scoredItems,
  160. Expression<Func<T, TResult>> selector) where TResult : class, new()
  161. {
  162. var result = new List<SearchResultItem<TResult>>();
  163. // 构建一个字典来快速查找匹配度信息
  164. var scoreDict = new Dictionary<int, SearchResultItem<T>>();
  165. foreach (var scoredItem in scoredItems)
  166. {
  167. var id = GetEntityId(scoredItem.Data);
  168. if (id > 0)
  169. {
  170. scoreDict[id] = scoredItem;
  171. }
  172. }
  173. // 关联匹配度信息
  174. foreach (var lightItem in lightweightData)
  175. {
  176. var id = GetEntityId(lightItem);
  177. if (id > 0 && scoreDict.TryGetValue(id, out var scoredItem))
  178. {
  179. result.Add(new SearchResultItem<TResult>
  180. {
  181. Data = lightItem,
  182. MatchScore = scoredItem.MatchScore,
  183. MatchFields = scoredItem.MatchFields
  184. });
  185. }
  186. else
  187. {
  188. result.Add(new SearchResultItem<TResult>
  189. {
  190. Data = lightItem,
  191. MatchScore = 0,
  192. MatchFields = new List<MatchFieldInfo>()
  193. });
  194. }
  195. }
  196. return result.OrderByDescending(x => x.MatchScore).ToList();
  197. }
  198. /// <summary>
  199. /// 获取实体ID(通过反射)
  200. /// </summary>
  201. private int GetEntityId<TEntity>(TEntity entity)
  202. {
  203. if (entity == null) return 0;
  204. var idProperty = typeof(TEntity).GetProperty("Id");
  205. if (idProperty != null && idProperty.PropertyType == typeof(int))
  206. {
  207. return (int)(idProperty.GetValue(entity) ?? 0);
  208. }
  209. return 0;
  210. }
  211. /// <summary>
  212. /// 获取实体可搜索字段信息
  213. /// </summary>
  214. /// <returns>可搜索字段信息列表,按权重降序排列</returns>
  215. public List<FieldInfo> GetSearchableFields()
  216. {
  217. var entityType = typeof(T);
  218. var properties = entityType.GetProperties();
  219. var searchableFields = new List<FieldInfo>();
  220. foreach (var prop in properties)
  221. {
  222. var fieldInfo = new FieldInfo
  223. {
  224. FieldName = prop.Name,
  225. DisplayName = GetDisplayName(prop),
  226. DataType = prop.PropertyType.Name,
  227. IsSearchable = prop.PropertyType == typeof(string),
  228. DefaultWeight = GetDefaultWeight(prop.Name),
  229. Description = GetFieldDescription(prop),
  230. CanFilter = true,
  231. CanSort = true
  232. };
  233. searchableFields.Add(fieldInfo);
  234. }
  235. return searchableFields
  236. .OrderByDescending(f => f.DefaultWeight)
  237. .ThenBy(f => f.FieldName)
  238. .ToList();
  239. }
  240. /// <summary>
  241. /// 验证字段配置
  242. /// </summary>
  243. /// <param name="fieldWeights">字段权重配置</param>
  244. /// <param name="returnFields">返回字段列表</param>
  245. /// <returns>验证结果</returns>
  246. public (bool IsValid, string Message) ValidateFieldConfig(
  247. Dictionary<string, int> fieldWeights,
  248. List<string> returnFields)
  249. {
  250. var allFields = GetSearchableFields();
  251. var validFieldNames = allFields.Select(f => f.FieldName).ToList();
  252. // 验证搜索字段
  253. if (fieldWeights != null)
  254. {
  255. var invalidSearchFields = fieldWeights.Keys.Except(validFieldNames).ToList();
  256. if (invalidSearchFields.Any())
  257. {
  258. return (false, $"无效的搜索字段: {string.Join(", ", invalidSearchFields)}");
  259. }
  260. }
  261. // 验证返回字段
  262. if (returnFields != null)
  263. {
  264. var invalidReturnFields = returnFields.Except(validFieldNames).ToList();
  265. if (invalidReturnFields.Any())
  266. {
  267. return (false, $"无效的返回字段: {string.Join(", ", invalidReturnFields)}");
  268. }
  269. }
  270. return (true, "字段配置有效");
  271. }
  272. #region 私有方法 - 搜索逻辑
  273. /// <summary>
  274. /// 使用原生SQL进行搜索
  275. /// </summary>
  276. private async Task<(List<T> Data, int TotalCount)> SearchWithNativeSqlAsync(DynamicSearchRequest request)
  277. {
  278. var whereConditions = new List<string>();
  279. var parameters = new List<SugarParameter>();
  280. // 获取搜索字段
  281. var searchFields = request.FieldWeights?.Keys.ToList() ?? GetDefaultSearchFields();
  282. var validFields = ValidateSearchFields(searchFields);
  283. // 构建搜索条件
  284. if (!string.IsNullOrWhiteSpace(request.Keyword))
  285. {
  286. #region and 构建
  287. var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  288. var keywordConditions = new List<string>(); // 专门存放关键字相关条件
  289. // 符号分割的关键字条件
  290. foreach (var segment in searchAnalysis.SymbolSegments)
  291. {
  292. var cleanSegment = Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  293. if (!string.IsNullOrEmpty(cleanSegment))
  294. {
  295. var fieldConditions = validFields.Select(field =>
  296. {
  297. var paramName = $"@segment{parameters.Count}";
  298. parameters.Add(new SugarParameter(paramName, $"%{cleanSegment}%"));
  299. return $"{field} LIKE {paramName}";
  300. });
  301. // 每个片段内部使用 OR 连接不同字段
  302. keywordConditions.Add($"({string.Join(" OR ", fieldConditions)})");
  303. }
  304. }
  305. // 单字检索条件
  306. foreach (var singleChar in searchAnalysis.SingleChars)
  307. {
  308. var charStr = singleChar.ToString();
  309. var fieldConditions = validFields.Select(field =>
  310. {
  311. var paramName = $"@char{parameters.Count}";
  312. parameters.Add(new SugarParameter(paramName, $"%{charStr}%"));
  313. return $"{field} LIKE {paramName}";
  314. });
  315. // 每个单字内部使用 OR 连接不同字段
  316. keywordConditions.Add($"({string.Join(" OR ", fieldConditions)})");
  317. }
  318. // 所有关键字条件使用 OR 连接
  319. if (keywordConditions.Any())
  320. {
  321. whereConditions.Add($"({string.Join(" OR ", keywordConditions)})");
  322. }
  323. #endregion
  324. #region or 构建
  325. //var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  326. //var keywordConditions = new List<string>();
  327. //// 统一处理所有搜索词
  328. //var allSearchTerms = new List<string>();
  329. //// 添加符号分割的片段(去除特殊字符)
  330. //allSearchTerms.AddRange(searchAnalysis.SymbolSegments
  331. // .Select(segment => Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", ""))
  332. // .Where(segment => !string.IsNullOrEmpty(segment)));
  333. //// 添加单字(排除重复)
  334. //foreach (var singleChar in searchAnalysis.SingleChars)
  335. //{
  336. // var charStr = singleChar.ToString();
  337. // // 只有当单字不在任何符号片段中时才添加
  338. // if (!allSearchTerms.Any(term => term.Contains(charStr)))
  339. // {
  340. // allSearchTerms.Add(charStr);
  341. // }
  342. //}
  343. //// 处理每个搜索词
  344. //foreach (var term in allSearchTerms.Distinct())
  345. //{
  346. // var fieldConditions = validFields.Select(field =>
  347. // {
  348. // var paramName = $"@term{parameters.Count}";
  349. // parameters.Add(new SugarParameter(paramName, $"%{term}%"));
  350. // return $"{field} LIKE {paramName}";
  351. // });
  352. // keywordConditions.Add($"({string.Join(" OR ", fieldConditions)})");
  353. //}
  354. //// 所有搜索条件使用 AND 连接
  355. //if (keywordConditions.Any())
  356. //{
  357. // whereConditions.Add($"({string.Join(" AND ", keywordConditions)})");
  358. //}
  359. #endregion
  360. }
  361. // 构建过滤条件
  362. var filterConditions = BuildNativeFilterConditions(request.Filters, parameters);
  363. whereConditions.AddRange(filterConditions);
  364. // 构建完整SQL
  365. var whereClause = whereConditions.Any()
  366. ? "WHERE " + string.Join(" AND ", whereConditions)
  367. : "";
  368. var orderByClause = BuildNativeOrderByClause(request.OrderBy, request.IsDescending);
  369. var tableName = _db.EntityMaintenance.GetTableName(typeof(T));
  370. // 构建返回字段
  371. //var returnFields = BuildReturnFields(request.ReturnFields);
  372. // 先查询总数
  373. var countSql = $"SELECT COUNT(1) FROM {tableName} {whereClause}";
  374. var totalCount = await _db.Ado.GetIntAsync(countSql, parameters);
  375. // 再查询数据
  376. var offset = (request.PageIndex - 1) * request.PageSize;
  377. var dataSql = $@"
  378. SELECT * FROM (
  379. SELECT *, ROW_NUMBER() OVER ({orderByClause}) AS RowNumber
  380. FROM {tableName}
  381. {whereClause}
  382. ) AS Paginated
  383. WHERE Paginated.RowNumber > {offset} AND Paginated.RowNumber <= {offset + request.PageSize}
  384. {orderByClause}";
  385. var data = await _db.Ado.SqlQueryAsync<T>(dataSql, parameters);
  386. return (data, totalCount);
  387. }
  388. /// <summary>
  389. /// 构建基础查询
  390. /// </summary>
  391. private ISugarQueryable<T> BuildBaseQuery(DynamicSearchRequest request)
  392. {
  393. var query = _db.Queryable<T>();
  394. // 应用过滤条件
  395. query = ApplyFilters(query, request.Filters);
  396. // 应用排序
  397. query = ApplyOrderBy(query, request.OrderBy, request.IsDescending);
  398. return query;
  399. }
  400. /// <summary>
  401. /// 构建原生过滤条件
  402. /// </summary>
  403. private List<string> BuildNativeFilterConditions(List<SearchFilter> filters, List<SugarParameter> parameters)
  404. {
  405. var conditions = new List<string>();
  406. if (filters == null) return conditions;
  407. foreach (var filter in filters)
  408. {
  409. var condition = filter.Operator?.ToLower() switch
  410. {
  411. "eq" => BuildNativeCondition(filter, "=", parameters),
  412. "neq" => BuildNativeCondition(filter, "!=", parameters),
  413. "contains" => BuildNativeLikeCondition(filter, "%", "%", parameters),
  414. "startswith" => BuildNativeLikeCondition(filter, "", "%", parameters),
  415. "endswith" => BuildNativeLikeCondition(filter, "%", "", parameters),
  416. "gt" => BuildNativeCondition(filter, ">", parameters),
  417. "gte" => BuildNativeCondition(filter, ">=", parameters),
  418. "lt" => BuildNativeCondition(filter, "<", parameters),
  419. "lte" => BuildNativeCondition(filter, "<=", parameters),
  420. "in" => BuildNativeInCondition(filter, parameters),
  421. _ => null
  422. };
  423. if (!string.IsNullOrEmpty(condition))
  424. {
  425. conditions.Add(condition);
  426. }
  427. }
  428. return conditions;
  429. }
  430. private string BuildNativeCondition(SearchFilter filter, string op, List<SugarParameter> parameters)
  431. {
  432. var paramName = $"@filter{parameters.Count}";
  433. parameters.Add(new SugarParameter(paramName, filter.Value));
  434. return $"{filter.Field} {op} {paramName}";
  435. }
  436. private string BuildNativeLikeCondition(SearchFilter filter, string prefix, string suffix, List<SugarParameter> parameters)
  437. {
  438. var paramName = $"@filter{parameters.Count}";
  439. parameters.Add(new SugarParameter(paramName, $"{prefix}{filter.Value}{suffix}"));
  440. return $"{filter.Field} LIKE {paramName}";
  441. }
  442. private string BuildNativeInCondition(SearchFilter filter, List<SugarParameter> parameters)
  443. {
  444. if (filter.Values == null || !filter.Values.Any())
  445. return null;
  446. var paramNames = new List<string>();
  447. foreach (var value in filter.Values)
  448. {
  449. var paramName = $"@filter{parameters.Count}";
  450. parameters.Add(new SugarParameter(paramName, value));
  451. paramNames.Add(paramName);
  452. }
  453. return $"{filter.Field} IN ({string.Join(",", paramNames)})";
  454. }
  455. #endregion
  456. #region 私有方法 - 匹配度计算含出现位置权重(单字检索时全部出现)
  457. /// <summary>
  458. /// 在应用层计算匹配度
  459. /// </summary>
  460. private List<SearchResultItem<T>> CalculateMatchScore(List<T> data, DynamicSearchRequest request)
  461. {
  462. if (string.IsNullOrWhiteSpace(request.Keyword))
  463. {
  464. // 无关键词时,所有记录匹配度为0
  465. return data.Select(item => new SearchResultItem<T>
  466. {
  467. Data = item,
  468. MatchScore = 0
  469. }).ToList();
  470. }
  471. var searchAnalysis = AnalyzeSearchPattern(request.Keyword);
  472. var searchFields = request.FieldWeights?.Keys.ToList() ?? GetDefaultSearchFields();
  473. var fieldWeights = request.FieldWeights ?? GetDefaultFieldWeights(searchFields);
  474. var scoredItems = data.Select(item =>
  475. {
  476. var matchResult = CalculateItemMatchScore(item, searchAnalysis, searchFields, fieldWeights, request.RequireAllSingleChars);
  477. return new SearchResultItem<T>
  478. {
  479. Data = item,
  480. MatchScore = matchResult.TotalScore,
  481. MatchFields = matchResult.MatchFields
  482. };
  483. })
  484. .Where(item => item.MatchScore > 0) // 只保留有匹配的记录
  485. .OrderByDescending(item => item.MatchScore)
  486. .ThenByDescending(item => GetCreateTime(item.Data))
  487. .ToList();
  488. return scoredItems;
  489. }
  490. /// <summary>
  491. /// 计算单个项的匹配度详情
  492. /// </summary>
  493. private (int TotalScore, List<MatchFieldInfo> MatchFields) CalculateItemMatchScore(
  494. T item,
  495. SearchAnalysis analysis,
  496. List<string> searchFields,
  497. Dictionary<string, int> fieldWeights,
  498. bool requireAllSingleChars)
  499. {
  500. int totalScore = 0;
  501. var matchFields = new List<MatchFieldInfo>();
  502. // 新增:根据参数检查是否所有单字都出现
  503. if (requireAllSingleChars && analysis.SingleChars.Any())
  504. {
  505. bool allSingleCharsExist = CheckAllSingleCharsExist(item, analysis.SingleChars, searchFields);
  506. // 如果要求所有单字必须出现但未完全匹配,直接返回0分
  507. if (!allSingleCharsExist)
  508. {
  509. return (0, matchFields);
  510. }
  511. }
  512. foreach (var field in searchFields)
  513. {
  514. var fieldValue = GetFieldValue(item, field);
  515. if (string.IsNullOrEmpty(fieldValue))
  516. continue;
  517. var weight = fieldWeights.ContainsKey(field) ? fieldWeights[field] : GetDefaultWeight(field);
  518. int fieldScore = 0;
  519. var fieldMatchReasons = new List<string>();
  520. // 符号分割关键字匹配
  521. foreach (var segment in analysis.SymbolSegments)
  522. {
  523. var cleanSegment = Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  524. if (!string.IsNullOrEmpty(cleanSegment) && fieldValue.Contains(cleanSegment))
  525. {
  526. int segmentScore = weight;
  527. if (fieldValue.Equals(cleanSegment))
  528. {
  529. segmentScore += 15;
  530. fieldMatchReasons.Add($"完全匹配 '{cleanSegment}'");
  531. }
  532. else if (fieldValue.StartsWith(cleanSegment))
  533. {
  534. segmentScore += 10;
  535. fieldMatchReasons.Add($"开头匹配 '{cleanSegment}'");
  536. }
  537. else if (fieldValue.EndsWith(cleanSegment))
  538. {
  539. segmentScore += 5;
  540. fieldMatchReasons.Add($"结尾匹配 '{cleanSegment}'");
  541. }
  542. else
  543. {
  544. fieldMatchReasons.Add($"包含 '{cleanSegment}'");
  545. }
  546. fieldScore += segmentScore;
  547. }
  548. }
  549. // 单字匹配 - 加入位置权重计算
  550. foreach (var singleChar in analysis.SingleChars)
  551. {
  552. int count = fieldValue.Count(c => c == singleChar);
  553. if (count > 0)
  554. {
  555. int charScore = count * (int)(weight * 0.3);
  556. // 计算位置权重奖励
  557. var positionBonus = CalculateSingleCharPositionBonus(fieldValue, singleChar, weight);
  558. charScore += positionBonus.Bonus;
  559. if (fieldValue.StartsWith(singleChar.ToString()))
  560. {
  561. charScore += weight;
  562. fieldMatchReasons.Add($"开头单字 '{singleChar}' +{weight}");
  563. }
  564. else if (positionBonus.Bonus > 0)
  565. {
  566. fieldMatchReasons.Add($"靠前单字 '{singleChar}' +{positionBonus.Bonus}");
  567. }
  568. // 添加位置信息到原因
  569. if (positionBonus.FirstPosition >= 0)
  570. {
  571. fieldMatchReasons.Add($"位置{positionBonus.FirstPosition + 1}");
  572. }
  573. fieldMatchReasons.Add($"包含单字 '{singleChar}'({count}次)");
  574. fieldScore += charScore;
  575. }
  576. }
  577. if (fieldScore > 0)
  578. {
  579. totalScore += fieldScore;
  580. matchFields.Add(new MatchFieldInfo
  581. {
  582. FieldName = field,
  583. FieldValue = GetDisplayFieldValue(fieldValue),
  584. Score = fieldScore,
  585. MatchReason = string.Join("; ", fieldMatchReasons)
  586. });
  587. }
  588. }
  589. // 按分数排序匹配字段
  590. matchFields = matchFields.OrderByDescending(m => m.Score).ToList();
  591. return (totalScore, matchFields);
  592. }
  593. /// <summary>
  594. /// 新增:检查所有单字是否在任意字段中出现
  595. /// </summary>
  596. private bool CheckAllSingleCharsExist(T item, List<char> singleChars, List<string> searchFields)
  597. {
  598. foreach (var singleChar in singleChars)
  599. {
  600. bool charExists = false;
  601. // 检查所有搜索字段中是否包含该单字
  602. foreach (var field in searchFields)
  603. {
  604. var fieldValue = GetFieldValue(item, field);
  605. if (!string.IsNullOrEmpty(fieldValue) && fieldValue.Contains(singleChar))
  606. {
  607. charExists = true;
  608. break;
  609. }
  610. }
  611. // 如果有一个单字不存在,直接返回false
  612. if (!charExists)
  613. {
  614. return false;
  615. }
  616. }
  617. return true;
  618. }
  619. /// <summary>
  620. /// 计算单字位置权重奖励
  621. /// </summary>
  622. private (int Bonus, int FirstPosition) CalculateSingleCharPositionBonus(string fieldValue, char singleChar, int baseWeight)
  623. {
  624. var firstPosition = fieldValue.IndexOf(singleChar);
  625. if (firstPosition == -1)
  626. return (0, -1);
  627. // 计算位置比例 (0-1),0表示开头,1表示结尾
  628. double positionRatio = (double)firstPosition / Math.Max(fieldValue.Length - 1, 1);
  629. // 位置权重系数:位置越靠前,奖励越高
  630. double positionFactor = 1.0 - positionRatio;
  631. // 计算奖励分数(基于基础权重)
  632. int positionBonus = (int)(baseWeight * positionFactor * 0.8); // 最大奖励为基础权重的80%
  633. // 确保奖励至少为1
  634. positionBonus = Math.Max(positionBonus, 1);
  635. return (positionBonus, firstPosition);
  636. }
  637. /// <summary>
  638. /// 获取显示字段值(截断过长的值)
  639. /// </summary>
  640. private string GetDisplayFieldValue(string fieldValue, int maxLength = 50)
  641. {
  642. if (string.IsNullOrEmpty(fieldValue) || fieldValue.Length <= maxLength)
  643. return fieldValue;
  644. return fieldValue.Substring(0, maxLength) + "...";
  645. }
  646. #endregion
  647. #region 私有方法 - 辅助功能
  648. /// <summary>
  649. /// 应用过滤条件
  650. /// </summary>
  651. private ISugarQueryable<T> ApplyFilters(ISugarQueryable<T> query, List<SearchFilter> filters)
  652. {
  653. if (filters == null || !filters.Any())
  654. return query;
  655. foreach (var filter in filters)
  656. {
  657. query = filter.Operator?.ToLower() switch
  658. {
  659. "eq" => query.Where($"{filter.Field} = @Value", new { filter.Value }),
  660. "neq" => query.Where($"{filter.Field} != @Value", new { filter.Value }),
  661. "contains" => query.Where($"{filter.Field} LIKE '%' + @Value + '%'", new { filter.Value }),
  662. "startswith" => query.Where($"{filter.Field} LIKE @Value + '%'", new { filter.Value }),
  663. "endswith" => query.Where($"{filter.Field} LIKE '%' + @Value", new { filter.Value }),
  664. "gt" => query.Where($"{filter.Field} > @Value", new { filter.Value }),
  665. "gte" => query.Where($"{filter.Field} >= @Value", new { filter.Value }),
  666. "lt" => query.Where($"{filter.Field} < @Value", new { filter.Value }),
  667. "lte" => query.Where($"{filter.Field} <= @Value", new { filter.Value }),
  668. "in" => ApplyInFilter(query, filter),
  669. _ => query
  670. };
  671. }
  672. return query;
  673. }
  674. /// <summary>
  675. /// 使用SqlSugar条件构建器构建搜索条件
  676. /// </summary>
  677. private List<IConditionalModel> BuildSearchConditions(SearchAnalysis analysis, List<string> searchFields)
  678. {
  679. var conditionalModels = new List<IConditionalModel>();
  680. // 获取有效的搜索字段
  681. var validFields = ValidateSearchFields(searchFields);
  682. if (!validFields.Any())
  683. return conditionalModels;
  684. // 1. 符号分割的关键字条件
  685. foreach (var segment in analysis.SymbolSegments)
  686. {
  687. var cleanSegment = Regex.Replace(segment, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  688. if (!string.IsNullOrEmpty(cleanSegment))
  689. {
  690. var segmentGroup = new List<IConditionalModel>();
  691. foreach (var field in validFields)
  692. {
  693. segmentGroup.Add(new ConditionalModel
  694. {
  695. FieldName = field,
  696. ConditionalType = ConditionalType.Like,
  697. FieldValue = $"%{cleanSegment}%"
  698. });
  699. }
  700. if (segmentGroup.Count > 1)
  701. {
  702. conditionalModels.Add(new ConditionalCollections
  703. {
  704. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>(
  705. segmentGroup.Select((model, index) =>
  706. new KeyValuePair<WhereType, ConditionalModel>(
  707. index == 0 ? WhereType.And : WhereType.Or,
  708. (ConditionalModel)model))
  709. )
  710. });
  711. }
  712. else if (segmentGroup.Count == 1)
  713. {
  714. conditionalModels.Add(segmentGroup[0]);
  715. }
  716. }
  717. }
  718. return conditionalModels;
  719. }
  720. /// <summary>
  721. /// 应用IN过滤条件
  722. /// </summary>
  723. private ISugarQueryable<T> ApplyInFilter(ISugarQueryable<T> query, SearchFilter filter)
  724. {
  725. if (filter.Values == null || !filter.Values.Any())
  726. return query;
  727. var valueList = string.Join(",", filter.Values.Select(v => $"'{v}'"));
  728. return query.Where($"{filter.Field} IN ({valueList})");
  729. }
  730. /// <summary>
  731. /// 应用排序
  732. /// </summary>
  733. private ISugarQueryable<T> ApplyOrderBy(ISugarQueryable<T> query, string orderBy, bool isDescending)
  734. {
  735. if (string.IsNullOrWhiteSpace(orderBy))
  736. {
  737. // 默认按主键或创建时间排序
  738. var entityType = typeof(T);
  739. var idProperty = entityType.GetProperty("Id") ?? entityType.GetProperty("CreateTime");
  740. if (idProperty != null)
  741. {
  742. orderBy = idProperty.Name;
  743. }
  744. }
  745. if (!string.IsNullOrWhiteSpace(orderBy))
  746. {
  747. return isDescending
  748. ? query.OrderBy($"{orderBy} DESC")
  749. : query.OrderBy($"{orderBy} ASC");
  750. }
  751. return query;
  752. }
  753. /// <summary>
  754. /// 为轻量级搜索应用排序
  755. /// </summary>
  756. private ISugarQueryable<TResult> ApplyOrderByForLightweight<TResult>(
  757. ISugarQueryable<TResult> query,
  758. string orderBy,
  759. bool isDescending) where TResult : class, new()
  760. {
  761. if (string.IsNullOrWhiteSpace(orderBy))
  762. {
  763. // 检查结果类型是否有Id或CreateTime字段
  764. var resultType = typeof(TResult);
  765. var idProperty = resultType.GetProperty("Id") ?? resultType.GetProperty("CreateTime");
  766. if (idProperty != null)
  767. {
  768. orderBy = idProperty.Name;
  769. }
  770. else
  771. {
  772. // 如果没有默认排序字段,返回原查询
  773. return query;
  774. }
  775. }
  776. if (!string.IsNullOrWhiteSpace(orderBy))
  777. {
  778. // 验证排序字段是否存在于结果类型中
  779. var resultType = typeof(TResult);
  780. var orderByProperty = resultType.GetProperty(orderBy);
  781. if (orderByProperty != null)
  782. {
  783. return isDescending
  784. ? query.OrderBy($"{orderBy} DESC")
  785. : query.OrderBy($"{orderBy} ASC");
  786. }
  787. else
  788. {
  789. _logger.LogWarning("排序字段 {OrderBy} 在返回类型 {ResultType} 中不存在", orderBy, resultType.Name);
  790. }
  791. }
  792. return query;
  793. }
  794. /// <summary>
  795. /// 构建原生排序子句
  796. /// </summary>
  797. private string BuildNativeOrderByClause(string orderBy, bool isDescending)
  798. {
  799. if (string.IsNullOrWhiteSpace(orderBy))
  800. //return "ORDER BY Id DESC";
  801. return "";
  802. return $"ORDER BY {orderBy} {(isDescending ? "DESC" : "ASC")}";
  803. }
  804. /// <summary>
  805. /// 分析搜索模式
  806. /// </summary>
  807. private SearchAnalysis AnalyzeSearchPattern(string keyword)
  808. {
  809. var analysis = new SearchAnalysis { OriginalKeyword = keyword };
  810. // 检查是否包含特殊符号
  811. var specialSymbols = new[] { ' ', ',', ',', ';', ';', '、', '/', '\\', '|', '-', '_' };
  812. if (keyword.Any(c => specialSymbols.Contains(c)))
  813. {
  814. analysis.HasSpecialSymbols = true;
  815. analysis.SymbolSegments = keyword.Split(specialSymbols, StringSplitOptions.RemoveEmptyEntries)
  816. .Select(s => s.Trim())
  817. .Where(s => !string.IsNullOrEmpty(s))
  818. .ToList();
  819. }
  820. // 清理关键词并提取单字
  821. var cleanKeyword = Regex.Replace(keyword, @"[^\u4e00-\u9fa5a-zA-Z0-9]", "");
  822. if (!string.IsNullOrEmpty(cleanKeyword))
  823. {
  824. foreach (char c in cleanKeyword)
  825. {
  826. analysis.SingleChars.Add(c);
  827. }
  828. analysis.IsSingleChar = true;
  829. // 如果没有特殊符号但有关键词,也作为符号分割段
  830. if (cleanKeyword.Length > 1 && !analysis.HasSpecialSymbols)
  831. {
  832. analysis.SymbolSegments.Add(cleanKeyword);
  833. }
  834. }
  835. // 去重
  836. analysis.SingleChars = analysis.SingleChars.Distinct().ToList();
  837. analysis.SymbolSegments = analysis.SymbolSegments.Distinct().ToList();
  838. return analysis;
  839. }
  840. /// <summary>
  841. /// 获取创建时间(用于排序)
  842. /// </summary>
  843. private DateTime GetCreateTime(T item)
  844. {
  845. var createTimeProperty = typeof(T).GetProperty("CreateTime");
  846. if (createTimeProperty != null)
  847. {
  848. return (DateTime)(createTimeProperty.GetValue(item) ?? DateTime.MinValue);
  849. }
  850. return DateTime.MinValue;
  851. }
  852. /// <summary>
  853. /// 获取字段值
  854. /// </summary>
  855. private string GetFieldValue(T item, string fieldName)
  856. {
  857. var property = typeof(T).GetProperty(fieldName);
  858. return property?.GetValue(item) as string;
  859. }
  860. /// <summary>
  861. /// 获取默认搜索字段
  862. /// </summary>
  863. private List<string> GetDefaultSearchFields()
  864. {
  865. return typeof(T).GetProperties()
  866. .Where(p => p.PropertyType == typeof(string))
  867. .Select(p => p.Name)
  868. .Take(5)
  869. .ToList();
  870. }
  871. /// <summary>
  872. /// 获取默认字段权重
  873. /// </summary>
  874. private Dictionary<string, int> GetDefaultFieldWeights(List<string> fields)
  875. {
  876. var weights = new Dictionary<string, int>();
  877. foreach (var field in fields)
  878. {
  879. weights[field] = GetDefaultWeight(field);
  880. }
  881. return weights;
  882. }
  883. /// <summary>
  884. /// 验证搜索字段
  885. /// </summary>
  886. private List<string> ValidateSearchFields(List<string> searchFields)
  887. {
  888. var validFields = typeof(T).GetProperties()
  889. .Where(p => p.PropertyType == typeof(string))
  890. .Select(p => p.Name)
  891. .ToList();
  892. return searchFields.Intersect(validFields).ToList();
  893. }
  894. /// <summary>
  895. /// 获取默认权重
  896. /// </summary>
  897. private int GetDefaultWeight(string fieldName)
  898. {
  899. return fieldName.ToLower() switch
  900. {
  901. var name when name.Contains("name") => 10,
  902. var name when name.Contains("title") => 10,
  903. var name when name.Contains("code") => 8,
  904. var name when name.Contains("no") => 8,
  905. var name when name.Contains("desc") => 6,
  906. var name when name.Contains("content") => 6,
  907. var name when name.Contains("remark") => 5,
  908. var name when name.Contains("phone") => 4,
  909. var name when name.Contains("tel") => 4,
  910. var name when name.Contains("address") => 3,
  911. var name when name.Contains("email") => 3,
  912. _ => 5
  913. };
  914. }
  915. /// <summary>
  916. /// 获取显示名称
  917. /// </summary>
  918. private string GetDisplayName(System.Reflection.PropertyInfo property)
  919. {
  920. var displayAttr = property.GetCustomAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>();
  921. return displayAttr?.Name ?? property.Name;
  922. }
  923. /// <summary>
  924. /// 获取字段描述
  925. /// </summary>
  926. private string GetFieldDescription(System.Reflection.PropertyInfo property)
  927. {
  928. var descriptionAttr = property.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
  929. return descriptionAttr?.Description ?? string.Empty;
  930. }
  931. #endregion
  932. }
  933. }