- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一组时间戳记,自该纪元以来以秒为单位。我想以epoch_seconds
的形式插入到ElasticSearch中,但是在查询时希望将输出看成一个漂亮的日期,例如strict_date_optional_time
。
我的下方映射保留了输入的格式-是否有任何方法可以通过mapping API将输出标准化为一种格式?
当前映射:
PUT example
{
"mappings": {
"time": {
"properties": {
"time_stamp": {
"type": "date",
"format": "strict_date_optional_time||epoch_second"
}
}
}
}
}
POST example/time
{
"time_stamp": "2018-03-18T00:00:00.000Z"
}
POST example/time
{
"time_stamp": "1521389162" // Would like this to output as: 2018-03-18T16:05:50.000Z
}
{
"total": 2,
"max_score": 1,
"hits": [
{
"_source": {
"time_stamp": "1521389162", // Stayed as epoch_second
}
},
{
"_source": {
"time_stamp": "2018-03-18T00:00:00.000Z"
}
}
]
}
最佳答案
Elasticsearch区分_source
和所谓的存储字段。第一个应该代表您的输入。
如果您实际使用存储的字段(通过在映射中指定store=true
),然后指定multiple date formats,这很简单:(强调我的)
Multiple formats can be specified by separating them with || as a separator. Each format will be tried in turn until a matching format is found. The first format will be used to convert the milliseconds-since-the-epoch value back into a string.
PUT /test -d '{ "mappings": {"doc": { "properties": {"post_date": {
"type":"date",
"format":"basic_date_time||epoch_millis",
"store":true
} } } } }'
PUT /test/doc/2 -d '{
"user" : "test1",
"post_date" : "20150101T121030.000+01:00"
}'
PUT /test/doc/1 -d '{
"user" : "test2",
"post_date" : 1525167490500
}'
GET /test/_search?stored_fields=post_date&pretty=1
时,两种不同的输入格式将如何导致相同的格式
{
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"post_date" : [
"20150101T111030.000Z"
]
}
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"post_date" : [
"20180501T093810.500Z"
]
}
}
]
}
_source
),那么就不太幸运了,已删除了
mapping-transform功能:
This was deprecated in 2.0.0 because it made debugging very difficult. As of now there really isn’t a feature to use in its place other than transforming the document in the client application.
关于datetime - 插入日期为epoch_seconds,输出为格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49350065/
我是一名优秀的程序员,十分优秀!