elasticsearch-日期类型
PUT my-index-000001/_doc/2
{ "date": "2015-01-01T12:10:30Z" }
PUT my-index-000001/_doc/3
{ "date": 1420070400001 }
PUT my-index-000001/_doc/1
{ "date": "2015-01-01" }
PUT my-index-000001/_doc/4
{ "date": "2014-01-01" }
2015-01-01T12:10:30Z
, 时间戳,2015-01-01
都可以识别为 date类型
GET /my-index-000001/_mapping
{
"my-index-000001": {
"mappings": {
"properties": {
"date": {
"type": "date"
}
}
}
}
}
因为 es 默认使用 "strict_date_optional_time||epoch_millis"
这个格式
如果 使用 yyyy-MM-dd HH:mm:ss
格式,则 es 会推断为 文本类型
PUT /my-index-000002/_doc/1
{
"createTime": "2023-06-02 13:48:16"
}
GET /my-index-000002/_mapping
{
"my-index-000002": {
"mappings": {
"properties": {
"createTime": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
其实也好办,在创建索引时,指定索引mapping 中 date 类型匹配的格式
PUT my-index-000003
{
"mappings": {
"properties": {
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
PUT /my-index-000003/_doc/1
{
"createTime": "2023-06-02 13:48:16"
}
GET /my-index-000003/_search
{
"query": {
"range": {
"createTime": {
"gte": "2023-06-02 13:48:15", //可以查出,也没有时区
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}