Elasticsearch only one record based on userid?

In post index, postid is primary key and userid is foreign key.

i want all post but only post from one userid, such that only one user have the one post in results sort by postdate(optional latest first)

//Actual Result
[
    {
        userid: "u1",
        postid: "p1"
    },
    {
        userid: "u1",
        postid: "p2"
    },
    {
        userid: "u2",
        postid: "p3"
    },
    {
        userid: "u3",
        postid: "p4"
    },
    {
        userid: "u3",
        postid: "p5"
    },
    {
        userid: "u3",
        postid: "p6"
    }
]

needed as below

//Expecting Result
[
    {
        userid: "u1",
        postid: "p1"
    },
    {
        userid: "u2",
        postid: "p3"
    },
    {
        userid: "u3",
        postid: "p4"
    }
]

Answer

I think you can use top hit for this. Here the sample for this :

DELETE my-index-000001

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "userid": {
        "type": "keyword"
      },
      "postid": {
        "type": "keyword"
      },
      "postdate": {
        "type": "date"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{"userid": "u1", "postid": "p1", "postdate": "2021-03-01"}

PUT my-index-000001/_doc/2
{"userid": "u1", "postid": "p2", "postdate": "2021-03-02"}

PUT my-index-000001/_doc/3
{"userid": "u2", "postid": "p3", "postdate": "2021-03-03"}

PUT my-index-000001/_doc/4
{"userid": "u3", "postid": "p4", "postdate": "2021-03-04"}

PUT my-index-000001/_doc/5
{"userid": "u3", "postid": "p5", "postdate": "2021-03-05"}

PUT my-index-000001/_doc/6
{"userid": "u3", "postid": "p6", "postdate": "2021-03-06"}

These are the sample index creating steps. And here the query :

GET my-index-000001/_search
{
  "size": 0,
  "aggs": {
    "top_users": {
      "terms": {
        "field": "userid",
        "size": 100
      },
      "aggs": {
        "top": {
          "top_hits": {
            "sort": [
              {
                "postdate": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": [ "postdate", "postid" ]
            },
            "size": 1
          }
        }
      }
    }
  }
}

And, inside the resultset you can see the top post for the every users inside the aggregations:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "top_users" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "u3",
          "doc_count" : 3,
          "top" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "my-index-000001",
                  "_type" : "_doc",
                  "_id" : "6",
                  "_score" : null,
                  "_source" : {
                    "postdate" : "2021-03-06",
                    "postid" : "p6"
                  },
                  "sort" : [
                    1614988800000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "u1",
          "doc_count" : 2,
          "top" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "my-index-000001",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : null,
                  "_source" : {
                    "postdate" : "2021-03-02",
                    "postid" : "p2"
                  },
                  "sort" : [
                    1614643200000
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "u2",
          "doc_count" : 1,
          "top" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "my-index-000001",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : null,
                  "_source" : {
                    "postdate" : "2021-03-03",
                    "postid" : "p3"
                  },
                  "sort" : [
                    1614729600000
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}