我们有一条记录,其中有一些设备的记录,每条记录包含有关位置,时间戳和deviceId的信息:
[
{
deviceId: 1,
geoPoint: {
lat: 1, lon: 2
},
timestamp: '2018-01-01 12:00:00'
},
{
deviceId: 1,
geoPoint: {
lat: 3, lon: 4
},
timestamp: '2018-01-01 12:01:00'
},
{
deviceId: 1,
geoPoint: {
lat: 5, lon: 6
},
timestamp: '2018-01-01 12:02:00'
},
{
deviceId: 2,
geoPoint: {
lat: 1, lon: 2
},
timestamp: '2018-01-01 12:01:00'
},
{
deviceId: 2,
geoPoint: {
lat: 3, lon: 4
},
timestamp: '2018-01-01 12:02:00'
},
{
deviceId: 2,
geoPoint: {
lat: 5, lon: 6
},
timestamp: '2018-01-01 12:03:00'
}
];
我想获得geohash_grid结果,以便与Google地图(或其他任何地图)一起使用,以显示具有可用点的群集或具有详细信息的单个项目。此结果应包括每个唯一广告的最后位置(最大时间戳) deviceId。
然后根据不同的精度获得预期结果:
精度1示例:
single bucket with 2 documents count
精度2示例:
2 buckets with one document in (in case of only one element return details)
这是当前查询:
{
_source: ['deviceId'],
aggs: {
clustering: {
geohash_grid: {
field: 'geoPoint',
precision: 2
},
aggs: {
unique_id: {
terms: {
field: 'deviceId',
},
aggs: {
last_one: {
max: {
field: 'timestamp'
}
},
include_source: {
top_hits: {
size: 1,
_source: {
include: [
'timestamp', 'geoPoint', 'deviceId'
]
}
}
}
}
}
}
}
}
}
但是每个存储桶的doc_count
返回的结果是记录的总数,而不是最后的记录(基于时间戳)。
此查询应如何获得正确的结果? 另外:在执行聚合以排除记录时没有查询是否是一种性能正确的解决方案?
编辑:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"clustering": {
"buckets": [
{
"key": "u2",
"doc_count": 4,
"last_device_position": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 2,
"last_one": {
"value": 1514764860000,
"value_as_string": "2018-01-01 00:01:00"
}
},
{
"key": "2",
"doc_count": 2,
"last_one": {
"value": 1514764860000,
"value_as_string": "2018-01-01 00:01:00"
}
}
]
}
},
{
"key": "t5",
"doc_count": 2,
"last_device_position": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 1,
"last_one": {
"value": 1514764920000,
"value_as_string": "2018-01-01 00:02:00"
}
},
{
"key": "2",
"doc_count": 1,
"last_one": {
"value": 1514764920000,
"value_as_string": "2018-01-01 00:02:00"
}
}
]
}
}
]
}
}
}
查找唯一结果无法正常工作,以及最大字段值