ES7中的alias和rollover

个人学习笔记,谢绝转载!!!

原文:https://www.cnblogs.com/wshenjin/p/15160383.html


为索引设置别名后,就可以通过这个别名来操作索引,ES会自动将别名映射到实际的索引名中。

alias的基本语法:

给现有index添加alias:

PUT /_alias { 
    "actions" : [ 
        {
            "add" : {
                "index" : "students",
                "alias" : "students_alias",
                "is_write_index" : true
           }
        }
    ]
}

创建index并设置alias

PUT /students {
    "settings" : {
        "number_of_shards":3 ,
        "number_of_replicas":1
    },
    "aliases" : {
      "students_alias" : {
        "is_write_index" : true
      }
    }
}

删除alias

POST /_aliases {
    "actions" : [
        {
            "remove" : {
                 "index" : "context01",
                 "alias" : "context_alias"
            }
        },
    ]
}

关联多个索引:

POST /_aliases {
    "actions" : [
        {
            "add" : {
                 "index" : "context01",
                 "alias" : "context_alias"
            }
        },
        {
            "add" : {
                 "index" : "context02",
                 "alias" : "context_alias",
                 "is_write_index" : true
            }
        }
    ]
}

别名不仅仅可以关联一个索引,它能聚合多个索引

上面的别名context_alias聚合了context01和 context02,这样对context_alias的读操作,会操作context01和 context02。

因为设置了参数is_write_index,所以对context_alias的写操作就是操作context02。但无法操作context01

Rollover

手动Rollover

新建索引01,并设置alias

PUT  /context01 {
    "settings" : {
        "number_of_shards":3 ,
        "number_of_replicas":1
    },
    "aliases" : {
      "context_alias" : {
        "is_write_index" : true
      }
    }
}

通过alias, 向索引中写入数据

PUT /context_alias/_doc/0001 {
   "body":"hello world"
}

创建新索引02

PUT /context02 {
    "settings" : {
        "number_of_shards":3 ,
        "number_of_replicas":1
    }
}

手动Rollover,alias移除context01,指向context02

POST /_aliases {
    "actions" : [
        {
            "remove" : {
                 "index" : "context01",
                 "alias" : "context_alias"
            }
        },
        {
            "add" : {
                 "index" : "context02",
                 "alias" : "context_alias",
                 "is_write_index" : true
            }
        }
    ]
}
自动Rollover

条件是达到5个docs,就rollover到context03

POST /context_alias/_rollover/context03 {
    "conditions": {
        "max_docs":  5
    },
    "settings" : {
        "index.number_of_shards":3 ,
        "index.number_of_replicas":1
    }
}'

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "old_index" : "context02",
  "new_index" : "context03",
  "rolled_over" : true,
  "dry_run" : false,
  "conditions" : {
    "[max_docs: 5]" : true
  }
}

触发了max_docs达到5的条件rollover成功,自动创建新的索引并将alias指向了新的索引。

posted @ 2021-08-19 10:07  wshenJin  阅读(1176)  评论(0编辑  收藏  举报