赞
踩
elasticsearch在没有手动指定映射时会自动根据值映射类型,在时间的映射有需要注意。
首先创建一个索引(test),这里不指定映射类型
PUT /test
往test索引中插入一个数据
POST /test/_doc
{
"name":"zhangsan",
"birthday":"2021-01-01"
}
然后查询索引中数据映射关系
GET /test/_mapping
映射关系如下
{ "test" : { "mappings" : { "dynamic_date_formats" : [ "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" ], "date_detection" : true, "properties" : { "birthday" : { "type" : "date", "format" : "yyyy-MM-dd" }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }
从映射关系可以看到birthday自动映射成了日期类型。将test索引删除重新创建
PUT /test
仍然插入刚才的数据,现在将birthday的时分秒加上
POST /test/_doc
{
"name":"zhangsan",
"birthday":"2021-01-01 00:00:00"
}
再看看映射关系
GET /oa_data_01/_mapping
{ "test" : { "mappings" : { "properties" : { "birthday" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }
可以看到birthday加了时分秒后es没有将其映射为日期类型,而是字符串类型,这里需要在创建索引时做如下设置才能让es识别时分秒的日期。date_detection设置为ture是让es自动检测日期类型,dynamic_date_formats是需要匹配的日期格式,这里设置了两种格式都要让es自动映射为日期类型。
PUT /test
{
"mappings": {
"date_detection": true,
"dynamic_date_formats": ["yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"]
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。