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 @ 2021-01-06 15:03  fat_girl_spring  阅读(208)  评论(0编辑  收藏  举报