elastic re-index 日期格式太短
elastic re-index date format is too short
正在尝试重新索引索引,因为日期字段的格式已更改。
格式从
...
"start_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
...
到
...
"start_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
...
我尝试将我的索引重新索引到 tmp 索引中,但它抛出以下错误:
"cause": {
"type": "mapper_parsing_exception",
"reason": "failed to parse [start_date]",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Invalid format: \"2019-01-30 13:03\" is too short"
}
},
所以,现在我遇到了一个大问题。如何更改日期字段的格式?还有不重新索引的其他选择吗?
由于格式已更改,您需要做的是将 :00
附加到您的日期字段以匹配新格式:
POST _reindex
{
"source": {
"index": "oldindex"
},
"dest": {
"index": "newindex"
},
"script": {
"source": "ctx._source.start_date = ctx._source.start_date + ':00';"
}
}
如果您正在寻找更灵活的解决方案,我愿意为@Val 的回答做出贡献。请检查以下代码:
POST _reindex
{
"source": { "index": "old-index" },
"dest": { "index": "new-index" },
"script": {
"source": """
def old_sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
def old_dt = old_sf.parse(ctx._source.start_date);
ctx._source.start_date = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(old_dt);
"""
}
}
这样,您可以更轻松地显着更改日期格式。例如,
POST _reindex
{
"source": { "index": "old-index" },
"dest": { "index": "new-index" },
"script": {
"source": """
def old_sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
def old_dt = old_sf.parse(ctx._source.start_date);
ctx._source.start_date = new SimpleDateFormat('HH:mm:ss dd MMM yyyy').format(old_dt);
"""
}
}
正在尝试重新索引索引,因为日期字段的格式已更改。 格式从
...
"start_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
...
到
...
"start_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
...
我尝试将我的索引重新索引到 tmp 索引中,但它抛出以下错误:
"cause": {
"type": "mapper_parsing_exception",
"reason": "failed to parse [start_date]",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Invalid format: \"2019-01-30 13:03\" is too short"
}
},
所以,现在我遇到了一个大问题。如何更改日期字段的格式?还有不重新索引的其他选择吗?
由于格式已更改,您需要做的是将 :00
附加到您的日期字段以匹配新格式:
POST _reindex
{
"source": {
"index": "oldindex"
},
"dest": {
"index": "newindex"
},
"script": {
"source": "ctx._source.start_date = ctx._source.start_date + ':00';"
}
}
如果您正在寻找更灵活的解决方案,我愿意为@Val 的回答做出贡献。请检查以下代码:
POST _reindex
{
"source": { "index": "old-index" },
"dest": { "index": "new-index" },
"script": {
"source": """
def old_sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
def old_dt = old_sf.parse(ctx._source.start_date);
ctx._source.start_date = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(old_dt);
"""
}
}
这样,您可以更轻松地显着更改日期格式。例如,
POST _reindex
{
"source": { "index": "old-index" },
"dest": { "index": "new-index" },
"script": {
"source": """
def old_sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
def old_dt = old_sf.parse(ctx._source.start_date);
ctx._source.start_date = new SimpleDateFormat('HH:mm:ss dd MMM yyyy').format(old_dt);
"""
}
}