Dynamic Mapping for Nested Type

前端 未结 1 1047
深忆病人
深忆病人 2021-02-01 08:25

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

  {
    \"product\": {
        \"productId\": 99999,
        \"manufacturerId\": \"A0001\         


        
1条回答
  •  情歌与酒
    2021-02-01 09:05

    You just have to use path_match instead of match when the pattern refers to the whole field path, otherwise only its name (last part) is taken into account. Have a look at the reference page for the root object, which contains also some documentation related to dynamic templates.

    You might also want to use match_mapping_type as you can't set "index":"analyzed" for numeric or boolean fields for instance. In that case you might want to do different things depending on the field type.

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

    Also, I would avoid storing fields explicitly unless you really need to, as with elasticsearch you have the _source field stored by default, which is what you are going to need all the time.

    The following mapping should work in your case (without the product root 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"
                                  }
                              }
                          }
                      }
                  }                
              ]
          }
      }
    

    0 讨论(0)
提交回复
热议问题