0
我使用下面的代码从弹性搜索数据,我不能把它格式化按我的要求蟒蛇elasticsearch格式数据
from datetime import datetime
from elasticsearch import Elasticsearch
import json
es = Elasticsearch(hosts=[{'host': "localhost", 'port': "9200"}])
res = es.search(index="myindice",size=2, body={"query": {"match_all":{}}})
for hit in res['hits']['hits']:
v = hit["_source"]
q = json.dumps({'name': v['name'],'timestamp': v['@timestamp']})
print(q)
,输出是
{"timestamp": "2016-09-22T00:28:44.000Z", "name": "1456772324.47092"}
{"timestamp": "2016-09-22T00:32:16.000Z", "name": "1456772536.57587"}
{"timestamp": "2016-09-22T00:39:19.000Z", "name": "1456772836.57587"}
但我想要这样的输出:
{"mydata":[{"timestamp": "2016-09-22T00:28:44.000Z", "name":"1456772324.47092"},
{"timestamp": "2016-09-22T00:32:16.000Z", "name": "1456772536.57587"},
{"timestamp": "2016-09-22T00:39:19.000Z", "name": "1456772836.57587"}]}
你能帮我一下,我怎么能做到这一点。
Ossom works amazing – Dython