首页 > 解决方案 > 具有布尔类型的多字段的文档在创建期间失败

问题描述

在 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 Bad Request]\n{\"error\":{\"root_cause\":[{\"type\":\"mapper_parsing_exception\",\"reason \":\"解析[user_data.logical]失败\"}],\"type\":\"mapper_parsing_exception\",\"reason\":\"解析[user_data.logical]失败\",\ "caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"解析值失败 [auto_directorUrl] 因为只允许 [true] 或 [false]

输入数据是一个字符串“auto_directorURL”,但它失败了。ignore_malformed 标志不适用于布尔类型。但是,这在 v5.5 中有效。我发现在 v6.2 中,ES 严格执行布尔类型值作为“真”或“假”。但这在多字段中失败,因为它没有 ignore_malformed 标志。解决方案是什么?这是 BWC 休息和错误吗

标签: elasticsearchelasticsearch-6

解决方案


这是一个宣布的重大变化

另一种方法是使用带有转换处理器的摄取节点将该字段的布尔值存储到另一个布尔字段中:

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
    }
  }
]

推荐阅读