[TOC]html
基于es 5.4和5.6,参考两份资料,《从Lucene到Elasticsearch全文检索实战》和官方文档node
https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices.html (官方文档至关精彩,不容错过!)。app
PUT my_index
Note1:索引不能有大写字母;elasticsearch
Note2:es默认给索引设置5个分片1个副本;ide
NOte3:索引分片数一经指定后不能再修改,但副本数能够经过命令随时修改;ui
能够添加settings配置:code
PUT my_index { "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }
PUT my_index/_settings { "number_of_replicas": 2 }
权限参数以下:htm
参数设置 | 说明 |
---|---|
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 }
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
DELETE my_index
若是删除的索引不存在,会报索引未找到异常。
索引关闭之后就几乎不会占用系统资源。
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
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"} }
直接参考官方文档: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
, 2
or 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:
收缩索引前的准备:
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": {} } }
若是不太理解的话,就必定要好好阅读上面提供的官方文档连接。
建立索引别名:
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