当我使用 JsonNetSerializer 时,程序映射所有字段而不是我选择的字段
Program map all fields instead of ones I choose, when I use 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"
}
}
}
}
}
}
}
所以我不明白,为什么它忽略我的映射并添加所有字段而不是一个
我选择?请告诉我如何解决它。也许我必须以不同的方式创建映射?
可能发生的情况是
- 应用您在创建索引时定义的显式映射
- Elasticsearch 为它在 JSON 没有映射的文档中看到的属性添加新的字段映射,并为它们推断字段映射类型。
点 2 是 Elasticsearch 的默认行为,但可以通过更改 dynamic
property when creating the index and mapping.
根据问题的内容,您似乎在使用 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)
)
)
)
)
)
)
);
根据文档 link,false
的 dynamic
值将忽略 new 字段并且不会创建新的字段映射或索引字段,但字段仍将位于 _source
文档中。您 可能 还想在应该忽略的 Lecturer
的属性上设置 [JsonIgnore]
属性,这样它们就不会被序列化并发送到 Elasticsearch。
我在映射方面遇到了一些问题。我没有使用默认值,而是使用具有以下属性的 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"
}
}
}
}
}
}
}
所以我不明白,为什么它忽略我的映射并添加所有字段而不是一个 我选择?请告诉我如何解决它。也许我必须以不同的方式创建映射?
可能发生的情况是
- 应用您在创建索引时定义的显式映射
- Elasticsearch 为它在 JSON 没有映射的文档中看到的属性添加新的字段映射,并为它们推断字段映射类型。
点 2 是 Elasticsearch 的默认行为,但可以通过更改 dynamic
property when creating the index and mapping.
根据问题的内容,您似乎在使用 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)
)
)
)
)
)
)
);
根据文档 link,false
的 dynamic
值将忽略 new 字段并且不会创建新的字段映射或索引字段,但字段仍将位于 _source
文档中。您 可能 还想在应该忽略的 Lecturer
的属性上设置 [JsonIgnore]
属性,这样它们就不会被序列化并发送到 Elasticsearch。