默认日期类型
当索引第一条数据入库时,ElasticSearch 会自动猜测字段类型,时序数据索引都会有时间字段,除了手动设置mapping和通过template以外,我们希望通过date-detection机制,让他自己猜测出此字段是日期字段。
默认能被动态监测除了的日期格式(dynamic_date_formats
)是:
yyyy/MM/dd HH:mm:ss Z
yyyy/MM/dd Z
Java程序测试
测试代码
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// 省略
_json.put("@timestamp", sdf.format(new Date()));
//省略
查看映射
入库后查看mapping映射
curl -XGET 10.2x.7x.xxx:39xxx/httpmetrics-dns-2018.01.06/_mapping
映射结果
mapping结果如下
{
"httpmetrics-dns-2018.01.06": {
"mappings": {
"dns": {
"properties": {
"@timestamp": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "long"
}
}
}
}
}
}