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

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

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

var connectionSettings =
                new ConnectionSettings(pool, sourceSerializer: (builtin, settings) => new JsonNetSerializer(
                    builtin, settings,
                    () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore,
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore},
                    resolver => resolver.NamingStrategy = new CamelCaseNamingStrategy()
                ))
                .BasicAuthentication(userName, password);

            client = new ElasticClient(connectionSettings);

我喜欢这样:

private static CreateIndexDescriptor GetLecturerMap(string indexName)
        {
            CreateIndexDescriptor map = new CreateIndexDescriptor(indexName);
            map.Mappings(M => M
                .Map<Lecturer>(m => m
                    .Properties(prop => prop
                        .Text(s => s
                            .Name(n => n.FullName)
                        )
                        .Boolean(o => o
                            .Name(s => s.IsActive)
                         )
                        .Number(s => s
                            .Name(n => n.Id)
                            .Type(NumberType.Integer)
                        )

                        .Date(d => d
                            .Name(n => n.User.LastLogin)
                        )
                        .Object<User>(u=>u
                            .Name(n=>n.User)
                            .Properties(pr => pr
                                .Text(t=>t
                                    .Name(n=>n.SkypeContact)
                                    )
                                )
                            )
                    )
                )
             )
           ;
            return map;
        }

并这样称呼它:

public int InitializeLecturers()
    {
        string lecturersIndexName = LECUTRERS_INDEX_NAME;
        client.Indices.Create(GetLecturerMap(lecturersIndexName));
        List<Lecturer> lecturers = GetLecturers();

        client.IndexMany(lecturers, lecturersIndexName);
        return lecturers.Count;
    }

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

private List<Lecturer> GetLecturers() {
            using (Context context = new Context(connectionString))
            {
                return context.Lecturers
                    .ToList<Lecturer>();
            }
        }

程序创建以下Map:

{
  "lecturers" : {
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "fullName" : {
          "type" : "text"
        },
        "id" : {
          "type" : "integer"
        },
        "isActive" : {
          "type" : "boolean"
        },
        "isLecturerHasGraduateStudents" : {
          "type" : "boolean"
        },
        "isNew" : {
          "type" : "boolean"
        },
        "isSecretary" : {
          "type" : "boolean"
        },
        "lastLogin" : {
          "type" : "date"
        },
        "lastName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "middleName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "skill" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "user" : {
          "properties" : {
            "skypeContact" : {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

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

hrirmatl

hrirmatl1#

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

var client = new ElasticClient(settings);

client.CreateIndex("index_name", c => c
    .Mappings(m => m
        .Map<Lecturer>(m => m
            .Dynamic(false)
            .Properties(prop => prop
                .Text(s => s
                    .Name(n => n.FullName)
                )
                .Boolean(o => o
                    .Name(s => s.IsActive)
                 )
                .Number(s => s
                    .Name(n => n.Id)
                    .Type(NumberType.Integer)
                )

                .Date(d => d
                    .Name(n => n.User.LastLogin)
                )
                .Object<User>(u => u
                    .Name(n => n.User)
                    .Properties(pr => pr
                        .Text(t => t
                            .Name(n => n.SkypeContact)
                            )
                        )
                    )
            )
        )
    )
);

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

相关问题