Dynamic mapping for nested type

I am trying to create a dynamic mapping for the following objects:

{ "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 } } } 

I would like feature_* properties to be a nested type, which I defined in the nested_feature pattern matching below and that works as expected. However, I also want each property of the embedded object of the feature_* property to be multi_value with the optional facet property. I tried the second nested_template template, but without any success.

  { "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"} } } } 

Unfortunately, the properties in feature_* properties are created from another process and can be almost any pair of names / values. Any suggestions on using a dynamic template to set a property of both a nested and for each property inside a multi_field nested object with the additional facet property?

+8
elasticsearch
source share
1 answer

You just need to use path_match instead of match when the template refers to the entire field path, otherwise only its name (the last part) is taken into account. See the man page for the root object, which also contains documentation related to dynamic templates.

You can also use match_mapping_type as you cannot set "index":"analyzed" for numeric or logical fields, for example. In this case, you may need to do different things depending on the type of field.

I noticed that your document contains a root product object that you really don't need. I would delete it, since the type name is already a product.

In addition, I would not explicitly store the fields if you really do not need to, since with elasticsearch you have a _source field saved by default, this is what you will need all the time.

The following mapping should work in your case (without the root product object in the documents):

 { "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" } } } } } ] } } 
+21
source share

All Articles