弹性重新索引日期格式太短

问题描述 投票:0回答:2

尝试重新索引索引,因为日期字段的格式已更改。格式改变了

 ...
 "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"
    }
  },

所以,现在我有一个大问题。如何更改我的日期字段的格式?还有其他选择不重新索引吗?

elasticsearch format reindex
2个回答
1
投票

由于格式已更改,您需要做的是将:00附加到日期字段以匹配新格式:

POST _reindex
{
  "source": {
    "index": "oldindex"
  },
  "dest": {
    "index": "newindex"
  },
  "script": {
    "source": "ctx._source.start_date = ctx._source.start_date + ':00';"
  }
}

0
投票

如果您正在寻找更灵活的解决方案,我想为@ 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);
    """
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.