轉自:https://blog.csdn.net/jiao_fuyou/article/details/50511255html
根據Elasticsearch中文社區《ES冷熱分離(讀寫分離) hot, stale 場景》一篇整理的。node
寫一個定時任務天天凌晨將前一天的索引標記爲stalemysql
PUT /index_name/_settings
{
"index.routing.allocation.include.zone" : "stale" }
這樣舊索引數據會自動遷移到stale集羣上sql
詳細狀況參考:
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/modules-cluster.html
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/cluster-reroute.htmlbash
上面的規則有時並不能徹底的控制複本必定在某個集羣上,它只能儘可能保證主分片和副本不在一個集羣上,這個時候一樣也能夠起個定時任務,手動的將副本reroute到集羣節點上elasticsearch
POST /_cluster/reroute
{
"commands" : [ { "move" : { "index" : "test", "shard" : 0, "from_node" : "node1", "to_node" : "node2" } }, { "allocate" : { "index" : "test", "shard" : 1, "node" : "node3" } } ] }
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/modules-cluster.html
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/cluster-reroute.htmlide
上面兩種方法將冷熱,讀寫數據都分佈到了不一樣的集羣上,下面看看如何在指定查詢參數,只查詢stale集羣節點上的數據。ui
POST /_search?preference=_only_nodes:zone:stale
{
"query": { "match": { "title": "elasticsearch" } } }
可是須要注意一點的是:由於es的replica也是一個實際須要cpu和io的indexing過程,並且indexing自己也有要求要寫夠必定副本數來纔算寫入完成。因此,你即便只請求replica,也是有可能影響到寫入的。.net
因此,你只能開啓一個副本——寫入副本數要求是從二個副本開始纔有。