千家信息网

ElasticSearch常用操作:索引篇

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,[TOC]0 说明基于es 5.4和5.6,参考两份资料,《从Lucene到Elasticsearch全文检索实战》和官方文档https://www.elastic.co/guide/en/elast
千家信息网最后更新 2026年11月03日ElasticSearch常用操作:索引篇

[TOC]


0 说明

基于es 5.4和5.6,参考两份资料,《从Lucene到Elasticsearch全文检索实战》和官方文档

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices.html (官方文档相当精彩,不容错过!)。

1 创建索引

PUT my_index

Note1:索引不能有大写字母;

Note2:es默认给索引设置5个分片1个副本;

NOte3:索引分片数一经指定后不能再修改,但副本数可以通过命令随时修改;

可以添加settings配置:

PUT my_index{  "settings": {    "number_of_shards": 3,    "number_of_replicas": 1  }}

2 更新索引副本数

PUT my_index/_settings{  "number_of_replicas": 2}

3 读写权限设置

权限参数如下:

参数设置说明
blocks.read_only:true为true时,设置当前索引只允许读不允许写或者更新
blocks.read:true为true时,禁止对当前索引进行读操作
blocks.write:true为true时,禁止对当前索引进行写操作

比如要禁止用户进行写操作:

PUT my_index/_settings{  "blocks.write": true}

再写入数据时,就会返回403错误。

恢复写操作:

PUT my_index/_settings{  "blocks.write": false}

4 查看索引

GET my_index/_mapping

返回结果:

{  "my_index": {    "mappings": {      "my_type": {        "properties": {          "title": {            "type": "text",            "fields": {              "keyword": {                "type": "keyword",                "ignore_above": 256              }            }          }        }      }    }  }}

同时查看多个索引的setting信息:

GET my_index,my_index2/_mapping

查看集群中所有索引的setting信息:

GET _all/_settings

5 删除索引

DELETE my_index

如果删除的索引不存在,会报索引未找到异常。

6 索引的打开与关闭

索引关闭以后就几乎不会占用系统资源。

POST my_index/_close

关闭多个索引:

POST my_index,my_index2/_close

加上ignore_unavailable参数:

POST my_index,my_index2,my_index3/_close?ignore_unavailable=true

my_index3是不存在的,如果不加ignore_unavailable参数,则会抛出索引不存在错误。

关闭集群中所有索引:

POST _all/_close

以能配符方式关闭索引,关闭以test开头的索引:

POST test*/_close

7 复制索引

POST _reindex{  "source":{"index":"my_index"},  "dest":{"index":"my_index3"}}

Note1:目标索引不会复制源索引中的配置信息,_redinx操作之前需要设置目标索引的分片数、副本数等信息,如果没有设置,或者说原来就不存在my_index3,那么会新创建一个索引,并且使用默认配置信息;

Note2:_reindex实际上是用来复制索引文档的,因此如果my_index中没有文档,那么是不会新创建my_index3的;

可以在source中增加type和query来限制复制的文档:

POST _reindex{  "source":{    "index":"my_index",    "type":"my_type",    "query":{      "term":{"title":"elasticsearch"}    }  },  "dest":{"index":"my_index3"}}

8 收缩索引

直接参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices-shrink-index.html,非常详细。

The shrink index API allows you to shrink an existing index into a new index with fewer primary shards. The requested number of primary shards in the target index must be a factor of the number of shards in the source index. For example an index with 8 primary shards can be shrunk into 4, 2or 1 primary shards or an index with 15 primary shards can be shrunk into 5, 3 or 1. If the number of shards in the index is a prime number it can only be shrunk into a single primary shard. Before shrinking, a (primary or replica) copy of every shard in the index must be present on the same node.

Shrinking works as follows:

  • First, it creates a new target index with the same definition as the source index, but with a smaller number of primary shards.
  • Then it hard-links segments from the source index into the target index. (If the file system doesn't support hard-linking, then all segments are copied into the new index, which is a much more time consuming process.)
  • Finally, it recovers the target index as though it were a closed index which had just been re-opened.

收缩索引前的准备:

PUT /my_source_index/_settings{  "settings": {    "index.routing.allocation.require._name": "shrink_node_name",     "index.blocks.write": true   }}

进行索引的收缩:

POST my_source_index/_shrink/my_target_index

也可以添加其它一些配置信息:

POST my_source_index/_shrink/my_target_index{  "settings": {    "index.number_of_replicas": 1,    "index.number_of_shards": 1,     "index.codec": "best_compression"   },  "aliases": {    "my_search_indices": {}  }}

如果不太理解的话,就一定要好好阅读上面提供的官方文档链接。

9 索引别名

创建索引别名:

POST _aliases{  "actions": [    {      "add": {        "index": "test1",        "alias": "alias1"      }    }  ]}

移除索引别名:

POST _aliases{  "actions": [    {      "remove": {        "index": "test1",        "alias": "alias1"      }    }  ]}

Note1:一个索引可以有多个别名(添加多次就可以了),一个别名也可以对应多个索引(使用多次就可以了);

Note2:在使用别名的时候需要注意,如果别名和索引是一对一的,使用别名索引或者根据ID查询文档是可以的,但是如果别名和索引是一对多的,使用别名会发生错误,因为Elasticsearch不知道把文档写入哪个索引中去或者从哪个索引中读取文档;

查看某一个索引的别名:

GET my_index3/_aliases结果:{  "my_index3": {    "aliases": {      "alias_test": {},      "alias_test2": {}    }  }}

查看一个别名所对应的索引:

GET alias_test/_aliases结果:{  "my_index3": {    "aliases": {      "alias_test": {},      "alias_test2": {}    }  },  "my_index2": {    "aliases": {      "alias_test": {}    }  },  "my_index": {    "aliases": {      "alias_test": {}    }  }}

查看集群上所有的可用别名:

GET _all/_aliases或GET _aliases
索引 别名 文档 信息 副本 参数 多个 官方 配置 结果 错误 集群 收缩 权限 目标 参考 更新 精彩 一对一 全文 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 瑰夏咖啡 中国互联网科技股 奇酷互联网络科技 丰台科技园 互联网 广西拼得多互联网科技有限公司 北京华百购互联网科技有限公司 上海宁丞科技互联网 新兴科技互联网创新大赛颁奖典礼 长沙茂晟互联网科技有限公司 南博望互联网科技有限公司 山东有哪些互联网科技公司 小米互联网科技园入驻 启辰互联网科技 产业互联网科技服务公司 互联网与数字科技关系 上海乐五互联网科技有限公司 广东鹅鹅鹅互联网科技有限公司 悟空问答互联网科技 杭州科技互联网公司有哪些 恒生科技互联网指数十大成分股 为想互联网科技怎么样 美国互联网科技巨头监管 科技制造互联网公司 互联网络科技有限公司 上海万想互联网科技 中国科技互联网公司收入 长沙互联网科技 求路客互联网科技电话 第三届互联网大会安防黑科技 上海旺乐尼互联网科技有限公司
0