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