2013-10-11 76 views
8

我试图创建一个动态映射像对象的以下:动态映射为嵌套类型

{ 
    "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_*属性中的属性是从另一个进程创建,并且可以是几乎任何名称/值对。有关如何使用动态模板设置嵌套属性的任何建议,以及使嵌套对象multi_field中的每个属性都具有其他facet属性?

回答

21

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

您可能还想使用match_mapping_type,因为您无法为数字或布尔值字段设置"index":"analyzed"。在这种情况下,您可能想根据字段类型做不同的事情。

我注意到你的文档包含了你不需要的产品根对象。我会删除它,因为类型名称已经是产品。

此外,我会避免明确地存储字段,除非你真的需要,与elasticsearch一样,你有默认存储的_source field,这是你一直需要的。

以下映射应该在你的情况下工作(没有在文档中的产品根对象):

{ 
     "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" 
           } 
          } 
         } 
        } 
       }     
      ] 
     } 
    } 
+0

这完美地工作!感谢关于字段类型匹配的建议,存储字段/ _source字段和文档结构。我对使用Elasticsearch很感兴趣,并且非常感谢这些技巧。 –

+0

@PaigeCook欢迎您,非常感谢elasticsearch,我相信您很快就会开始回答有关它的问题! ;) – javanna