0

The following is the record of 10 devices for every few minutes. I need to return unique set of the record for each id and each should be only the latest.

How can I do that with elastic search or any other solution would be good.

{
    {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
}
2
  • Just to clarify, you are trying to return a collection of objects, one for each id and it should be the object with the latest time value for its respective id? Commented Apr 24, 2017 at 22:56
  • @ken out of 10000 records, I will return only 10 records with unique id and latest/largest time value. Commented Apr 24, 2017 at 23:04

2 Answers 2

1

This is as close as I can get. Not sure how you would choose which 10 results to return if you had say over 10 unique id's in your original dataset.

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ken, but I'm working on to resolve it via elastic search because the actual problem is more complicated.
1

I think , You are looking for this , It will give max time with unique id :-

{
    "_source":false,
    "aggs": {
        "byId": {
            "terms": {
                "field": "id"
            },
            "aggs": {
                "byTime": {
                    "max": {
                        "field": "time"
                    }
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.