ES插入date类型数据报错

mapping信息如下

{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "case_date": {
          "type": "date"
        }
      }
    }
  }
}

index创建完毕之后插入数据报错,信息如下:
{
“id”: 20001,
“case_date”: “2021-05-18 13:58:01”
}在这里插入图片描述

{
	"error": {
		"root_cause": [
			{
				"type": "mapper_parsing_exception",
				"reason": "failed to parse field [case_date] of type [date]"
			}
		],
		"type": "mapper_parsing_exception",
		"reason": "failed to parse field [case_date] of type [date]",
		"caused_by": {
			"type": "illegal_argument_exception",
			"reason": "Invalid format: "2021-05-18 13:58:01" is malformed at " 13:58:01""
		}
	},
	"status": 400
}

解决方法,修改mapping如下:

{
  "mappings": {
    "_doc": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "case_date": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

重新创建index之后再次插入数据成功
在这里插入图片描述
ES的日期类型表示格式可以是以下几种:
(1)日期格式的字符串,比如 “2018-01-13” 或 “2018-01-13 12:10:30”
(2)long类型的毫秒数( milliseconds-since-the-epoch,epoch就是指UNIX诞生的UTC时间1970年1月1日0时0分0秒)
(3)integer的秒数(seconds-since-the-epoch)

ElasticSearch 内部会将日期数据转换为UTC,并存储为milliseconds-since-the-epoch的long型整数。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