简体   繁体   English

在ElasticSearch中使用动态模板失败

[英]Failing with dynamic templates in ElasticSearch

I try to create a simple template, including some dynamic template, and I can't seem to index documents. 我尝试创建一个简单的模板,包括一些动态模板,但似乎无法为文档建立索引。

I get the error: 我得到错误:

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

It works fine if I remove the dynaic_templates part of the JSON. 如果删除JSON的dynaic_templates部分,则效果很好。

What am I doing wrong? 我究竟做错了什么?

Here is a reproduction of the code in python: 这是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

The dynamic_templates should be an array of elements, meaning surrounded by [ ] . dynamic_templates应该是一个元素数组,意味着由[ ]包围。 So, yours should look like this: 因此,您应该看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM