以下是每隔几分钟记录10台设备。我需要为每个id返回唯一的记录集,每个都应该只是最新的。
如何使用弹性搜索或其他任何解决方案都可以做到这一点。
{
{id: 1, time: 12345},
{id: 2, time: 12346},
{id: 1, time: 12347},
{id: 2, time: 12348},
{id: 1, time: 12349},
{id: 3, time: 12350},
... // 10 different ids and 10000 records
}
答案 0 :(得分:1)
这是我能得到的尽可能接近。如果您在原始数据集中说出超过10个唯一ID,则不确定如何选择返回哪10个结果。
var rawData = [
{id: 1, time: 12345},
{id: 2, time: 12346},
{id: 1, time: 12347},
{id: 2, time: 12348},
{id: 1, time: 12349},
{id: 3, time: 12350},
{id: 4, time: 12351},
{id: 2, time: 12352},
{id: 7, time: 12353},
{id: 5, time: 12354},
{id: 3, time: 12355},
{id: 6, time: 12356},
{id: 3, time: 12357},
{id: 7, time: 12358},
{id: 6, time: 12359},
{id: 9, time: 12360}
]
var maxSet = {};
// Get all of the max values
rawData.forEach(function (currentValue, index, array) {
var currentMax = maxSet[currentValue.id] || null;
if(currentMax) {
if(currentValue.time > currentMax){
maxSet[currentValue.id] = currentValue.time;
}
} else {
maxSet[currentValue.id] = currentValue.time;
}
});
console.log(maxSet);
// Convert back to object if necessary
var keys = Object.keys(maxSet);
var resultObjs = [];
for(var key in keys) {
resultObjs.push({id: keys[key], time: maxSet[keys[key]]});
}
console.log(resultObjs);

答案 1 :(得分:1)
我认为,你正在寻找这个,它会给最大time
一个唯一的id
: -
{
"_source":false,
"aggs": {
"byId": {
"terms": {
"field": "id"
},
"aggs": {
"byTime": {
"max": {
"field": "time"
}
}
}
}
}
}