当我使用JsonNetSerializer时,程序映射所有字段,而不是我选择的字段



我在映射方面遇到了一些问题。我使用具有以下属性的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>();
}
}

程序创建以下映射:

{
"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"
}
}
}
}
}
}
}

所以我不明白,为什么它忽略了我的映射,并添加了所有字段而不是一个我选择?请告诉我如何修复它。也许我必须用不同的方式创建映射?

可能发生的是

  1. 应用在创建索引时定义的显式映射
  2. Elasticsearch为它在JSON文档中看到的没有映射的属性添加了新的字段映射,并推断出它们的字段映射类型

Point 2是Elasticsearch的默认行为,但在创建索引和映射时,可以通过更改dynamic属性来更改它。

根据问题的内容,看起来你使用的是Elasticsearch6.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)
)
)
)
)
)
)
);

根据文档链接,falsedynamic值将忽略新的字段,并且不会创建新的字段映射或索引字段,但这些字段仍将在_source文档中。您可能还想在Lecturer的属性上设置应忽略的[JsonIgnore]属性,这样它们就不会被序列化并发送到Elasticsearch。

最新更新