Filter on latest document of each group in Elasticsearch Filter on latest document of each group in Elasticsearch elasticsearch elasticsearch

Filter on latest document of each group in Elasticsearch


You can do this with bucket selector aggregation(Only ES 2.x). I am basically comparing max date of each student with max date when they got F grade(filtering) and only retaining results where both dates are same. You can remove top hits aggregation if you want, it is just there to get that particular record where student failed.

{  "size": 0,  "aggs": {    "group_by_students": {      "terms": {        "field": "student"      },      "aggs": {        "only_f_grade_bucket": {          "filter": {            "term": {              "grade": "F"            }          },          "aggs": {            "latest_date": {              "max": {                "field": "date"              }            },            "top_hit":{              "top_hits": {                "size": 1              }            }          }        },        "max_date": {          "max": {            "field": "date"          }        },        "latest_failure": {          "bucket_selector": {            "buckets_path": {              "failed_date": "only_f_grade_bucket.latest_date",              "max_date": "max_date"            },            "script": "failed_date == max_date"          }        }      }    }  }}