您好,登录后才能下订单哦!
# ElasticSearch怎么查看、删除以及创建索引
## 一、ElasticSearch索引基础概念
### 1.1 什么是ElasticSearch索引
ElasticSearch中的索引(Index)是文档(Document)的集合,类似于传统关系型数据库中的"数据库"概念。每个索引包含一组具有相似特征的文档,这些文档被存储并建立索引以便快速搜索。
索引的核心特点包括:
- **分布式存储**:索引可以分片(Shard)存储在多个节点上
- **不可变性**:写入的文档不可更改,更新操作实际上是删除后重新创建
- **近实时搜索**:文档通常在1秒内即可被搜索到
### 1.2 索引与类型的历史演变
在ElasticSearch 7.0之前,索引可以包含多个类型(Type),类似于数据库中的表。但从7.0开始:
- 移除了类型概念,每个索引只能包含一个类型
- 8.x版本完全移除了类型相关API
- 现在最佳实践是为每个文档类型创建独立的索引
## 二、查看索引信息
### 2.1 查看所有索引
使用`_cat/indices` API可以查看集群中的所有索引:
```bash
GET /_cat/indices?v
响应示例:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open my_index L4j7dJj3Ql-9J7wzZwJZkw 1 1 5 0 12.5kb 12.5kb
各列含义: - health: 索引健康状态(green/yellow/red) - status: 索引状态(open/close) - pri: 主分片数量 - rep: 副本分片数量 - docs.count: 文档数量
获取索引的详细映射和设置信息:
GET /my_index
响应包含三部分: 1. aliases: 索引别名 2. mappings: 字段映射定义 3. settings: 索引配置设置
获取索引的详细统计信息:
GET /my_index/_stats
返回数据包括: - 文档总数和删除文档数 - 存储大小 - 索引操作计数 - 搜索统计等
查看索引的字段映射关系:
GET /my_index/_mapping
这对于了解字段的数据类型和分析器配置非常有用。
最简单的创建方式(使用默认配置):
PUT /new_index
创建索引时可以指定详细配置:
PUT /new_tech_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2,
"index": {
"analysis": {
"analyzer": {
"default": {
"type": "ik_max_word"
}
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"publish_date": {
"type": "date"
},
"author": {
"type": "keyword"
}
}
}
}
关键配置项: - number_of_shards: 主分片数(创建后不可修改) - number_of_replicas: 每个主分片的副本数(可动态调整) - analysis: 定义分析器和分词器 - mappings: 字段类型定义
对于需要创建多个相似索引的场景,可以使用索引模板:
PUT /_index_template/tech_template
{
"index_patterns": ["tech_*"],
"template": {
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"created_at": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}
当创建名称匹配tech_*
模式的索引时,会自动应用模板配置。
合理设置分片数:
副本设置:
映射设计:
删除单个索引:
DELETE /old_index
删除多个索引(使用逗号分隔):
DELETE /index1,index2
使用通配符删除(谨慎操作):
DELETE /temp_*
备份重要数据:
POST /_snapshot/my_backup/snapshot_2023?wait_for_completion=true
{
"indices": "important_index",
"ignore_unavailable": true,
"include_global_state": false
}
先关闭再删除(对于大索引):
POST /large_index/_close
DELETE /large_index
权限控制:
索引别名切换:
POST /_aliases
{
"actions": [
{ "remove": { "index": "old_index", "alias": "current_data" }},
{ "add": { "index": "new_index", "alias": "current_data" }}
]
}
冷数据归档:
创建别名:
POST /_aliases
{
"actions": [
{
"add": {
"index": "products_2023",
"alias": "current_products"
}
}
]
}
零停机切换:
POST /_aliases
{
"actions": [
{ "remove": { "index": "products_2023", "alias": "current_products" }},
{ "add": { "index": "products_2024", "alias": "current_products" }}
]
}
创建生命周期策略:
PUT /_ilm/policy/tech_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50GB",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "365d",
"actions": {
"delete": {}
}
}
}
}
}
应用策略到索引:
PUT /_index_template/tech_template
{
"index_patterns": ["tech-*"],
"template": {
"settings": {
"index.lifecycle.name": "tech_policy",
"index.lifecycle.rollover_alias": "tech-alias"
}
}
}
关闭索引(保留数据但不可查询):
POST /large_index/_close
重新打开索引:
POST /large_index/_open
冻结索引(减少内存占用):
POST /old_index/_freeze
解冻索引:
POST /old_index/_unfreeze
分片分配问题:
GET /_cluster/allocation/explain
资源不足:
配置错误:
分片大小调整:
PUT /large_index/_settings
{
"index.routing.allocation.total_shards_per_node": 2
}
刷新间隔调整:
PUT /high_write_index/_settings
{
"index.refresh_interval": "30s"
}
合并分段:
POST /fragmented_index/_forcemerge?max_num_segments=1
从快照恢复:
POST /_snapshot/my_backup/snapshot_2023/_restore
{
"indices": "important_index",
"rename_pattern": "important_index",
"rename_replacement": "restored_important_index"
}
本文详细介绍了ElasticSearch索引的核心操作:
_cat/indices
、_mapping
等API获取索引信息实际生产环境中,建议: - 为索引设计合理的命名规范 - 使用模板和别名简化管理 - 实施完善的备份策略 - 监控索引健康状况
通过合理运用这些索引管理技术,可以构建高效、稳定的ElasticSearch搜索系统。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。