弹性搜索查询根据父id获取最新的子文档

Elastic search query to get the latest child document based on the parent id

在下面的数据中,有

数据:

"doc":{
    "PublishedDate": "2018-07-04T03:00:00Z",
    "Id": "Article_3",
    "ContentId": "Content_1"
},
"doc":{
    "PublishedDate": "2018-07-03T03:00:00Z",
    "Id": "Article_2",
    "ContentId": "Content_1"
},
"doc":{
    "PublishedDate": "2018-07-02T03:00:00Z",
    "Id": "Article_1",
    "ContentId": "Content_1"
}


"doc":{
    "PublishedDate": "null",
    "Id": "Gallery_3",
    "ContentId": "Content_2"
},
"doc":{
    "PublishedDate": "2018-07-03T03:00:00Z",
    "Id": "Gallery_2",
    "ContentId": "Content_2"
},
"doc":{
    "PublishedDate": "2018-07-02T03:00:00Z",
    "Id": "Gallery_1",
    "ContentId": "Content_2"
}

当 "Content_1" 和 "Content_2" 作为输入参数传递时,有人可以帮我查询以获取如下结果。

预期结果为:

"doc":{
    "PublishedDate": "2018-07-04T03:00:00Z",
    "Id": "Article_3",
    "ContentId": "Content_1"
}
"doc":{
    "PublishedDate": "2018-07-03T03:00:00Z",
    "Id": "Gallery_2",
    "ContentId": "Content_2"
}

非常感谢任何帮助。谢谢。

首先按 contentID 聚合。 然后使用 top_hits.

检索最后一个事件

应该是这样的:

{
    "aggs": {
        "top_tags": {
            "terms": {
                "field": "ContentId",
                "size": 10
            },
            "aggs": {
                "last_event": {
                    "top_hits": {
                        "sort": [
                            {
                                "PublishedDate": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }
                }
            }
        }
    }
}