Sorting by timestamp

Hi,
I am trying to get the last 3 consulted products :
so i need to make an aggregation based on productcode then use the option order .
here is what i am trying to do

{
"size" : 0,
"aggs" : {
    "products" : {
        "terms" : {
          "field" : "productcode",
          "order": {
            "timestamp" : "asc" 
          },
          "size" : 3
        }
    }
}
 }

this won't work for sure, but is there any solution for this problem?? any proposition will be helpful

Thank you!

I Found a solution for this problem:

{
"aggs": {
"products": {
"terms": {
"field": "code",
"order":{"first_hit":"desc"},
"size" : 3
},
"aggs": {
"first_hit": {
"max": {
"field" : "@timestamp"
}
}
}
}
}
}

I hope this will help someone :smiley:

1 Like