具有布尔类型多字段的文档在创建过程中失败

doc with multi-field of boolean type fails during creation

在 v5.5 中,我们有以下工作正常的映射

PUT multiple_datatypes
{
  "mappings": {
    "_doc": {
      "properties": {
        "user_data": {
          "type": "text",
          "fields": {
            "numeric": {
              "type": "double",
              "ignore_malformed": true
            },
            "date": {
              "type": "date",
              "ignore_malformed": true
            }
            "logical": {
              "type": "boolean",
             }
          }
        }
      }
    }
  }

在 6.2 中,相同的映射失败并出现错误
HTTP/1.1 400 错误请求]\n{\"error\":{\"root_cause\":[{\"type\":\"mapper_parsing_exception\",\"reason\":\"failed to parse [user_data.logical]\"}],\"type\":\"mapper_parsing_exception\",\"reason\":\"failed to parse [user_data.logical]\",\"caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"无法解析值 [auto_directorUrl] 因为只允许 [true] 或 [false]

输入的数据是字符串,"auto_directorURL"失败了。 ignore_malformed 标志不适用于布尔类型。但是,这在 v5.5 中有效。我发现在 v6.2 中,ES 已经严格强制将 boolean 类型值设为 'true' 或 'false'。但这在多字段中失败,因为它没有 ignore_malformed 标志。 解决方案是什么?这是 BWC 漏洞和错误吗

这是一个announced breaking change

另一种方法是使用 ingest node with a convert processor 将该字段的布尔值存储到另一个布尔字段中:

PUT _ingest/pipeline/boolean-pipeline
{
  "description": "converts the content of the field to a boolean value",
  "processors" : [
    {
      "convert" : {
        "field" : "user_data",
        "target_field" : "user_data_boolean",
        "type": "boolean",
        "on_failure" : [
          {
            "set" : {
              "field" : "user_data_boolean",
              "value" : false
            }
          }
        ]
      }
    }
  ]
}

然后您可以使用该管道索引数据

PUT test/doc/1?pipeline=boolean-pipeline
{
  "user_data": "true"
}

PUT test/doc/2?pipeline=boolean-pipeline
{
  "user_data": "auto_directorURL"
}

因此,您将获得以下索引数据,这几乎是您所期望的:

"hits" : [
  {
    "_index" : "test",
    "_type" : "doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "user_data" : "auto_directorURL",
      "user_data_boolean" : false
    }
  },
  {
    "_index" : "test",
    "_type" : "doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "user_data" : "true",
      "user_data_boolean" : true
    }
  }
]