例如,我想通过状态聚合,但是以下返回数据tyep是字符串而不是数组。 如何编写返回数组的Elasticsearch术语聚合?
我的部分代码:
import urllib2 as urllib
import json
query = {
"size":0,
"aggs":{
"states":{
"terms":{
"field":"states.raw",
"size":8
}
}
}
}
query = json.dumps(query )
headers = {'Content-type': 'application/json'}
req = urllib2.Request(url, query , headers)
out = urllib2.urlopen(req)
rs = out.read()
print type(rs )
返回:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"states": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "New York",
"doc_count": 200
},
{
"key": "California",
"doc_count": 10
},
{
"key": "New Jersey",
"doc_count": 10
},
{
"key": "North Carolina",
"doc_count": 1802
},
{
"key": "North Dakota",
"doc_count": 125
}
]
}
}
}
我尝试通过rs ['聚合'] ['状态'] ['桶'] [0] [' key& #39] 但得到错误消息
"TypeError: string indices must be integers, not str"
我发现返回数据类型是字符串,如何使返回数据是一个数组?
答案 0 :(得分:1)
运行
public mActive MyEnumInViewModel {get;set;}
然后rs将成为您使用import json
...
rs = json.loads(rs)
但是,它建议您使用python客户端进行弹性搜索,而不是自己编写,因为后者已经处理了您正在寻找的内容。检查我的答案here,了解如何使用elasticsearch-py运行查询。
这里是elasticsearch-py文档的链接: