zoukankan      html  css  js  c++  java
  • ElasticSearch——自定义模板

    output中配置

    elasticsearch{
      action => "index"
      hosts => ["xxx"]
      index => "http-log-logstash"
      document_type => "logs"
      template => "opt/http-logstash.json"
      template_name => "http-log-logstash"
      template_overwrite => true
    }

    自定义模板示例

    { 
        "template" : "logstash-*",   -------------> 匹配的索引名字
        "order":1,  -------------> 代表权重,如果有多个模板的时候,优先进行匹配,值越大,权重越高
        "settings" : { "index.refresh_interval" : "60s" }, 
        "mappings" : { 
            "_default_" : { 
                "_all" : { "enabled" : false }, 
           "_source" : { "enabled" : false },
           "dynamic": "strict",
    "dynamic_templates" : [{ "message_field" : { "match" : "message", "match_mapping_type" : "string", "mapping" : { "type" : "string", "index" : "not_analyzed" } } }, { "string_fields" : { "match" : "*", "match_mapping_type" : "string", "mapping" : { "type" : "string", "index" : "not_analyzed" } } }], "properties" : { "@timestamp" : { "type" : "date"}, "@version" : { "type" : "integer", "index" : "not_analyzed" }, "path" : { "type" : "string", "index" : "not_analyzed" }, "host" : { "type" : "string", "index" : "not_analyzed" }, "record_time":{"type":"date","format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, "method":{"type":"string","index" : "not_analyzed"}, "unionid":{"type":"string","index" : "not_analyzed"}, "user_name":{"type":"string","index" : "not_analyzed"}, "query":{"type":"string","index" : "not_analyzed"}, "ip":{ "type" : "ip"}, "webbrower":{"type":"string","index" : "not_analyzed"}, "os":{"type":"string","index" : "not_analyzed"}, "device":{"type":"string","index" : "not_analyzed"}, "ptype":{"type":"string","index" : "not_analyzed"}, "serarch_time":{"type":"date","format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, "have_ok":{"type":"string","index" : "not_analyzed"}, "legal":{"type":"string","index" : "not_analyzed"} } } } }

    关键设置

    • template for index-pattern

    只有匹配 logstash-* 的索引才会应用这个模板。有时候我们会变更 Logstash 的默认索引名称,记住你也得通过 PUT 方法上传可以匹配你自定义索引名的模板。当然,我更建议的做法是,把你自定义的名字放在 "logstash-" 后面,变成 index => "logstash-custom-%{+yyyy.MM.dd}" 这样。

    • refresh_interval for indexing

    Elasticsearch 是一个实时搜索引擎。它实际上是每 1 秒钟刷新一次数据。对于日志分析应用,我们用不着这么实时,所以 logstash 自带的模板修改成了 5 秒钟。你还可以根据需要继续放大这个刷新间隔以提高数据写入性能。

    • multi-field with not_analyzed

    Elasticsearch 会自动使用自己的默认分词器(空格,点,斜线等分割)来分析字段。分词器对于搜索和评分是非常重要的,但是大大降低了索引写入和聚合请求的性能。所以 logstash 模板定义了一种叫"多字段"(multi-field)类型的字段。这种类型会自动添加一个 ".raw" 结尾的字段,并给这个字段设置为不启用分词器。简单说,你想获取 url 字段的聚合结果的时候,不要直接用 "url" ,而是用 "url.raw" 作为字段名。

    • geo_point

    Elasticsearch 支持 geo_point 类型, geo distance 聚合等等。比如说,你可以请求某个 geo_point 点方圆 10 千米内数据点的总数。在 Kibana 的 bettermap 类型面板里,就会用到这个类型的数据。

    • order

    如果你有自己单独定制 template 的想法,很好。这时候有几种选择:

    1. 在 logstash/outputs/elasticsearch 配置中开启 manage_template => false 选项,然后一切自己动手;
    2. 在 logstash/outputs/elasticsearch 配置中开启 template => "/path/to/your/tmpl.json" 选项,让 logstash 来发送你自己写的 template 文件;
    3. 避免变更 logstash 里的配置,而是另外发送一个 template ,利用 elasticsearch 的 templates order 功能。

    这个 order 功能,就是 elasticsearch 在创建一个索引的时候,如果发现这个索引同时匹配上了多个 template ,那么就会先应用 order 数值小的 template 设置,然后再应用一遍 order 数值高的作为覆盖,最终达到一个 merge 的效果。

    比如,对上面这个模板已经很满意,只想修改一下 refresh_interval ,那么只需要新写一个:

    {
      "order" : 1,
      "template" : "logstash-*",
      "settings" : {
        "index.refresh_interval" : "20s"
      }
    }

    然后运行以下命令即可:

    curl -XPUT http://localhost:9200/_template/template_newid -d '@/path/to/your/tmpl.json'
    • set _source 设置为 false

    假设你只关心度量结果,不是原始文件内容。比如,你可以把原始的数据存储在 MySQL ,hbase 等其他地方,从 es 中得到 id 后,去相应的数据库中进行取数据。

    将节省磁盘空间并减少 IO。

    “_source”:{“enabled”:false}
    • _all 设置为 false

    假设你确切地知道你对哪个 field 做查询操作?   

    能实现性能提升,缩减存储。

    “_all”:{“enabled”:false }
    • dynamic设置为 strict

    假设你的数据是结构化数据。
    字段设置严格,避免脏数据注入。

    dynamic”:”strict”
  • 相关阅读:
    JUC-狂神笔记整理学习
    多线程-学习笔记
    Redis分布锁
    Redis
    springcloud一个简单的基本流程
    Nacos
    mysql单表查询
    mysql多表查询
    mysql数据库
    mysql详细安装教程以及1067错误代码解决方案
  • 原文地址:https://www.cnblogs.com/caoweixiong/p/11791438.html
Copyright © 2011-2022 走看看