官网
##
https://www.elastic.co/guide/en/elasticsearch/reference/master/search-request-body.html#request-body-search-preference?q=preference
目的
es冷热数据分离目的是为了节省成本。如下图:
对于开发人员而言即数据的冷热分离,实现此功能有2个前提条件:
硬件:
- 处理速度不同的硬件,最起码有读写速度不同的硬盘,如SSD、机械硬盘HDD。 软件配置:可以配置
- 不同的数据存储在不同的硬盘,如近期数据存储在SSD,较远历史数据存储在sata。
elasticsearch 的冷热分离配置主要依赖于分片分布规则设置。
冷热分离实践
step1: 划分冷热节点
node.tag: hot
node.tag: cold
node.max_local_storage_nodes: 2 #允许每个机器启动两个es进程(可选)
step2:按时间规律等建索引。比如按天、按周建索引
索引模板logstash:所有 logstash* 的索引匹配的模板。
PUT /_template/logstash
{
"order": 0,
"template": "logstash*",
"settings": {
"index.routing.allocation.include.tag": "hot",
"index.refresh_interval": "30s",
"index.number_of_replicas": "1",
"index.number_of_shards": "1",
"index.translog.flush_threshold_ops": "30000"
}
}
“index.routing.allocation.include.tag”: “hot“: 表示新建索引将分配到 node.tag = hot 的节点下
step3: 定时任务将历史索引分配到 cold 节点下。最新索引保存在hot节点,历史索引定时保存到cold节点。
自己写脚本,将历史索引标记为stale。
PUT /index_name/_settings
{
"index.routing.allocation.include.tag" : "cold"
}
这样旧索引数据会自动迁移到cold集群上。
冷热数据查询
GET /_search?preference=xyzabc123
{
"query": {
"match": {
"title": "elasticsearch"
}
}
}
更多推荐