ElasticSearch怎么查看、删除以及创建索引

发布时间:2021-09-07 09:16:32 作者:chen
来源:亿速云 阅读:227
# 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: 文档数量

2.2 查看特定索引详情

获取索引的详细映射和设置信息:

GET /my_index

响应包含三部分: 1. aliases: 索引别名 2. mappings: 字段映射定义 3. settings: 索引配置设置

2.3 查看索引统计信息

获取索引的详细统计信息:

GET /my_index/_stats

返回数据包括: - 文档总数和删除文档数 - 存储大小 - 索引操作计数 - 搜索统计等

2.4 查看索引映射

查看索引的字段映射关系:

GET /my_index/_mapping

这对于了解字段的数据类型和分析器配置非常有用。

三、创建索引

3.1 基本索引创建

最简单的创建方式(使用默认配置):

PUT /new_index

3.2 带配置的索引创建

创建索引时可以指定详细配置:

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: 字段类型定义

3.3 索引模板

对于需要创建多个相似索引的场景,可以使用索引模板:

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_*模式的索引时,会自动应用模板配置。

3.4 创建索引的最佳实践

  1. 合理设置分片数

    • 每个分片建议存储20-40GB数据
    • 考虑数据增长量
    • 分片过多会导致资源开销
  2. 副本设置

    • 生产环境至少1个副本
    • 可先设置为0,索引创建完成后再调整
  3. 映射设计

    • 预先定义好字段类型
    • 区分text和keyword类型的用途
    • 设置合适的分词器

四、删除索引

4.1 基本删除操作

删除单个索引:

DELETE /old_index

删除多个索引(使用逗号分隔):

DELETE /index1,index2

使用通配符删除(谨慎操作):

DELETE /temp_*

4.2 删除前的注意事项

  1. 备份重要数据

    POST /_snapshot/my_backup/snapshot_2023?wait_for_completion=true
    {
     "indices": "important_index",
     "ignore_unavailable": true,
     "include_global_state": false
    }
    
  2. 先关闭再删除(对于大索引):

    POST /large_index/_close
    DELETE /large_index
    
  3. 权限控制

    • 确保用户有删除权限
    • 生产环境建议限制删除操作

4.3 删除索引的替代方案

  1. 索引别名切换

    POST /_aliases
    {
     "actions": [
       { "remove": { "index": "old_index", "alias": "current_data" }},
       { "add": { "index": "new_index", "alias": "current_data" }}
     ]
    }
    
  2. 冷数据归档

    • 将旧索引移动到冷节点
    • 或使用ILM(Index Lifecycle Management)自动管理

五、索引管理高级技巧

5.1 索引别名管理

创建别名:

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" }}
  ]
}

5.2 索引生命周期管理(ILM)

创建生命周期策略:

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"
    }
  }
}

5.3 索引状态管理

关闭索引(保留数据但不可查询):

POST /large_index/_close

重新打开索引:

POST /large_index/_open

冻结索引(减少内存占用):

POST /old_index/_freeze

解冻索引:

POST /old_index/_unfreeze

六、常见问题解决方案

6.1 索引创建失败排查

  1. 分片分配问题

    GET /_cluster/allocation/explain
    
  2. 资源不足

    • 检查磁盘空间
    • 检查内存使用情况
  3. 配置错误

    • 验证mapping语法
    • 检查分析器配置

6.2 索引性能优化

  1. 分片大小调整

    PUT /large_index/_settings
    {
     "index.routing.allocation.total_shards_per_node": 2
    }
    
  2. 刷新间隔调整

    PUT /high_write_index/_settings
    {
     "index.refresh_interval": "30s"
    }
    
  3. 合并分段

    POST /fragmented_index/_forcemerge?max_num_segments=1
    

6.3 索引恢复技巧

从快照恢复:

POST /_snapshot/my_backup/snapshot_2023/_restore
{
  "indices": "important_index",
  "rename_pattern": "important_index",
  "rename_replacement": "restored_important_index"
}

七、总结

本文详细介绍了ElasticSearch索引的核心操作:

  1. 查看索引:使用_cat/indices_mapping等API获取索引信息
  2. 创建索引:通过PUT请求创建,可配置分片、映射等参数
  3. 删除索引:使用DELETE命令,需谨慎操作并做好备份
  4. 高级管理:别名、ILM策略等提升索引管理效率

实际生产环境中,建议: - 为索引设计合理的命名规范 - 使用模板和别名简化管理 - 实施完善的备份策略 - 监控索引健康状况

通过合理运用这些索引管理技术,可以构建高效、稳定的ElasticSearch搜索系统。 “`

推荐阅读:
  1. MySQL之表的创建、删除、修改、删除、查看及索引
  2. Elasticsearch根据条件进行删除索引命令

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elasticsearch

上一篇:RAC创建ACFS用来部署OGG的过程

下一篇:MySQL添加索引过程中出现waiting for table metadata lock怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》