在ElasticSearch中使用动态模板失败

时间:2015-07-08 07:45:31

标签: elasticsearch

我尝试创建一个简单的模板,包括一些动态模板,我似乎无法索引文档。

我收到错误:

400 {"error":"MapperParsingException[mapping [_default_]]; nested: ClassCastException[java.util.LinkedHashMap cannot be cast to java.util.List]; ","status":400}

如果删除JSON的dynaic_templates部分,它可以正常工作。

我做错了什么?

这是python代码的再现:

import requests
import json

template = {
    "template": "some_index_*",
    "settings": {
        "index": {
            "number_of_replicas": "0",
            "number_of_shards": "8",

        }
    },
    "mappings": {
        "_default_": {
            "_all": {
                "enabled": False
            },
            "properties": {
                "H1": {
                    "properties": {
                        "sub1": {
                            "doc_values": True,
                            "type": "boolean",
                            "index": "not_analyzed"
                        },
                        "sub2": {
                            "index": "no",
                            "type": "string"
                        },
                    }
                }
            },
            "dynamic_templates": {
                "text_indexed_template": {
                    "match_mapping_type": "string",
                    "mapping": {
                        "index": "not_analyzed",
                        "type": "string",
                        "doc_values": True
                    },
                    "match": "*_idx"
                }
            },
            "_source": {
                "compress": False
            }
        }
    },
}

res = requests.put(
    url="http://127.0.0.1:9200/" + "_template/my_template/",
    data=json.dumps(template)
)

print res.status_code, res.content

new_doc = {
            "H1": {
                "sub1": True,
                "sub2": "testing, testing"
            }
        }

res = requests.post(
    url="http://127.0.0.1:9200/" + 'some_index_tryme/record/',
    data=json.dumps(new_doc)
)

print res.status_code, res.content

1 个答案:

答案 0 :(得分:5)

dynamic_templates应该是一个元素数组,意味着被[ ]包围。所以,你的应该是这样的:

  "dynamic_templates": [
    {
      "text_indexed_template": {
        "match_mapping_type": "string",
        "mapping": {
          "index": "not_analyzed",
          "type": "string",
          "doc_values": true
        },
        "match": "*_idx"
      }
    }
  ]