2017-01-30 3 views
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"}]} 

제가 이걸 어떻게 달성 할 수 있는지 제발 도와 주실 수 있겠습니까?

답변

1

어때?

mydata = [] 
for hit in res['hits']['hits']: 
    v = hit["_source"] 
    mydata.append({'name': v['name'],'timestamp': v['@timestamp']}) 
result = {"mydata": mydata} 
q = json.dumps(result) 
print(q) 
+1

놀라운 작품 – Dython