ElasticSearch 不识别日期类型,显示为字符串类型

ElasticSearch not recognizing date type, shown as String type

ElasticSearch 版本:6.3

映射定义: ES_DOCTYPE = { "properties": { "UsageEndDate": {"type": "date", "format": "YYYY-MM-dd HH:mm:ss"}, "UsageStartDate": {"type": "date", "format": "YYYY-MM-dd HH:mm:ss"} } }

json数据:

{ 'UsageEndDate': '2018-08-01 02:00:00', 'UsageStartDate': '2018-08-01 01:00:00' } { 'UsageEndDate': '2018-08-02 02:00:00', 'UsageStartDate': '2018-08-02 01:00:00' }

创建索引: es.index(index="test", doc_type='test', body=ES_DOCTYPE)

发送数据: helpers.streaming_bulk(es, documents(), index="test", doc_type='test', chunk_size=1000)

嗨,我做了我的工作 google 搜索,但也许关键字太笼统了,所以我没有得到太多。

我看了文档,ES应该可以自动找到日期格式,但即使我添加了映射定义,那些UsageStartDateUsageEndDate在我查看时仍然显示为string他们在 Kibana。

有什么我遗漏的吗?

非常感谢:)

According to the docs ElasticSearch 通过以下方式与 date 协同工作。在内部,它将它们存储为一个长数字,表示自纪元以来的毫秒数。对于输出日期将始终呈现为字符串。

引自文档:

JSON doesn’t have a date datatype, so dates in Elasticsearch can either be:

  • strings containing formatted dates, e.g. "2015-01-01" or "2015/01/01 12:10:30".
  • a long number representing milliseconds-since-the-epoch.
  • an integer representing seconds-since-the-epoch.

Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.

Queries on dates are internally converted to range queries on this long representation, and the result of aggregations and stored fields is converted back to a string depending on the date format that is associated with the field.

我自己想出来了,

dynamicTemplate = { "properties": { "UsageEndDate": {"type": "date", "format": "YYYY-MM-dd HH:mm:ss"}, "UsageStartDate": {"type": "date", "format": "YYYY-MM-dd HH:mm:ss"} }, "dynamic_templates": [ { "notanalyzed": { "match": "*", "match_mapping_type": "string", "mapping": { "type": "string", "index": "not_analyzed" } } } ] }

并先定义映射,然后将数据发送到ElasticSearch

es.indices.put_mapping(index="test", doc_type='test', body={'test': dynamicTemplate})

es.index(index='test-index', doc_type='tweet', body=ES_DOCTYPE)