当我使用jsonnetserializer时,程序Map所有字段而不是我选择的字段

zrfyljdw  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(388)

我在绘图方面有些问题。我使用具有以下属性的jsonnetserializer而不是默认值:

  1. var connectionSettings =
  2. new ConnectionSettings(pool, sourceSerializer: (builtin, settings) => new JsonNetSerializer(
  3. builtin, settings,
  4. () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore,
  5. ReferenceLoopHandling = ReferenceLoopHandling.Ignore},
  6. resolver => resolver.NamingStrategy = new CamelCaseNamingStrategy()
  7. ))
  8. .BasicAuthentication(userName, password);
  9. client = new ElasticClient(connectionSettings);

我喜欢这样:

  1. private static CreateIndexDescriptor GetLecturerMap(string indexName)
  2. {
  3. CreateIndexDescriptor map = new CreateIndexDescriptor(indexName);
  4. map.Mappings(M => M
  5. .Map<Lecturer>(m => m
  6. .Properties(prop => prop
  7. .Text(s => s
  8. .Name(n => n.FullName)
  9. )
  10. .Boolean(o => o
  11. .Name(s => s.IsActive)
  12. )
  13. .Number(s => s
  14. .Name(n => n.Id)
  15. .Type(NumberType.Integer)
  16. )
  17. .Date(d => d
  18. .Name(n => n.User.LastLogin)
  19. )
  20. .Object<User>(u=>u
  21. .Name(n=>n.User)
  22. .Properties(pr => pr
  23. .Text(t=>t
  24. .Name(n=>n.SkypeContact)
  25. )
  26. )
  27. )
  28. )
  29. )
  30. )
  31. ;
  32. return map;
  33. }

并这样称呼它:

  1. public int InitializeLecturers()
  2. {
  3. string lecturersIndexName = LECUTRERS_INDEX_NAME;
  4. client.Indices.Create(GetLecturerMap(lecturersIndexName));
  5. List<Lecturer> lecturers = GetLecturers();
  6. client.IndexMany(lecturers, lecturersIndexName);
  7. return lecturers.Count;
  8. }

当我使用以下方法从数据库中获取讲师时:

  1. private List<Lecturer> GetLecturers() {
  2. using (Context context = new Context(connectionString))
  3. {
  4. return context.Lecturers
  5. .ToList<Lecturer>();
  6. }
  7. }

程序创建以下Map:

  1. {
  2. "lecturers" : {
  3. "mappings" : {
  4. "properties" : {
  5. "firstName" : {
  6. "type" : "text",
  7. "fields" : {
  8. "keyword" : {
  9. "type" : "keyword",
  10. "ignore_above" : 256
  11. }
  12. }
  13. },
  14. "fullName" : {
  15. "type" : "text"
  16. },
  17. "id" : {
  18. "type" : "integer"
  19. },
  20. "isActive" : {
  21. "type" : "boolean"
  22. },
  23. "isLecturerHasGraduateStudents" : {
  24. "type" : "boolean"
  25. },
  26. "isNew" : {
  27. "type" : "boolean"
  28. },
  29. "isSecretary" : {
  30. "type" : "boolean"
  31. },
  32. "lastLogin" : {
  33. "type" : "date"
  34. },
  35. "lastName" : {
  36. "type" : "text",
  37. "fields" : {
  38. "keyword" : {
  39. "type" : "keyword",
  40. "ignore_above" : 256
  41. }
  42. }
  43. },
  44. "middleName" : {
  45. "type" : "text",
  46. "fields" : {
  47. "keyword" : {
  48. "type" : "keyword",
  49. "ignore_above" : 256
  50. }
  51. }
  52. },
  53. "skill" : {
  54. "type" : "text",
  55. "fields" : {
  56. "keyword" : {
  57. "type" : "keyword",
  58. "ignore_above" : 256
  59. }
  60. }
  61. },
  62. "user" : {
  63. "properties" : {
  64. "skypeContact" : {
  65. "type" : "text"
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }

所以我不明白,为什么它会忽略我的Map并添加所有字段而不是我选择的字段?请告诉我怎么修。可能我必须用另一种方式创建Map?

hrirmatl

hrirmatl1#

可能发生的是
将应用在创建索引时定义的显式Map
elasticsearch为它在json文档中看到的没有Map的属性添加新的字段Map,并推断它们的字段Map类型。
第2点是elasticsearch的默认行为,但是可以通过更改 dynamic 属性创建索引和Map时。
根据问题的内容,看起来您正在使用elasticsearch 6.x,这将是

  1. var client = new ElasticClient(settings);
  2. client.CreateIndex("index_name", c => c
  3. .Mappings(m => m
  4. .Map<Lecturer>(m => m
  5. .Dynamic(false)
  6. .Properties(prop => prop
  7. .Text(s => s
  8. .Name(n => n.FullName)
  9. )
  10. .Boolean(o => o
  11. .Name(s => s.IsActive)
  12. )
  13. .Number(s => s
  14. .Name(n => n.Id)
  15. .Type(NumberType.Integer)
  16. )
  17. .Date(d => d
  18. .Name(n => n.User.LastLogin)
  19. )
  20. .Object<User>(u => u
  21. .Name(n => n.User)
  22. .Properties(pr => pr
  23. .Text(t => t
  24. .Name(n => n.SkypeContact)
  25. )
  26. )
  27. )
  28. )
  29. )
  30. )
  31. );

根据文档链接, dynamic 的价值 false 将忽略新字段,不创建新字段Map或索引字段,但字段仍将位于 _source 文件。您可能还需要设置 [JsonIgnore] 属性的属性 Lecturer 这应该被忽略,这样它们就不会被序列化并发送到elasticsearch。

展开查看全部

相关问题