【问题标题】:Returning error when trying to insert document with non-existent field in elasticsearch尝试在弹性搜索中插入不存在字段的文档时返回错误
【发布时间】:2017-12-07 03:11:38
【问题描述】:

例如,我想在 myindex/mytype 中插入一个包含 两个 字符串的新文档

{ "name" : "joe", "city": "ny" }

但 mytype 仅包含 一个 字符串字段:“name”

在这种情况下,我希望索引失败,并且 elasticsearch 将返回错误。我怎么做? 现在,它创建了一个新文档:皱眉:

感谢您的帮助!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用映射的dynamic 设置。

    PUT /my_index
    {
        "mappings": {
            "my_type": {
                "dynamic":      "strict", 
                "properties": {
                    "name":  { "type": "string"}
                }
            }
        }
    }
    
    • 如果设置为 true(默认设置),则动态添加新字段。
    • 如果设置为 false,则忽略新字段。
    • 如果您设置了 strict,如果遇到未知字段,elasticsearch 会抛出异常。

    来源:https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 2021-06-20
      • 2018-09-29
      • 1970-01-01
      • 2016-08-13
      • 2016-10-10
      相关资源
      最近更新 更多