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": {

    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章