2017-05-25 126 views
0

下面的查詢給出elasticsearch我的文檔的詞彙量的大小:在如何在python中打印elasticsearch的查詢結果?

{ 
"took": 0, 
"timed_out": false, 
"_shards": { 
"total": 1, 
"successful": 1, 
"failed": 0 
}, 
"hits": { 
"total": 84678, 
"max_score": 0, 
"hits": [] 
}, 
"aggregations": { 
"vocab": { 
"value": 178050** 
    } 
} 
} 

現在我想打印領域的「值」的值(= 178050):

GET my_index/my_type/_search 
{ 
    "size": 0, 
    "aggs": { 
    "vocab": { 
     "cardinality": { 
      "field": "text"}}}} 

輸出是蟒蛇。

我寫在Python以下幾點:

query_body={ 
    "size": 0, 
    "aggs": { 
    "vocab": { 
     "cardinality": { 
      "field": "text"}}}} 

res = es.search(index = INDEX_NAME, body=query_body) 

我怎麼能打印的價值?謝謝。

回答

0

鑑於你的結果字典是這樣的:

從你的輸出

{ 
    "took": 0, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 84678, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "vocab": { 
      "value": 178050 
     } 
    } 
} 

您可以訪問喜歡的結果@mohammedyasin推薦:

res['aggregations']['vocab']['value'] 

如果resstr,您可以使用012將其轉換爲字典

import json 
res = json.loads(s) 
value = res['aggregations']['vocab']['value'] 
+0

非常感謝!這工作。 – farha

+0

如果答案有幫助,請回覆/標記爲已回答。 – 2ps

0

這是你期待的? 在python中,我們可以通過key得到字典的值。

output = { 
"took": 0, 
"timed_out": false, 
"_shards": { 
"total": 1, 
"successful": 1, 
"failed": 0 
}, 
"hits": { 
"total": 84678, 
"max_score": 0, 
"hits": [] 
}, 
"aggregations": { 
"vocab": { 
"value": 178050** 
    } 
} 
} 

print (output["aggregations"]["vocab"]["value"]) # It will print the value or use output.get("aggregations").get("vocab").get("value") 
178050