I want to group by address then get latest address by date then filter this results by there status.
ex
{address: \'A\', date: \'10-10-1991\', status: \'so
This is possible with ES 2.x
using pipeline aggregations
. First we need to aggregate on addresses. Then we use two aggregations, one which gets the latest_date and other which gets latest date for sold status. Then we check if both dates match with bucket selector aggregation. This is how it looks.
{
"size": 0,
"aggs": {
"unique_address": {
"terms": {
"field": "address",
"size": 10
},
"aggs": {
"latest_date": {
"max": {
"field": "date"
}
},
"filter_sold": {
"filter": {
"term": {
"status": "sold"
}
},
"aggs": {
"latest_sold_date": {
"max": {
"field": "date"
}
}
}
},
"should_we_consider": {
"bucket_selector": {
"buckets_path": {
"my_var1": "latest_date",
"my_var2": "filter_sold>latest_sold_date"
},
"script": "my_var1 == my_var2"
}
}
}
}
}
}
Hope this helps!!