我正在尝试为对象创建动态映射,如下所示:
{
"product": {
"productId": 99999,
"manufacturerId": "A0001",
"manufacturerCode": "A101LI",
"name": "Test Product",
"description": "Describe the product here.",
"feature_details":{
"category": "Category1",
"brand": "Brand Name"
},
"feature_tpcerts":{
"certifiedPass": true,
"levelCertified": 2
},
"feature_characteristics":{
"amount": 0.73,
"location": 49464
}
}
}
我希望 feature_*
属性是一个嵌套类型,我在下面的映射中使用nested_feature模板定义了它,它按预期工作 . 但是,我还希望将 feature_*
属性的嵌套对象中的每个属性设置为 multi_value
,并定义另外的 facet
属性 . 我已经尝试了第二个nested_template模板,但没有任何成功 .
{
"product" : {
"_timestamp" : {"enabled" : true, "store": "yes" },
"dynamic_templates": [
{
"nested_feature": {
"match" : "feature_*",
"mapping" : {
"type" : "nested",
"stored": "true"
}
}
},
{
"nested_template": {
"match": "feature_*.*",
"mapping": {
"type": "multi_field",
"fields": {
"{name}": {
"type": "{dynamic_type}",
"index": "analyzed"
},
"facet": {
"type": "{dynamic_type}",
"index": "not_analyzed"
}
}
}
}
}
],
"properties" : {
"productId" : { "type" : "integer", "store" : "yes"},
"manufacturerId" : { "type" : "string", "store" : "yes", "index" : "analyzed"},
"manufacturer" : { "type" : "string", "store" : "yes", "index" : "not_analyzed"},
"manufacturerCode" : { "type" : "string", "store" : "yes"},
"name" : {"type" : "string", "store" : "yes"},
"description": {"type": "string", "index" : "analyzed"}
}
}
}
不幸的是, feature_*
属性中的属性是从另一个进程创建的,几乎可以是任何名称/值对 . 有关如何使用动态模板将属性设置为嵌套的建议,以及使用附加的 facet
属性使嵌套对象 multi_field
中的每个属性成为可能吗?
1 回答
当模式引用整个字段路径时,您只需使用
path_match
而不是match
,否则仅考虑其名称(最后一部分) . 查看根对象的reference page,其中还包含一些与动态模板相关的文档 .您可能还想使用
match_mapping_type
,因为您无法为数字或布尔字段设置"index":"analyzed"
. 在这种情况下,您可能希望根据字段类型执行不同的操作 .我注意到您的文档包含产品根对象,您并不真正需要它 . 我会删除它,因为类型名称已经是产品 .
此外,我会避免显式存储字段,除非你真的需要,就像在弹性搜索中你默认存储_source field,这是你一直需要的 .
以下映射应该适用于您的情况(文档中没有产品根对象):