代码之家  ›  专栏  ›  技术社区  ›  keypoint

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

  •  1
  • keypoint  · 技术社区  · 6 年前

    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)

    嗨,我做了我的工作谷歌搜索各地,但可能是关键字太一般,所以我没有得到太多。

    UsageStartDate UsageEndDate 仍显示为 string 当我在基巴纳看到他们的时候。

    我有什么遗漏吗?

    非常感谢:)

    2 回复  |  直到 6 年前
        1
  •  0
  •   keypoint    6 年前

    我自己想出来的,

    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)
    
        2
  •  -1
  •   rkm    6 年前

    According to the docs ElasticSearch与 date 以下面的方式。在内部,它将它们存储为表示自epoch以来的毫秒数的长数字。对于输出日期,将始终呈现为字符串。

    JSON没有日期数据类型,因此Elasticsearch中的日期可以是:

    • 包含格式化日期的字符串,例如“2015-01-01”或“2015/01/01”
    • 一个整数,表示从纪元开始的秒数。

    在内部,日期是 一个长的数字,表示从epoch开始的毫秒数。

    长表示,以及聚合和存储字段的结果 根据所使用的日期格式转换回字符串 与字段关联。