第四章:索引管理

学习如何创建、修改、删除索引,以及索引的别名管理和动态配置。

最后更新: 2024-01-15
页面目录

第四章:索引管理

4.1 创建索引

4.1.1 基本创建

PUT /my-index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "created_at": { "type": "date" }
    }
  }
}

4.1.2 完整配置示例

PUT /blog-articles
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 2,
      "refresh_interval": "5s",
      "max_result_window": 50000,
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "type": "custom",
            "tokenizer": "ik_max_word",
            "filter": ["lowercase", "stop"]
          }
        }
      }
    },
    "index.blocks.read_only_allow_delete": "false"
  },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "id": { "type": "long" },
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": { "type": "keyword" }
        }
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "author": {
        "type": "object",
        "properties": {
          "name": { "type": "keyword" },
          "email": { "type": "keyword" }
        }
      },
      "tags": { "type": "keyword" },
      "views": { "type": "long" },
      "published_at": { "type": "date" }
    }
  }
}

4.2 索引配置详解

4.2.1 分片配置

参数 说明 默认值
number_of_shards 主分片数 1
number_of_replicas 副本数 1
shard.check_on_startup 启动时检查分片 false

4.2.2 刷新配置

参数 说明 默认值
refresh_interval 刷新间隔 1s
index.refresh_interval 索引级别刷新 1s

4.2.3 动态参数

PUT /my-index/_settings
{
  "index": {
    "number_of_replicas": 2,
    "refresh_interval": "2s",
    "max_result_window": 100000
  }
}

4.3 索引别名

4.3.1 创建别名

# 创建索引时指定别名
PUT /products-v1
{
  "aliases": {
    "products": {}
  }
}

# 后期添加别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "products-v2",
        "alias": "products"
      }
    }
  ]
}

4.3.2 别名操作

# 查看索引别名
GET /products/_alias

# 查看别名指向的索引
GET /_alias/products

# 删除别名
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "products-v1",
        "alias": "products"
      }
    }
  ]
}

4.3.3 别名路由

# 带过滤器的别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "products",
        "alias": "products-electronics",
        "filter": {
          "term": { "category": "electronics" }
        }
      }
    }
  ]
}

# 带路由的别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "products",
        "alias": "products-user-123",
        "routing": "123"
      }
    }
  ]
}

4.3.4 零停机方案

# 场景:从 v1 迁移到 v2

# 1. 创建新索引 v2
PUT /products-v2

# 2. 同时指向旧索引和新索引
POST /_aliases
{
  "actions": [
    { "remove": { "index": "products-v1", "alias": "products" } },
    { "add":    { "index": "products-v2", "alias": "products" } }
  ]
}

4.4 索引模板

4.4.1 创建模板

PUT /_index_template/logs-template
{
  "index_patterns": ["logs-*"],
  "priority": 100,
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "index.lifecycle.name": "logs-policy"
    },
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" },
        "level": { "type": "keyword" },
        "message": { "type": "text" },
        "service": { "type": "keyword" },
        "host": { "type": "ip" }
      }
    }
  }
}

4.4.2 组件模板

# 创建共享设置模板
PUT /_index_template/common-settings
{
  "index_patterns": ["*"],
  "template": {
    "settings": {
      "number_of_replicas": 1,
      "refresh_interval": "5s"
    }
  }
}

# 创建日志映射模板
PUT /_index_template/logs-mappings
{
  "index_patterns": ["logs-*"],
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" },
        "message": { "type": "text" }
      }
    }
  }
}

4.5 查看索引信息

4.5.1 查看索引配置

# 查看索引设置
GET /products/_settings

# 查看索引映射
GET /products/_mapping

# 查看索引统计
GET /products/_stats

4.5.2 查看集群索引列表

# 查看所有索引
GET /_cat/indices?v

# 查看索引大小
GET /_cat/indices?v&h=index,docs.count,store.size,pri,rep

# 查看健康状态
GET /_cat/indices?v&health=status

4.6 修改索引

4.6.1 修改副本数

# 动态修改副本数
PUT /products/_settings
{
  "index": {
    "number_of_replicas": 2
  }
}

4.6.2 修改刷新间隔

# 临时禁用刷新(批量导入时)
PUT /products/_settings
{
  "index": {
    "refresh_interval": "-1"
  }
}

# 恢复正常
PUT /products/_settings
{
  "index": {
    "refresh_interval": "1s"
  }
}

4.6.3 只读模式

# 设置只读
PUT /products/_settings
{
  "index.blocks.read_only_allow_delete": true
}

# 解除只读
PUT /products/_settings
{
  "index.blocks.read_only_allow_delete": false
}

4.7 删除索引

4.7.1 基本删除

# 删除单个索引
DELETE /products

# 删除多个索引
DELETE /products,orders

# 删除符合模式的索引
DELETE /logs-*

# 使用通配符(需确认)
DELETE /logs-2024.*

4.7.2 安全删除

# 删除前检查
GET /_cat/indices/logs-*?v

# 确认后删除
DELETE /logs-*

4.8 索引收缩(Shrink)

4.8.1 收缩索引

# 收缩前先同步副本
POST /logs/_flush/synced

# 将 6 分片收缩为 1 分片
POST /logs/_shrink/logs-shrunk
{
  "settings": {
    "index.number_of_shards": 1,
    "index.number_of_replicas": 1
  }
}

4.9 索引滚动(Rollup)

4.9.1 创建滚动索引

PUT /logs-rollup
{
  "settings": {
    "index.rollover_alias": "logs-rollup"
  },
  "mappings": {
    "properties": {
      "@timestamp": { "type": "date" },
      "message": { "type": "text" }
    }
  }
}

# 触发滚动
POST /logs-rollup/_rollover
{
  "conditions": {
    "max_age": "7d",
    "max_docs": 100000,
    "max_size": "50gb"
  }
}

4.10 索引清理

4.10.1 手动刷新

# 刷新单个索引
POST /products/_refresh

# 刷新所有索引
POST /_refresh

4.10.2 强制合并

# 合并分片(减少段数量)
POST /products/_forcemerge
{
  "max_num_segments": 1,
  "only_expunge_deletes": true
}

4.10.3 清理删除的文档

# 仅清理已删除文档
POST /products/_forcemerge
{
  "only_expunge_deletes": true
}

4.11 常见操作汇总

操作 命令
创建索引 PUT /index-name
删除索引 DELETE /index-name
查看索引 GET /index-name
检查存在 HEAD /index-name
打开索引 POST /index-name/_open
关闭索引 POST /index-name/_close
添加别名 POST /_aliases
修改设置 PUT /index-name/_settings

4.12 总结

本章详细介绍了索引的各种管理操作,包括创建、配置、别名管理、模板、修改和删除等。合理使用索引别名和模板是实现零停机部署的关键。下一章将学习文档的 CRUD 操作。