[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