Link Search Menu Expand Document Documentation Menu

忽略格式错误

ignore_malformed 映射参数指示索引引擎忽略与字段预期格式不匹配的值。启用此参数后,畸形值将不会被索引,从而避免因数据格式问题导致整个文档被拒绝。这确保了即使一个或多个字段包含无法解析的数据,文档也能被存储。

默认情况下,ignore_malformed 处于禁用状态,这意味着如果某个值无法根据字段类型进行解析,则整个文档的索引将失败。

示例:ignore_malformed 关闭

创建一个名为 people_no_ignore 的索引,其中包含一个 integer 类型的 age 字段。默认情况下,ignore_malformed 设置为 false

PUT /people_no_ignore
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      }
    }
  }
}

索引一个包含畸形值的文档

PUT /people_no_ignore/_doc/1
{
  "age": "twenty"
}

请求因畸形值而失败

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [age] of type [integer] in document with id '1'. Preview of field's value: 'twenty'"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse field [age] of type [integer] in document with id '1'. Preview of field's value: 'twenty'",
    "caused_by": {
      "type": "number_format_exception",
      "reason": "For input string: \"twenty\""
    }
  },
  "status": 400
}

示例:ignore_malformed 开启

创建一个名为 people_ignore 的索引,其中 age 字段的 ignore_malformed 设置为 true

PUT /people_ignore
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer",
        "ignore_malformed": true
      }
    }
  }
}

索引一个包含畸形值的文档

PUT /people_ignore/_doc/1
{
  "age": "twenty"
}

检索文档

GET /people_ignore/_doc/1

响应显示文档已成功索引,尽管包含畸形值

{
  "_index": "people_ignore",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "age": "twenty"
  }
}
剩余 350 字符

有问题?

想要贡献?