Ignore malformed(忽略格式错误)
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"
}
}