嵌套类型的动态映射

Pai*_*ook 10 elasticsearch

我正在尝试为对象创建动态映射,如下所示:

  {
    "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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这些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"}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,属性中的feature_*属性是从另一个进程创建的,几乎可以是任何名称/值对.有关如何使用动态模板将属性设置为嵌套以及multi_field使用其他facet属性使嵌套对象中的每个属性的任何建议?

jav*_*nna 25

您只需使用path_match而不是match当模式引用整个字段路径时,否则仅考虑其名称(最后部分).查看根对象的参考页面,其中还包含一些与动态模板相关的文档.

您可能还想使用,match_mapping_type因为您不能"index":"analyzed"为数字或布尔字段设置.在这种情况下,您可能希望根据字段类型执行不同的操作.

我注意到您的文档包含产品根对象,您并不真正需要它.我会删除它,因为类型名称已经是产品.

此外,我会避免显式存储字段,除非你真的需要,因为有弹性搜索,你有默认存储的_source字段,这是你一直需要的.

以下映射应该适用于您的情况(文档中没有产品根对象):

{
      "product" : {
          "dynamic_templates": [
              {
                "nested_feature": {
                  "match" : "feature_*",
                  "mapping" : {
                    "type" : "nested"
                  }
                }
              },
              {
                  "nested_template": {
                      "path_match": "feature_*.*",
                      "match_mapping_type" : "string",
                      "mapping": {
                          "type": "multi_field",
                          "fields": {
                              "{name}": {
                                  "type": "{dynamic_type}",
                                  "index": "analyzed"
                              },
                              "facet": {
                                  "type": "{dynamic_type}",
                                  "index": "not_analyzed"
                              }
                          }
                      }
                  }
              }                
          ]
      }
  }
Run Code Online (Sandbox Code Playgroud)