下面是我的代码,它抓取数据并将数据转换为CSV文件(这是有效的)。我试图只关注当前日期返回的数据。
有人可以告诉我这是怎么做的,DTDT是约会对象。 我的代码:
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST*"}}
]
}
}
}, size=10)
header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}
with open('mycsvfile.csv', 'w') as f: # Just use 'w' mode in 3.x
header_present = False
for doc in res['hits']['hits']:
my_dict = doc['_source']
if not header_present:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(header_names) # will write DATE, TIME, ... in correct place
header_present = True
w.writerow(my_dict)
例如,我想只从午夜到下午2点返回数据(使用当前日期)。
答案 0 :(得分:0)
您可以使用范围过滤器执行此操作:
{
"_source": [
"DTDT",
"TRDT",
"SPLE",
"RPLE"
],
"query": {
"bool": {
"should": [
{
"wildcard": {
"CN": "TEST*"
}
}
],
"must": [
{
"range": {
"DTDT": {
"gte": "now/d",
"lte": "now+1d/d"
}
}
}
]
}
}
}
*编辑到当前日历日的范围与舍入,而不是过去24小时。