日期字段值返回字符串而不是日期

时间:2018-07-17 10:11:05

标签: elasticsearch

import pandas as pd
import numpy as np
from elasticsearch import Elasticsearch
import json
import sys
from dateutil.parser import parse

# create instance of elasticsearch
es = Elasticsearch('localhost:9200')

with open ('C:\\Users\\paul.braimoh\\Desktop\\infrastructure.json')as b:
    data = json.load(b)
    print(data)
print (data);

for key, value in enumerate(data):
    print(key, value)
    es.index(index="infrastructure2", doc_type="test-type", body=value)

这是json文件的样子

[
  {
    "Date": "1/1/2018",
    "Location": "location",
    "TYPE OF SOLUTION": "solution",
    "SLO": "90%",
    "Availability": "100.00%",
    "Comments": "resolve"
  }
]

1 个答案:

答案 0 :(得分:0)

默认情况下,当在文档中找到以前看不见的字段时,Elasticsearch会将新字段添加到类型映射中。如果启用了date_detectiondefault),则将检查新的字符串字段,以查看其内容是否与dynamic_date_formats中指定的任何日期模式匹配。

要考虑的另一个因素是according to the client documentation

日期时间将被序列化

es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
{u'_id': u'42', u'_index': u'my-index', u'_type': u'test-type', u'_version': 1, u'ok': True}

但不反序列化

es.get(index="my-index", doc_type="test-type", id=42)['_source']
{u'any': u'data', u'timestamp': u'2013-05-12T19:45:31.804229'}

因此,您只需要使用datetime.strptime来解析日期