Elasticsearch索引操作 #

一、创建索引 #

1.1 基本创建 #

bash
PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}

响应:

json
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

1.2 完整创建 #

bash
PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1,
    "index": {
      "max_result_window": 10000,
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["lowercase", "stop"]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "standard"
      },
      "price": {
        "type": "scaled_float",
        "scaling_factor": 100
      },
      "brand": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  },
  "aliases": {
    "product_search": {}
  }
}

1.3 索引设置参数 #

参数 说明 默认值
number_of_shards 主分片数量 1
number_of_replicas 副本数量 1
max_result_window 最大返回结果数 10000
refresh_interval 刷新间隔 1s
analysis 分析器配置 -

二、查看索引 #

2.1 查看索引信息 #

bash
GET /products

响应:

json
{
  "products": {
    "aliases": {},
    "mappings": {
      "properties": {
        "name": { "type": "text" },
        "price": { "type": "float" }
      }
    },
    "settings": {
      "index": {
        "number_of_shards": "3",
        "number_of_replicas": "1"
      }
    }
  }
}

2.2 查看索引设置 #

bash
GET /products/_settings

2.3 查看索引映射 #

bash
GET /products/_mapping

2.4 查看多个索引 #

bash
GET /products,orders/_settings

GET /logs-*/_mapping

2.5 查看所有索引 #

bash
GET /_all

GET /_cat/indices?v

三、修改索引 #

3.1 更新设置 #

bash
PUT /products/_settings
{
  "number_of_replicas": 2,
  "refresh_interval": "30s"
}

注意number_of_shards 创建后不可修改。

3.2 更新映射 #

bash
PUT /products/_mapping
{
  "properties": {
    "description": {
      "type": "text"
    },
    "category": {
      "type": "keyword"
    }
  }
}

注意

  • 可以添加新字段
  • 已有字段类型不可修改
  • 可以添加multi-fields

3.3 添加别名 #

bash
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "products",
        "alias": "product_search"
      }
    }
  ]
}

四、删除索引 #

4.1 删除单个索引 #

bash
DELETE /products

4.2 删除多个索引 #

bash
DELETE /index1,index2

DELETE /logs-2024-*

4.3 删除所有索引 #

bash
DELETE /_all

DELETE /*

安全建议:在 elasticsearch.yml 中禁用:

yaml
action.destructive_requires_name: true

五、索引打开与关闭 #

5.1 关闭索引 #

bash
POST /products/_close

关闭后:

  • 索引数据保留
  • 无法读写
  • 不占用集群资源

5.2 打开索引 #

bash
POST /products/_open

5.3 查看索引状态 #

bash
GET /_cat/indices?v&status=closed

六、索引统计 #

6.1 索引统计信息 #

bash
GET /products/_stats

6.2 索引段信息 #

bash
GET /products/_segments

6.3 索引恢复信息 #

bash
GET /products/_recovery

6.4 索引分片存储 #

bash
GET /products/_shard_stores

七、索引克隆 #

7.1 克隆前准备 #

bash
PUT /products/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

7.2 执行克隆 #

bash
POST /products/_clone/products_clone
{
  "settings": {
    "index.blocks.write": null
  },
  "aliases": {
    "products_read": {}
  }
}

7.3 恢复原索引写入 #

bash
PUT /products/_settings
{
  "settings": {
    "index.blocks.write": false
  }
}

八、索引拆分 #

8.1 准备拆分 #

bash
PUT /logs-small/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

8.2 执行拆分 #

bash
POST /logs-small/_split/logs-large
{
  "settings": {
    "index.number_of_shards": 6
  }
}

拆分规则

  • 新分片数必须是原分片数的倍数
  • 原索引必须只读

九、索引收缩 #

9.1 准备收缩 #

bash
PUT /logs-large/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

9.2 执行收缩 #

bash
POST /logs-large/_shrink/logs-small
{
  "settings": {
    "index.number_of_shards": 1,
    "index.number_of_replicas": 1,
    "index.blocks.write": null
  }
}

收缩规则

  • 新分片数必须是原分片数的因数
  • 原索引必须只读
  • 所有分片必须在同一节点

十、索引滚动 #

10.1 创建滚动别名 #

bash
PUT /logs-000001
{
  "aliases": {
    "logs_write": {}
  }
}

10.2 配置滚动策略 #

bash
POST /logs_write/_rollover
{
  "conditions": {
    "max_age": "7d",
    "max_docs": 1000000,
    "max_size": "50gb"
  }
}

响应:

json
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "old_index": "logs-000001",
  "new_index": "logs-000002",
  "rolled_over": true,
  "dry_run": false,
  "conditions": {
    "max_age": "7d",
    "max_docs": 1000000
  }
}

10.3 自动滚动配置 #

配合索引生命周期管理(ILM)使用。

十一、索引刷新 #

11.1 手动刷新 #

bash
POST /products/_refresh

11.2 刷新所有索引 #

bash
POST /_refresh

11.3 刷新间隔设置 #

bash
PUT /products/_settings
{
  "index": {
    "refresh_interval": "30s"
  }
}

禁用自动刷新(批量导入时):

bash
PUT /products/_settings
{
  "index": {
    "refresh_interval": "-1"
  }
}

十二、索引刷新与持久化 #

12.1 Flush操作 #

bash
POST /products/_flush

12.2 Force Merge #

bash
POST /products/_forcemerge?max_num_segments=1

参数说明

  • max_num_segments:合并后的段数量
  • only_expunge_deletes:只清除已删除文档

12.3 Clear Cache #

bash
POST /products/_cache/clear

十三、索引模板应用 #

13.1 创建索引时应用模板 #

bash
PUT /_index_template/products_template
{
  "index_patterns": ["products-*"],
  "priority": 100,
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" }
      }
    }
  }
}

13.2 查看匹配的模板 #

bash
GET /_index_template/products_template

十四、最佳实践 #

14.1 分片规划 #

text
分片规划建议
├── 单分片大小
│   └── 10GB - 50GB
├── 每节点分片数
│   └── 不超过 20 * 堆内存(GB)
├── 主分片数量
│   └── 根据数据量和节点数规划
└── 副本数量
    └── 生产环境至少1个

14.2 索引命名规范 #

text
命名建议
├── 使用小写字母
├── 使用连字符分隔
├── 包含时间信息(时间序列)
├── 包含业务标识
└── 示例
    ├── logs-nginx-2024-01-01
    ├── products-v1
    └── metrics-server-2024.01.01

14.3 索引生命周期 #

text
生命周期管理
├── 热阶段
│   ├── 高性能存储
│   └── 多副本
├── 温阶段
│   ├── 普通存储
│   └── 减少副本
├── 冷阶段
│   ├── 归档存储
│   └── 只读模式
└── 删除阶段
    └── 自动删除

十五、总结 #

本章介绍了Elasticsearch索引操作:

  1. 创建索引时规划好分片数量
  2. 映射可以添加字段,但不可修改已有字段类型
  3. 关闭索引可以节省资源
  4. 克隆、拆分、收缩用于索引调整
  5. 滚动索引用于时间序列数据
  6. 合理配置刷新间隔提升性能

下一步,我们将学习映射管理的详细内容。

最后更新:2026-03-27