LuceneIndexableBaseEntity.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Lucene.Net.Documents;
  2. using Masuit.LuceneEFCore.SearchEngine.Extensions;
  3. using Masuit.LuceneEFCore.SearchEngine.Interfaces;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.ComponentModel.DataAnnotations.Schema;
  8. using System.Reflection;
  9. namespace Masuit.LuceneEFCore.SearchEngine
  10. {
  11. /// <summary>
  12. /// 需要被索引的实体基类
  13. /// </summary>
  14. public abstract class LuceneIndexableBaseEntity : ILuceneIndexable
  15. {
  16. /// <summary>
  17. /// 主键id
  18. /// </summary>
  19. [LuceneIndex(Name = "Id", Store = Field.Store.YES), Key]
  20. #if Int
  21. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  22. public int Id { get; set; }
  23. #endif
  24. #if Long
  25. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  26. public long Id { get; set; }
  27. #endif
  28. #if String
  29. public string Id { get; set; }
  30. #endif
  31. #if Guid
  32. public Guid Id { get; set; }
  33. #endif
  34. /// <summary>
  35. /// 索引唯一id
  36. /// </summary>
  37. [LuceneIndex(Name = "IndexId", Store = Field.Store.YES)]
  38. [NotMapped, JsonIgnore]
  39. public string IndexId
  40. {
  41. get => GetType().Name + ":" + Id;
  42. set
  43. {
  44. }
  45. }
  46. /// <summary>
  47. /// 转换成Lucene文档
  48. /// </summary>
  49. /// <returns></returns>
  50. public virtual Document ToDocument()
  51. {
  52. var doc = new Document();
  53. var type = GetType();
  54. if (type.Assembly.IsDynamic && type.FullName.Contains("Prox"))
  55. {
  56. type = type.BaseType;
  57. }
  58. var classProperties = type.GetProperties();
  59. doc.Add(new StringField("Type", type.AssemblyQualifiedName, Field.Store.YES));
  60. foreach (var propertyInfo in classProperties)
  61. {
  62. var propertyValue = propertyInfo.GetValue(this);
  63. if (propertyValue == null)
  64. {
  65. continue;
  66. }
  67. var attrs = propertyInfo.GetCustomAttributes<LuceneIndexAttribute>();
  68. foreach (var attr in attrs)
  69. {
  70. string name = !string.IsNullOrEmpty(attr.Name) ? attr.Name : propertyInfo.Name;
  71. string value = attr.IsHtml ? propertyValue.ToString().RemoveHtmlTag() : propertyValue.ToString();
  72. switch (propertyValue)
  73. {
  74. case string _ when value.Length < 8191:
  75. doc.Add(new TextField(name, value, attr.Store));
  76. break;
  77. case string _:
  78. doc.Add(new TextField(name, value, attr.Store));
  79. break;
  80. case int num:
  81. doc.Add(new Int32Field(name, num, attr.Store));
  82. break;
  83. case long num:
  84. doc.Add(new Int64Field(name, num, attr.Store));
  85. break;
  86. case float num:
  87. doc.Add(new SingleField(name, num, attr.Store));
  88. break;
  89. case decimal num:
  90. doc.Add(new DoubleField(name, (double)num, attr.Store));
  91. break;
  92. case double num:
  93. doc.Add(new DoubleField(name, num, attr.Store));
  94. break;
  95. case DateTime _:
  96. doc.Add(new TextField(name, value, attr.Store));
  97. break;
  98. default:
  99. doc.Add(new TextField(name, value, attr.Store));
  100. break;
  101. }
  102. }
  103. }
  104. return doc;
  105. }
  106. }
  107. }