这是elasticsearch的返回结构,aggregations中的user、tags、ct等都是不可预知的字段,要如何取出这些结构,并还原成一维map?
```json
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 3,
"failed": 0
},
"hits": {
"total": 22,
"max_score": 0,
"hits": []
},
"aggregations": {
"user": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "olivere",
"doc_count": 16,
"tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "golang",
"doc_count": 16,
"ct": {
"value": 16
}
},
{
"key": "elasticsearch",
"doc_count": 7,
"ct": {
"value": 7
}
}
]
}
},
{
"key": "sandrae",
"doc_count": 6,
"tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "cycling",
"doc_count": 6,
"ct": {
"value": 6
}
},
{
"key": "sports",
"doc_count": 6,
"ct": {
"value": 6
}
}
]
}
}
]
}
}
}
```
使用map[string]interface{}的对象作为json.Unmarshal的输出参数,然后问题就解决了。当然,你需要对value的值做一些类型判断
#8
更多评论