python - 如何修复 Elasticsearch 中的错误 "failed to parse date field "

标签 python parsing elasticsearch timestamp

我试图通过 pyton 在 ELasticsearch 中进行查询。我想从现在开始获得过去一小时内的所有值。为此,我写了这个脚本:

import time
from elasticsearch import Elasticsearch
from datetime import datetime, timedelta

es = Elasticsearch()
index= "standalone"

filename = "2017-12-22V2.csv"

Timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
one_hour_from_now = datetime.now() - timedelta(hours=1)
one_hour_from_now = one_hour_from_now.strftime('%Y-%m-%d %H:%M:%S')


query = {"query":{"bool":{"must":{"range":{"Time":{"gt":one_hour_from_now,"lt":Timestamp}}},"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"aggs":{}}


ret = es.search(index, body=query)
print("ret", ret)

当我执行它时,我收到此错误:
 es.search exception:  TransportError(400, 'search_phase_execution_exception', 'failed to parse date field [2018-02-12 15:50:26] with format [strict_date_optional_time||epoch_millis]')

这是我的 ES 索引的结构:
Elasticsearch structure

有人能帮助我吗

谢谢

最佳答案

从文档看来,您的格式日期失败了。
根据您的屏幕截图,您的数据采用以下格式:

yyyy-MM-dd'T'HH:mm:ss
根据documentation这种格式是
date_hour_minute_second or strict_date_hour_minute_second
使用 python 中的 datetime 库,您以这种方式塑造了日期格式:
yyyy-MM-dd HH:mm:ss
尝试在范围查询中转换 strict_date_optional_time - es 中的默认日期格式,根据 es 索引的结构在日期字段中使用 - 使用 format子句并将其转换为值 yyyy-MM-dd HH:mm:ss或更改您的行代码:
one_hour_from_now = one_hour_from_now.strftime('%Y-%m-%d %H:%M:%S')
one_hour_from_now = one_hour_from_now.strftime("%Y-%m-%d"'T'"%H:%M:%S")
并且不要在查询中指定格式或指定正确的格式

关于python - 如何修复 Elasticsearch 中的错误 "failed to parse date field ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749364/

相关文章:

python - 使用 Matplotlib 自定义基于时间序列的数据的 x Axis

c# - 在 C# 中解析 unicode 字符的字符串

python - 在lxml中的html注释之间选择div

java - nodeBuilder()/NodeBuilder - 无法解析符号 - Elastic Search Java API(在 Android 上)

python - Elasticsearch和查询

elasticsearch - Elasticsearch 中的存储过程

python - 如何获取gtk.ListStore的项目数/行数/长度/计数

python - Raspberry Pi - 访问 0x40 : Check your I2C address 时出错

python - 多级列上的 Pandas get_dummies()

python - Python 的 NLTK 中的荷兰语语法