主题
创建与删除索引
索引是 Elasticsearch 存储和组织文档的基本单元。掌握索引的创建与删除操作,是有效管理数据和系统资源的关键。
1. 创建索引
通过 PUT
请求创建索引,可以指定索引名称及配置映射和设置。
基本创建示例
http
PUT /my-index
创建一个名为 my-index
的空索引,使用默认配置。
带映射和设置的创建
http
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": { "type": "text" },
"price": { "type": "float" },
"available": { "type": "boolean" }
}
}
}
number_of_shards
:分片数量,决定索引的并行处理能力number_of_replicas
:副本数量,提高数据冗余和查询性能mappings
:字段类型和索引规则定义
2. 删除索引
使用 DELETE
请求删除指定索引,操作不可逆,请谨慎执行。
http
DELETE /my-index
删除名为 my-index
的索引及其所有数据。
3. 注意事项
- 索引名称必须小写,且不能包含特殊字符
- 删除操作会清空所有相关数据,生产环境务必确认备份
- 创建索引时合理规划分片和副本数量,影响性能和扩展性
正确创建与删除索引,有助于优化数据存储结构和系统资源,确保 Elasticsearch 集群稳定高效运行。