版本为7.3.2,主要记录index,settings,mappings相关的操作,方便快速测试,调试

7版本已经不需要指定_doc属性,以下可忽略_doc

获取某个索引的mapping信息

get    http://10.192.78.27:39200/<index_name>/_mapping

创建一个动态mapping的index,并指定某些字段,并允许新字段

put    http://10.192.78.27:39200/<index_name>

{
  "mappings": {
     "dynamic": "true",
     "properties": {
        "id":{ "type":"keyword"},
        "rule_name": { "type": "keyword" },
        "metric_threshold":{ "type":"integer"},
        "audit_status":{"type": "integer"},
        "audit_comment_num":{"type":"integer"},
        "metric_count":{"type":"integer"},
        "window_start":{"type":"date"},
        "window_end":{"type":"date"}
    }
  }
}

在当前索引的mapping下新增加一个字段

加一个指定了format的date类型字段window_start

PUT   http://10.192.78.27:39200/<index_name>/_mapping/

{
   "properties": {
      "window_start": {
         "type": "date",
         "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSSZ"
      }
   }
}

date类型字段问题

format属性官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html#date-params

自定义时间格式属性:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html

date类型的默认格式为:"strict_date_optional_time||epoch_millis" (yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd or 1618321898)

时区问题:最佳实践为写入前就指定好时区(2022-01-01T12:58:17.136+0800或timezone=GMT+8),不要查出来后在服务端转或在前端转

写入前如果不指定时区,es默认按照UTC 0时区,时间相差8小时

再明确记录下:不指定date类型的格式的话,默认是不支持 yyyy-MM-dd HH:mm:ss格式的

PUT index_name
{
    "mappings": {
        "_doc": {
            "properties": {
                "create_time": {
                      "type": "date",
                      "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
                  }
             }
        }
    }
}    

删除索引(一般都会设置禁止批量删除.....)

DELETE    http://10.192.78.27:39200/<index_name>

相关文章: