0

I'm trying to create an index with dynamic mapping. It seems like there are some error in the mapping template. The dataset contains string, numeric value and dates. I've to accommodate all those.

mapping = {
    "settings": {
        "number_of_shards": 2,
        "number_of_replicas": 1
    },
    "mappings": {
        "_doc": { 
            "properties": {  
                "details": {
                    "dynamic_templates": [
                       {
                         "name_template": {
                           "match": "*",
                           "mapping": {
                             "type": "string",
                             "dynamic": True
                           }
                         }
                       }
                     ]
    
        }
    }

this is the code I'm using to create new index

if es.indices.exists(index="es_index") == False:
   index_creation = es.indices.create(index="es_index", ignore=400, body=mapping)
   print("index created: ",index_creation)

I'm getting the error

 index created:  {'error': {'root_cause': 
[{'type': 'mapper_parsing_exception', 'reason': 'Root mapping definition has 
unsupported parameters:  [details : {dynamic_templates=[{name_template=
{mapping={dynamic=true, type=string}, match=*}}]}]'}], 'type': 'mapper_parsing_exception', 'reason': 'Failed to parse mapping [_doc]: 
Root mapping definition has unsupported parameters:  [details : {dynamic_templates=[{name_template={mapping={dynamic=true, type=string}, match=*}}]}]', 'caused_by': {'type': 'mapper_parsing_exception', 'reason': 'Root mapping definition has unsupported parameters:  [details : {dynamic_templates=[{name_template={mapping={dynamic=true, type=string}, match=*}}]}]'}}, 'status': 400}
vishnuvid
  • 63
  • 9

1 Answers1

1

You're not doing dynamic templates correctly, here is how it should look like. dynamic_templates should be in its own array as a sibling of properties. Also you don't need to specify _doc:

mapping = {
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1
  },
  "mappings": {
    "dynamic_templates": [
      {
        "name_template": {
          "path_match": "details.*",
          "mapping": {
            "type": "text"
          }
        }
      }
    ],
    "properties": {
      "details": {
          "type": "object"
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • unfortunately I got another error. index created: {'error': {'root_cause': [{'type': 'mapper_parsing_exception', 'reason': 'No type specified for field [details]'}], 'type': 'mapper_parsing_exception', 'reason': 'Failed to parse mapping [_doc]: No type specified for field [details]', 'caused_by': {'type': 'mapper_parsing_exception', 'reason': 'No type specified for field [details]'}}, 'status': 400}. What is the type to be added for the details field? – vishnuvid Feb 18 '22 at 07:08
  • Actually, there were other errors, I've updated my answer completely. Please have a look – Val Feb 18 '22 at 07:13