我们使用动态模板为索引中的某些字段指定映射。模板中未提及大多数文本字段,并获取到分析字段和关键字子字段的默认映射:
"someField" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
这很好。
索引包含一些我们不想分析的字段,只是制作了关键字。但是,为了保持一致性,我们希望所有关键字字段都是名为"关键字"的子字段,如上所述。所以这个:
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"type" : "keyword",
"norms" : false
}
}
},
不合适,因为它使得#34; someField"本身就是一个关键字,我们想要" someField.keyword"成为关键字字段。但如果我使用这个模板:
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"fields": {
"keyword": {
"type": "keyword",
"norms" : false
}
}
}
}
},
分析了#34;顶级"创建字段以及关键字子字段:
"someField" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
有没有办法没有#34; someField"字段映射,只有" someField.keyword"?我可以为" someField"设置index:false。等级,但用户可能仍会尝试使用它,并想知道它为什么不起作用。
编辑:我试图将someField定义为对象类型,如haltabush所示,但是会出错。使用此模板:
{
"template": "test*",
"order": 1,
"mappings" : {
"_default_" : {
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"type" : "object",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
]
}
}
}
当我发布此文件时:
curl -XPUT localhost:9200/test-1/doc/1?pretty -d '
{
"someField" : "some value"
}
'
我收到此错误:
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "object mapping for [someField] tried to parse field [someField] as object, but found a concrete value"
}
],
"type" : "mapper_parsing_exception",
"reason" : "object mapping for [someField] tried to parse field [someField] as object, but found a concrete value"
},
"status" : 400
}
答案 0 :(得分:0)
您可以创建一个对象:
"someField" : {
"type" : "object",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},