elasticsearch索引按日期拆分

1.索引拆分原因

如果单个索引数据量过大会导致搜索变慢,而且不方便清理历史数据。

例如日志数据每天量很大,而且需要定期清理以往日志数据。例如原索引为sc_all_system_log,现按天拆分索引sc_all_system_log20220902,sc_all_system_log20220903,sc_all_system_log20220904,并且定期清理五天前索引。

实现最终效果

2022.09.03日志数据存入sc_all_system_log20220903

2022.09.04日志数据存入sc_all_system_log20220904

数据虽然存入了不同的索引中,但是搜索却指向一个索引搜索

实现方法

一个索引名称搜索出多个索引数据,其实实现原理很简单就是利用索引别名,不同的索引可以指向同一个索引别名,我们搜索时只要搜索索引别名即可。

具体实现方法,按照下面顺序看下去

定时清理策略

利用ES的索引生命周期,清理几天前索引,所以创建时间超过七天之后会自动删除

创建策略,后面会用到。

c 复制代码
PUT _ilm/policy/policy_cktest_sc_system_log
{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}

创建索引模板

创建索引模板,索引模板主要是用来创建索引默认属性

c 复制代码
PUT _template/sc_all_system_log
{
    "order":0,
    "index_patterns":[
        // 创建索引时,索引名称以这个为前缀时,默认使用此模板
        "sc_all_system_log*"
    ],
    "settings":{
        "index":{
			"refresh_interval": "10s",
            "number_of_shards":"5",
            "number_of_replicas":"1",
			"lifecycle":{
				// 前面创建的索引定时清理策略,创建的索引会使用此清理策略
				"name":"policy_cktest_sc_system_log"
			}
        }
    },
    "mappings":{
        "_doc":{
            "properties":{
                "version":{
					"type":"keyword",
					"index":"false"
                },
				"timestamp":{
					"type" : "date",
					"format" : "8uuuu-MM-dd HH:mm:ss.SSS"
                },
				"message":{
					"type":"text",
					"analyzer":"ik_smart",
					"search_analyzer":"ik_smart"
                },
				"level":{
					"type":"keyword"
                },
				"namespace":{
					"type":"keyword"
                },
				"appName":{
					"type":"keyword"
                },
				"traceId":{
					"type":"keyword"
                },
				"spanId":{
					"type":"keyword"
                },
				"ip":{
					"type":"keyword"
                },
				"tags":{
					"type":"object"
                }
            }
        }
    },
    "aliases":{
    	// 创建索引时指定的别名,很重要
        "sc_all_system_log":{}
    }
}

插入数据自动创建索引

这里我们指定的索引名称sc_all_system_log20220903,无此索引时会自动创建索引,创建索引时发现是以sc_all_system_log为前缀会默认使用上面的模板创建。所以索引sc_all_system_log20220903指向的别名是sc_all_system_log

c 复制代码
// 2022.09.03插入一条记录,创建sc_all_system_log20220903索引
POST /sc_all_system_log20220903/_doc
{
  "version":"1",
  "timestamp":"2022-09-03 17:50:00.000",
  "message":"程序异常请联系管理员处理",
  "level":"info",
  "namespace":"sc-test",
  "appName":"sc-test",
  "traceId":"123456789",
  "spanId":"123456789",
  "ip":"127.0.0.1",
  "tags":{
    "key1":"value1",
    "key2":"value2",
    "key3":"value3"
  }
}
// 2022.09.04插入一条记录,创建sc_all_system_log20220904索引
POST /sc_all_system_log20220904/_doc
{
  "version":"1",
  "timestamp":"2022-09-03 17:50:00.000",
  "message":"程序异常请联系管理员处理,测试4",
  "level":"info",
  "namespace":"sc-test",
  "appName":"sc-test",
  "traceId":"123456789",
  "spanId":"123456789",
  "ip":"127.0.0.1",
  "tags":{
    "key1":"value1",
    "key2":"value2",
    "key3":"value3"
  }
}

搜索时,我们只需要指向别名(sc_all_system_log)搜索即可,如下图

相关推荐
Hello.Reader7 小时前
Go-Elasticsearch v9 安装与版本兼容性
elasticsearch·golang·jenkins
mykyle9 小时前
Elasticsearch-ik分析器
大数据·elasticsearch·jenkins
Penge66618 小时前
Elasticsearch深度分页解决方案
elasticsearch
Penge66619 小时前
Elasticsearch match_phrase 查询 slop 参数详解文档
elasticsearch
Penge66619 小时前
Elasticsearch 中的 copy_to:一文掌握字段合并搜索的利器
elasticsearch
mykyle2 天前
Elasticsearch-8.17.0 centos7安装
大数据·elasticsearch·jenkins
躲在云朵里`2 天前
Git的使用
大数据·git·elasticsearch
Elasticsearch2 天前
Elastic 劳动力的生成式 AI:ElasticGPT 的幕后解析
elasticsearch
kong@react2 天前
docker安装 Elasticsearch、Kibana、IK 分词器
elasticsearch·docker·jenkins
Elasticsearch2 天前
LlamaIndex 和 Elasticsearch Rerankers:无与伦比的简洁
elasticsearch