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

时间:2019-04-08 18:01:20

标签: elasticsearch elasticsearch-6

在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 \”:\“无法解析[user_data .ologic] \“}],\” type \“:\” mapper_parsing_exception \“,\”原因\“:\”无法解析[user_data.logical] \“,\” caused_by \“:{\” type \ “:\” illegal_argument_exception \“,\”原因\“:\”由于仅允许使用[true]或[false],因此无法解析[auto_directorUrl]值

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

1 个答案:

答案 0 :(得分:1)

那是announced breaking change

另一种选择是将ingest nodeconvert 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
    }
  }
]