在默认配置下,向一个不存在的索引写数据时,会自动创建索引。禁用方式:
PUT _cluster/settings
{
"persistent": {
"action.auto_create_index": "false"
}
}
不禁用时的示例:
假如 school 索引之前不存在,现在向其中写入数据:
PUT school/student/1
{ "name": "letian" }
会发现添加数据成功。执行 GET school/_search
能查询到数据。
禁用时的示例
假如 school 索引之前不存在,现在向其中写入数据:
PUT school/student/1
{ "name": "letian" }
报错如下:
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_expression",
"resource.id": "school",
"index_uuid": "_na_",
"index": "school"
}
],
"type": "index_not_found_exception",
"reason": "no such index",
"resource.type": "index_expression",
"resource.id": "school",
"index_uuid": "_na_",
"index": "school"
},
"status": 404
}
使用下面的方式可以成功:
# 创建索引
PUT school
# 添加数据
PUT school/student/1
{ "name": "letian" }