我正在做一个查询,返回一堆文件。这些文档中的一个字段是称为hourly_values
的字典数组。我想返回该字典数组中的秒数字段的总和,但是我无法弄清楚如何进入该数组并求和每个字典中所有秒键的值。
这是存储在elasticsearch中并从简单查询返回的内容:
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "searchdb",
"_type": "profile",
"_id": "915",
"_score": 1,
"_source": {
"id": 915,
"market": "Chicago",
"latitude": "41.1234",
"longitude": "-87.5678",
"structure_type": "Digital Display",
"zip": 60654,
"city": "Chicago",
"player_id": 1234,
"geo_location": {
"lat": 41.1234,
"lon": -87.5678
},
"hourly_values": [
{
"datetime": "2015-11-18T20:02:04Z",
"seconds": 800
},
{
"datetime": "2015-11-18T21:08:29Z",
"seconds": 800
},
{
"datetime": "2015-11-18T21:37:29Z",
"seconds": 6400
}
]
}
}
]
}
}
这是我要构建的查询:
{
"size":0,
"aggregations": {
"seconds_agg": {
"geo_distance": {
"field": "geo_location",
"origin":"41.893371,-87.628329",
"unit":"km",
"ranges":[
{
"from":0,
"to":20
}
]
},
"aggregations":{
"ring_seconds_sum": {
"sum":{
"hourly_values":{
something goes here
}
}
}
}
}
}
}
我不知道要在查询的
something goes here
部分中输入什么。有任何想法吗?
最佳答案
试试这个(我将"to":20
更改为"to":200
,以便它与您发布的文档匹配):
POST /test_index/_search
{
"size": 0,
"aggregations": {
"seconds_agg": {
"geo_distance": {
"field": "geo_location",
"origin": "41.893371,-87.628329",
"unit": "km",
"ranges": [
{
"from": 0,
"to": 200
}
]
},
"aggregations": {
"ring_seconds_sum": {
"sum": {
"field": "hourly_values.seconds"
}
}
}
}
}
}
这是我用来测试的一些代码:
http://sense.qbox.io/gist/02942c5753f6555dfab4571bf8f64bbc1dea74df
关于elasticsearch - 在Elasticsearch中汇总嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33881709/