Elasticsearch索引自动套用模板

方案选择:

方案一:可对logstash配置output参数:

 如下所示:

 这种方案在logstash中指定模板文件,由logstash将template写入ES集群;

方案二:直接将template写入ES集群

通过ES提供的API,将JSON格式的template写入目标ES集群的_template路径,对新生成的所有符合过滤规则的索引直接套用该模板。

模板的结构大致分四块吧:

第一部分:通用设置,主要是模板匹配索引的过滤规则,影响该模板对哪些索引生效;

第二部分:settings:配置索引的公共参数,比如索引的replicas,以及分片数shards等参数;

第三部分:mappings:最重要的一部分,在这部分中配置每个type下的每个field的相关属性,比如field类型(string,long,date等等),是否分词,是否在内存中缓存等等属性都在这部分配置;

第四部分:aliases:索引别名,索引别名可用在索引数据迁移等用途上。

典型的一个template如下所示:

复制代码
{
        "template": "ld.log-*",
        "order":0,
        "settings": {
            "index.number_of_replicas": "1",
            "index.number_of_shards": "5"
        },
        "mappings": {
            "logs": {
                "properties": {
                    "@timestamp": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "@version": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "Exp": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "Guid": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "LogLevel": {
                        "type": "long"
                    },
                    "LogTime": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "LoggerName": {
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "Message": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "ProcessId": {
                        "type": "long"
                    },
                    "StackTrace": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "ThreadId": {
                        "type": "long"
                    },
                    "exp": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "logLevel": {
                        "type": "long"
                    },
                    "logTime": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "loggerName": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "message": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "processId": {
                        "type": "long"
                    },
                    "tags": {
                        "doc_values": true,
                        "index": "not_analyzed",
                        "type": "string"
                    },
                    "threadId": {
                        "type": "long"
                    }
                }
            }
        },
        "aliases": {
        }
}
复制代码

在这个JSON中可以清楚地看到四个部分,并且对string类型的fields设置了不进行默认分词信息。

posted @   fat_girl_spring  阅读(216)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示