在 _mapping
中指定dynamic
为 strict
即可。
示例:
# 步骤1: 创建索引
PUT school
# 步骤2: 创建 type
POST school/student/_mapping
{
"dynamic": "strict",
"properties" : {
"name" : {
"type" : "keyword"
}
}
}
执行下面的 DSL 会成功:
PUT school/student/1
{ "name": "letian" }
但下面的DSL 会失败:
PUT school/student/2
{ "name": "letian", "age": 20 }
报错信息如下:
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [age] within [student] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [age] within [student] is not allowed"
},
"status": 400
}
参考文档: