Bootstrap

Elasticsearch Index Management 索引管理

Elasticsearch search scroll 游标查询,内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,英文内容来自 Elasticsearch: The Definitive Guide [2.x]

为什么要手动创建索引?

... you start wanting to fine-tune the indexing and search process to better suit your particular use case.

虽然 Elasticsearch 默认的动态索引可以很方便的使用,但是如果你希望根据业务需求对索引和查询的过程进行调优,那么就需要考虑进行索引管理。

创建索引 Creating an Index

... want to ensure that the index has been created with the appropriate number of primary shards, and that analyzers and mappings are set up before we index any data.

创建索引的语法

PUT /my_index
{
    "settings": { ... any settings ... },
    "mappings": {
        "type_one": { ... any mappings ... },
        "type_two": { ... any mappings ... },
        ...
    }
}

number_of_shards: The number of primary shards, this setting cannot be changed after index creation.

number_of_replicas: The number of replica shards (copies) that each primary shard should have. This setting can be changed at any time on a live index.

可以在配置文件 中禁止自动创建索引:

action.auto_create_index: false

创建索引的示例

PUT /my_index
{
  // one primary shard and no replica shards
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "my_type": {
      "properties": {
        "my_field": {
          "type": "text"
        }
      }
    }
  }
}
修改索引

// change the number of replica
PUT /my_index/_settings
{
    "number_of_replicas": 1
}
删除索引 Deleting an Index

To delete an index:

DELETE /my_index

Delete multiple indices:

DELETE /index_one,index_two
DELETE /index_*

Delete all indices

DELETE /_all
DELETE /*

为了防止“删除跑路”,eliminate the possibility an accidental mass-deletion,可以在配置文件 中设置:

action.destructive_requires_name: true

禁用 _all 和 通配符,避免误删。