Elasticsearch之Template详解


在ES中我们可以通过设置 Index TemplateDynamic Template来更好的为我们管理和设置索引和mapping。

一、Index Template

比如一个我们需要使用ES来做日志管理,我们都知道日志的数据量是十分庞大的,如果使用单个索引来保存所有日志数据的话,可能会存在一些性能问题。我们可以通过按天或月来自动生成index,这时候我们就可以用到IndexTemplate,可以为索引和ES集群提供更好的性能。

Index Template:可以帮助你设定Mapping和Setting,并按照一定的规则,自动匹配到新创建的索引上。

  • Template仅在一个索引被创建时使用,修改template并不会对已创建的索引造成影响。
  • 可以设定多个索引模板,多个模板的特性会被合并在一起
  • 可以指定order的数值,来控制合并的过程。

当一个索引被创建时,所引用的template规则如下:

  1. 应用ES默认的settings和mappings
  2. 先应用order数值较低的index template中的设定
  3. 如果不同的index template中有相同的属性设置,会应用order高的index template,也就是低的会被覆盖
  4. 创建索引时,如果用户指定了settings和mappings的某些属性,则这些属性会以用户设定的为准,没有被指定到的属性,会以匹配的template为准。

示例1

比如,我们现在来创建又给默认模板,如果所创建的索引没有指定分片的话,就以当前模板配置为准。

put _template/template_default
{
  "index_patterns":["*"],
  "order":0,
  "version":1,
  "settings":{
    "number_of_shards":1,
    "number_of_replicas":1
  }
}

然后我们不指定分片信息来创建一个索引,并获取索引的信息。

put template_index
--------------------
get template_index
--------------------
{
  "template_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "template_index",
        "creation_date" : "1640524317827",
        "number_of_replicas" : "1",
        "uuid" : "e5zbL0_lQUyYWFkDqmx5xw",
        "version" : {
          "created" : "7160099"
        }
      }
    }
  }
}

可以看到索引已经按照我们设定的默认模板,来自动生成了分片的配置。

示例2

再创建一个模板,匹配规则为:索引的名称以template开头的,设定分片数量为1,副本数量为3。
date_detection:mapping设置,如果字符串符合日期的类型,就自动匹配为日期,true为开启,false为关闭
numeric_detection:mapping设置,如果字符串是数字的话,自动映射数字类型。

put _template/template_test
{
  "index_patterns":["template*"],
  "order":1,
  "settings":{
    "number_of_shards":1,
    "number_of_replicas":3
  },
  "mappings":{
    "date_detection":false,
    "numeric_detection":true
  }
}

我们创建一个匹配通配规则的索引,获取信息。可以看到如下,匹配到了template_test,因为template_default的order为0,所以以template_test的分片副本设置为准。

put template_index2
---------------------
get template_index2
---------------------
{
  "template_index2" : {
    "aliases" : { },
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "template_index2",
        "creation_date" : "1640524532090",
        "number_of_replicas" : "3",
        "uuid" : "QsP_eZuhQKWiaGvF_6ux4A",
        "version" : {
          "created" : "7160099"
        }
      }
    }
  }
}

我们来插入一个文档,再获取mapping,看看ES是否自动将日期和数字做了转换。

put template_index2/_doc/1
{
  "date":"2021-12-01T00:00:00.000Z",
  "num":"10000"
}
-----------------------------------
{
  "template_index2" : {
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "date" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "num" : {
          "type" : "long"
        }
      }
    }
  }
}

可以看到,由于我们设置的 "date_detection":false,"numeric_detection":true。所以就算匹配到了date类型也会给到text的字段类型,数值类型被匹配到了long类型。

示例3

我们再创建一个匹配模板的索引,指定setting的分片和副本数量,看看是否会以我们设定的为准。

put template_index3
{
    "settings":{
    "number_of_shards":2,
    "number_of_replicas":2
  }
}
-----------------
{
  "template_index3" : {
    "aliases" : { },
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "template_index3",
        "creation_date" : "1640524899092",
        "number_of_replicas" : "2",
        "uuid" : "nW7wNjmmQaqjgasWG8_7zQ",
        "version" : {
          "created" : "7160099"
        }
      }
    }
  }
}

