使用动态映射创建索引

Create Index with Dynamic mapping

我正在尝试使用动态映射创建索引。映射模板中似乎有一些错误。数据集包含字符串、数值和日期。我必须适应所有这些。

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

这是我用来创建新索引的代码

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)

我遇到了错误

 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}

您的 dynamic templates 操作不正确,它应该是这样的。 dynamic_templates 应该在它自己的数组中作为属性的兄弟。你也不需要指定 _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"
      }
    }
  }
}