LuceneIndexableBaseEntity.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Linq;
  9. using System.Reflection;
  10. namespace Masuit.LuceneEFCore.SearchEngine
  11. {
  12. /// <summary>
  13. /// 需要被索引的实体基类
  14. /// </summary>
  15. public abstract class LuceneIndexableBaseEntity : ILuceneIndexable
  16. {
  17. /// <summary>
  18. /// 主键id
  19. /// </summary>
  20. [LuceneIndex(Name = nameof(Id), Store = Field.Store.YES), Key]
  21. #if Int
  22. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  23. public int Id { get; set; }
  24. #endif
  25. #if Long
  26. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  27. public long Id { get; set; }
  28. #endif
  29. #if String
  30. public string Id { get; set; }
  31. #endif
  32. #if Guid
  33. public Guid Id { get; set; }
  34. #endif
  35. /// <summary>
  36. /// 转换成Lucene文档
  37. /// </summary>
  38. /// <returns></returns>
  39. public virtual Document ToDocument()
  40. {
  41. var doc = new Document();
  42. var type = GetType();
  43. if (type.Assembly.IsDynamic && type.FullName.Contains("Prox"))
  44. {
  45. type = type.BaseType;
  46. }
  47. var classProperties = type.GetProperties();
  48. doc.Add(new StringField("Type", type.AssemblyQualifiedName, Field.Store.YES));
  49. doc.Add(new StringField("IndexId", type.FullName + Id, Field.Store.YES));
  50. foreach (var propertyInfo in classProperties)
  51. {
  52. var attrs = propertyInfo.GetCustomAttributes<LuceneIndexAttribute>().ToList();
  53. if (attrs.Count==0)
  54. {
  55. continue;
  56. }
  57. var propertyValue = propertyInfo.GetValue(this);
  58. if (propertyValue == null)
  59. {
  60. continue;
  61. }
  62. foreach (var attr in attrs)
  63. {
  64. string name = !string.IsNullOrEmpty(attr.Name) ? attr.Name : propertyInfo.Name;
  65. switch (propertyValue)
  66. {
  67. case DateTime time:
  68. doc.Add(new StringField(name, time.ToString("yyyy-MM-dd HH:mm:ss"), attr.Store));
  69. break;
  70. case int num:
  71. doc.Add(new Int32Field(name, num, attr.Store));
  72. break;
  73. case long num:
  74. doc.Add(new Int64Field(name, num, attr.Store));
  75. break;
  76. case float num:
  77. doc.Add(new SingleField(name, num, attr.Store));
  78. break;
  79. case double num:
  80. doc.Add(new DoubleField(name, num, attr.Store));
  81. break;
  82. case Guid guid:
  83. doc.Add(new StringField(name, guid.ToString(), attr.Store));
  84. break;
  85. default:
  86. string value = attr.IsHtml ? propertyValue.ToString().RemoveHtmlTag() : propertyValue.ToString();
  87. doc.Add(new TextField(name, value, attr.Store));
  88. break;
  89. }
  90. }
  91. }
  92. return doc;
  93. }
  94. }
  95. }