由索引的mapping信息可以看到,当前这个索引时匹配到了template_test这个模板的,但是由于我们在创建索引的时候指定了分片和副本的信息,所以以我们指定的为准。

二、Dynamic Template

上面我们讲了Index Template,主要是在创建索引的时候,来根据template的统配规则,确定当前创建的索引是否符合,从而将template里面的配置信息应用在我们新创建的索引中,通常应用于要生成相同配置的索引场景下,比如日志数据管理、统一索引管理等。

Dynamic Template主要是应用于具体的索引中去的,定义在某个索引的mapping设置中,会根据我们设定的数据类型,匹配一些设定的规则,来动态设定字段类型。

比如:字符串全匹配Keyword、is开头的字段设置为boolean,long_开头的字段设置为long等等…都可以由我们来设置模板动态匹配,为匹配到的字段自定设置我们自定义的字段类型。

示例1

比如我们创建一个索引,需要将索引中的字段name中的子字段给copy_to到一个新字段full_name以供查询,但是name中的address属性是不需要的。

put template_mapping
{
  "mappings":{
    "dynamic_templates":[
      {
        "full_name":{
          "path_match":"name.*",
          "path_unmatch":"*.address",
          "mapping":{
            "type":"text",
            "copy_to":"full_name"
          }
        }
      }
    ]
  }
}

然后我们插入一个文档

put template_mapping/_doc/1
{
  "name":{
    "first":"程",
    "middle":"大",
    "last":"帅",
    "address":"上海市汤臣一品"
  }
}

获取下ES根据我们设定的Dynamic Template自动生成的mapping。

get template_mapping/_mapping
---------------------------
{
  "template_mapping" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "full_name" : {
            "path_match" : "name.*",
            "path_unmatch" : "*.address",
            "mapping" : {
              "copy_to" : "full_name",
              "type" : "text"
            }
          }
        }
      ],
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "full_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "properties" : {
            "address" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "first" : {
              "type" : "text",
              "copy_to" : [
                "full_name"
              ]
            },
            "last" : {
              "type" : "text",
              "copy_to" : [
                "full_name"
              ]
            },
            "middle" : {
              "type" : "text",
              "copy_to" : [
                "full_name"
              ]
            }
          }
        }
      }
    }
  }
}

同时因为fulfill_name是由name.*中的数据copy_to而来的,所以我们可以对其进行搜索,但是在文档的_srouce中,不存在这个字段。

get template_mapping/_search
{
  "query":{
    "match":{
      "full_name": "程"
    }
  }
}
------------------------
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "template_mapping",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : {
            "first" : "程",
            "middle" : "大",
            "last" : "帅",
            "address" : "上海市汤臣一品"
          }
        }
      }
    ]
  }
}

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Prometheus是一种一站式监控告警平台,它支持对云或容器的监控,并且具有功能齐全、依赖少的特点。相比其他系统主要对主机监控,Prometheus具有更强大的数据查询语句表现力和内置统计函数。然而,在数据存储扩展性和持久性方面,Prometheus不如InfluxDB、OpenTSDB和Sensu好。\[1\] 要对elasticsearch进行监控,可以使用基于事件的触发来修改指标的值。通常,这些指标需求来自业务方面,例如自研的应用需要将相关指标暴露给Prometheus进行监控和展示。在自研应用的代码中嵌入指标采集的代码(指标定义和设置值),可以实现这一目的。\[2\] 此外,任何遵循Prometheus数据格式并提供监控指标的程序都可以称为Exporter。在Prometheus社区中,有许多可供选择的Exporter,如node_exporter。这些Exporter可以帮助我们监控各种不同的系统和服务。\[3\] 综上所述,要详细了解Prometheus对elasticsearch的监控指标,可以使用基于事件的触发来修改指标的值,并使用适当的Exporter来收集和展示这些指标。 #### 引用[.reference_title] - *1* [prometheus 监控概述](https://blog.csdn.net/WuDan_1112/article/details/126074566)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Prometheus监控实战之exporter详解](https://blog.csdn.net/ygq13572549874/article/details/129114047)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程大帅气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值