以下查询在elasticsearch中提供了我的文档的词汇量大小:
GET my_index/my_type/_search
{
"size": 0,
"aggs": {
"vocab": {
"cardinality": {
"field": "text"}}}}
输出结果为:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 84678,
"max_score": 0,
"hits": []
},
"aggregations": {
"vocab": {
"value": 178050**
}
}
}
现在我想在python中打印字段" value"(= 178050)的值。
我在python中写了以下内容:
query_body={
"size": 0,
"aggs": {
"vocab": {
"cardinality": {
"field": "text"}}}}
res = es.search(index = INDEX_NAME, body=query_body)
如何打印该值?感谢。
答案 0 :(得分:1)
这是你期待的? 在python中,我们可以通过键来获取dict的值。
来自你的输出:
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
答案 1 :(得分:1)
鉴于您的结果字典如下所示:
{
"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']
如果res
是str
,您可以使用json.loads
将其转换为字典
import json
res = json.loads(s)
value = res['aggregations']['vocab']['value']