Elastic Search最佳实践

ES最佳实践(6.x)

按天生成index,支撑每天10亿级日志量入库。
  • ES日志采用按天生成index方法,查询先确定索引位置,如log_20190801,log_20190802。再去ES中查找
动态模板与指定字段类型配合使用,优先采用指定类型,字段类型指定为keyword,不分词。
  • 针对精确查询:使用term/terms + filter可完整查询字段并缓存结果。
  • 针对模糊查询,使用wildcard + term/terms + filter。
  • 指定时间格式为format:“yyyyMMdd HH:ss:mm”。
  • 不分词会减少日志存储量,不需要再去指定一个子字段存储完整信息。
ES分片尽可能少,如对高可用有要求,可适当增加。否则,分片设置为0或1。
  • 此项主要是日志量大,分片过多集群压力容量不够。“number_of_replicas”: “0”, 主分片的拷贝分片个数设置为0.
ES刷新时间增长,增加ES入库吞吐率。频繁刷新入库虽然会及时展示结果,但是会降低吞吐率。
  • “refresh_interval”: “5s”,刷新时间设置为5s

参考ES动态模板

  • 请尽可能的增加已知类型的properties属性。
可用动态模板

PUT _template/log

{
  "order": 0,
  "index_patterns": [
    "log_*"
  ],
  "settings": {
    "index": {
      "analysis.analyzer.default.type": "ik_max_word",
      "number_of_shards": "32",
      "number_of_replicas": "1",
      "refresh_interval": "60s"
    }
  },
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "string_fields": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": {
              "analyzer": "ik_max_word",
              "index": true,
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "Time": {
          "type": "date",
          "format": "yyyyMMdd HH:mm:ss"
        },
        "ServiceName": {
          "type": "keyword"
        },
        "ResulrMsg": {
          "type": "keyword"
        },
        "Exfield": {
          "type": "text",
          "analyzer": "ik_max_word",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
测试用例

POST log_20190903/doc

{
  "Time": "20190924 18:00:00",
  "ServiceName": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首",
  "ResulrMsg":"哈哈hh哈哈哈哈,nishi你是yige你是一个hello",
  "Integer":100,
  "Float":100.0,
  "StrTest":"展示次数超过1000,100,10000的ip",
  "Exfield":"这是一条默认分词的字段,类型为text,keyword是一个子字段属性,默认类型是keyword,不分词,可设置ignore_above:256"
}
动态模板解释
{
  "aion": {
    "order": 0,
    "index_patterns": [
      "aion_*"
    ],
    "settings": {
      "index": {
        "analysis": {...},                // 自定义的分析器
        "number_of_shards": "32",         // 主分片的个数
        "number_of_replicas": "0",        // 主分片的拷贝分片个数
        "refresh_interval": "5s"          // 刷新时间
      }
    },
    "mappings": {
      "doc": {
        "dynamic_templates": [
                 {
            "string_fields": {                                  // 字段映射模板的名称,一般为"类型_fields"的命名方式
                "match": "*",                                   // 匹配的字段名为所有
                "match_mapping_type": "string",                 // 限制匹配的字段类型,只能是 string 类型
                "mapping": {
                    "fielddata": { "format": "disabled" },      // fielddata 不可用,对于分析字段,其默认值是可用
                    "analyzer": "only_words_analyzer",          // 字段采用的分析器名,默认值为 standard 分析器
                    "index": "not_analyzed",                        // 索引方式定义为索引,默认值是分析,目前设置为不分析
                    "omit_norms": false,                         // omit_norms 为真表示考虑字段的加权,可分析字段默认值 false
                    "type": "keyword"                           // 字段类型限定为 keyword
                }
            }

        ],
        "properties": {
          "Time": {
            "type": "date",
            "format": "yyyyMMdd HH:mm:ss"
          }
          "ServiceName":{
          "type": "keyword"
          }
        }
      }
    },
    "aliases": {

    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值