Elasticsearch动态模板:不想分析字段,只想要关键字子字段
Elasticsearch dynamic template: don’t want field to be analyzed, only want a keyword subfield
我们正在使用动态模板来指定索引中某些字段的映射。大多数文本字段在模板中未提及,并获取到分析字段和关键字子字段的默认映射:
"someField" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
哪个好
索引中有一些我们不想分析的字段,只是做了关键字。但是,为了保持一致性,我们希望所有关键字字段都是一个名为 "keyword" 的子字段,如上所示。所以这个:
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"type" : "keyword",
"norms" : false
}
}
},
不合适,因为它使 "someField" 本身成为关键字,而我们希望 "someField.keyword" 成为关键字字段。但是如果我使用这个模板:
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"fields": {
"keyword": {
"type": "keyword",
"norms" : false
}
}
}
}
},
已创建已分析的 "top-level" 字段以及关键字子字段:
"someField" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
有没有办法不映射 "someField" 字段,只映射 "someField.keyword"?我可以将 index : false 设置为 "someField" 级别,但用户可能仍会尝试使用它,并想知道为什么它不起作用。
编辑:我试图按照 haltabush 的建议将 someField 定义为对象类型,但出现错误。使用此模板:
{
"template": "test*",
"order": 1,
"mappings" : {
"_default_" : {
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"type" : "object",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
]
}
}
}
当我post这个文件时:
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
}
您可以创建对象:
"someField" : {
"type" : "object",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
我们正在使用动态模板来指定索引中某些字段的映射。大多数文本字段在模板中未提及,并获取到分析字段和关键字子字段的默认映射:
"someField" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
哪个好
索引中有一些我们不想分析的字段,只是做了关键字。但是,为了保持一致性,我们希望所有关键字字段都是一个名为 "keyword" 的子字段,如上所示。所以这个:
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"type" : "keyword",
"norms" : false
}
}
},
不合适,因为它使 "someField" 本身成为关键字,而我们希望 "someField.keyword" 成为关键字字段。但是如果我使用这个模板:
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"fields": {
"keyword": {
"type": "keyword",
"norms" : false
}
}
}
}
},
已创建已分析的 "top-level" 字段以及关键字子字段:
"someField" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
有没有办法不映射 "someField" 字段,只映射 "someField.keyword"?我可以将 index : false 设置为 "someField" 级别,但用户可能仍会尝试使用它,并想知道为什么它不起作用。
编辑:我试图按照 haltabush 的建议将 someField 定义为对象类型,但出现错误。使用此模板:
{
"template": "test*",
"order": 1,
"mappings" : {
"_default_" : {
"dynamic_templates" : [
{
"someField" : {
"match" : "someField",
"mapping" : {
"type" : "object",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
]
}
}
}
当我post这个文件时:
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
}
您可以创建对象:
"someField" : {
"type" : "object",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},