SearchOptions.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Lucene.Net.Search;
  2. using Masuit.LuceneEFCore.SearchEngine.Extensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace Masuit.LuceneEFCore.SearchEngine
  8. {
  9. /// <summary>
  10. /// 搜索选项
  11. /// </summary>
  12. public class SearchOptions
  13. {
  14. /// <summary>
  15. /// 关键词
  16. /// </summary>
  17. public string Keywords { get; set; }
  18. /// <summary>
  19. /// 限定搜索字段
  20. /// </summary>
  21. public List<string> Fields { get; set; }
  22. /// <summary>
  23. /// 最大检索量
  24. /// </summary>
  25. public int MaximumNumberOfHits { get; set; }
  26. /// <summary>
  27. /// 多字段搜索时,给字段设定搜索权重
  28. /// </summary>
  29. private readonly Dictionary<string, float> _boosts;
  30. /// <summary>
  31. /// 多字段搜索时,给字段设定搜索权重
  32. /// </summary>
  33. internal Dictionary<string, float> Boosts
  34. {
  35. get
  36. {
  37. foreach (var field in Fields.Where(field => _boosts.All(x => x.Key.ToUpper() != field.ToUpper())))
  38. {
  39. _boosts.Add(field, 2.0f);
  40. }
  41. return _boosts;
  42. }
  43. }
  44. /// <summary>
  45. /// 排序字段
  46. /// </summary>
  47. public List<SortField> OrderBy { get; set; }
  48. /// <summary>
  49. /// 跳过多少条
  50. /// </summary>
  51. public int? Skip { get; set; }
  52. /// <summary>
  53. /// 取多少条
  54. /// </summary>
  55. public int? Take { get; set; }
  56. /// <summary>
  57. /// 文档类型
  58. /// </summary>
  59. public Type Type { get; set; }
  60. /// <summary>
  61. /// 匹配度,0-1,数值越大结果越精确
  62. /// </summary>
  63. public float Score { get; set; } = 0.5f;
  64. /// <summary>
  65. /// 过滤条件
  66. /// </summary>
  67. public Filter Filter { get; set; }
  68. /// <summary>
  69. /// 搜索选项
  70. /// </summary>
  71. /// <param name="keywords">关键词</param>
  72. /// <param name="fields">限定检索字段</param>
  73. /// <param name="maximumNumberOfHits">最大检索量</param>
  74. /// <param name="boosts">多字段搜索时,给字段设定搜索权重</param>
  75. /// <param name="type">文档类型</param>
  76. /// <param name="orderBy">排序字段</param>
  77. /// <param name="skip">跳过多少条</param>
  78. /// <param name="take">取多少条</param>
  79. public SearchOptions(string keywords, string fields, int maximumNumberOfHits = 1000, Dictionary<string, float> boosts = null, Type type = null, string orderBy = null, int? skip = null, int? take = null)
  80. {
  81. if (string.IsNullOrWhiteSpace(keywords))
  82. {
  83. throw new ArgumentException("搜索关键词不能为空!");
  84. }
  85. Keywords = keywords;
  86. MaximumNumberOfHits = maximumNumberOfHits;
  87. Skip = skip;
  88. Take = take;
  89. _boosts = boosts ?? new Dictionary<string, float>();
  90. Type = type;
  91. Fields = new List<string>();
  92. OrderBy = new List<SortField>()
  93. {
  94. SortField.FIELD_SCORE
  95. };
  96. // 添加被检索字段
  97. if (!string.IsNullOrEmpty(fields))
  98. {
  99. fields = fields.RemoveCharacters(" ");
  100. Fields.AddRange(fields.Split(',').ToList());
  101. }
  102. // 添加排序规则
  103. if (!string.IsNullOrEmpty(orderBy))
  104. {
  105. orderBy = orderBy.RemoveCharacters(" ");
  106. OrderBy.AddRange(orderBy.Split(',').Select(sortField => new SortField(sortField, SortFieldType.STRING)));
  107. }
  108. }
  109. /// <summary>
  110. /// 搜索选项
  111. /// </summary>
  112. /// <param name="keywords">关键词</param>
  113. /// <param name="size">页大小</param>
  114. /// <param name="fields">限定检索字段</param>
  115. /// <param name="page">第几页</param>
  116. public SearchOptions(string keywords, int page, int size, string fields) : this(keywords, fields, int.MaxValue, null, null, null, (page - 1) * size, size)
  117. {
  118. if (page < 1)
  119. {
  120. page = 1;
  121. }
  122. if (size < 1)
  123. {
  124. size = 1;
  125. }
  126. Skip = (page - 1) * size;
  127. Take = size;
  128. }
  129. /// <summary>
  130. /// 搜索选项
  131. /// </summary>
  132. /// <param name="keywords">关键词</param>
  133. /// <param name="size">页大小</param>
  134. /// <param name="page">第几页</param>
  135. /// <param name="t">需要被全文检索的类型</param>
  136. public SearchOptions(string keywords, int page, int size, Type t) : this(keywords, string.Join(",", t.GetProperties().Where(p => p.GetCustomAttributes<LuceneIndexAttribute>().Any()).Select(p => p.Name)), int.MaxValue, null, null, null, (page - 1) * size, size)
  137. {
  138. if (page < 1)
  139. {
  140. page = 1;
  141. }
  142. if (size < 1)
  143. {
  144. size = 1;
  145. }
  146. Skip = (page - 1) * size;
  147. Take = size;
  148. }
  149. public void SetBoosts(string field, float boost)
  150. {
  151. _boosts[field] = boost;
  152. }
  153. }
  154. }