Home > OS >  Create Index with Dynamic mapping
Create Index with Dynamic mapping

Time:02-18

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}

CodePudding user response:

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"
      }
    }
  }
}
  • Related