ES 日期格式处理

ES 日期格式处理

ES 中使用动态模板关于处理日期格式问题


一、ES 动态模板一

这里是把所有的日志格式字段格式化为 yyyy-MM-dd HH:mm:ss
举例:
2023-09-04 保存到es变成 2023-09-04 00:00:00
2023-09-04 10:00:00 保存到es变成 22023-09-04 10:00:00

PUT /_template/test_template
{
  "order": 0,
  # 那些索引需要用到模板,多个逗号分隔
  "index_patterns": [
    "abcdefg","hijkl"
  ],
  "settings": {},
  "mappings": {
    "dynamic_date_formats": "date_optional_time||strict_date_optional_time||yyyy-MM-dd HH:mm:ss",
    "dynamic_templates": [
      {
        "date_fields": {
            "mapping": {
              "format": "yyyy-MM-dd HH:mm:ss",
              "ignore_malformed": true,
              "type": "date"
            },
            "match_mapping_type": "date",
            "match": "*"
          }
      },
      {
        "my_template_string": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  },
  "aliases": {}
}

二、Es 动态模板二

这里是保留日期的原来格式
举例:
2023-09-04 保存到es变成 2023-09-04
2023-09-04 10:00:00 保存到es变成 22023-09-04 10:00:00

PUT /_template/test_template2
{
  "order" : 0,
    # 那些索引需要用到模板,多个逗号分隔
    "index_patterns" : [
      "xxxxxx"
    ],
    "settings" : { },
    "mappings" : {
      "dynamic_date_formats" : "date_optional_time||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
      "dynamic_templates" : [
        {
          "date_fields" : {
            "mapping" : {
              "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second",
              "ignore_malformed" : true,
              "type" : "date"
            },
            "match_mapping_type" : "date",
            "match" : "*"
          }
        },
        {
          "my_template_string" : {
            "mapping" : {
              "type" : "keyword"
            },
            "match_mapping_type" : "string"
          }
        }
      ]
    },
    "aliases" : { }
}

你可能感兴趣的:(elasticsearch,java,前端)