Elastic Search Filter 通过比较来自不同文档的相同字段

Elastic Search Filter by comparing same field from different documents

在我的索引中,我有这样的文档:

{
    "name": "name",
    "createdAt": 1.6117508295E12
}

{
    "name": "name1",
    "createdAt": 1.6117508296E12
}

{
    "name": "name",
    "createdAt": 1.6117508297E12
}

我想以这种方式编写查询,以便我可以比较任意 2 个文档之间的名称字段并获得唯一结果。结果应该是这样的:

{
    "name": "name1",
    "createdAt": 1.6117508296E12
}

{
    "name": "name",
    "createdAt": 1.6117508297E12
}

我也在弹性查询中使用 from 和 size。 我试过使用 collapse,但根据大小,结果数量较少。

我正在使用 elastic 7.15.2

您可以简单地使用 terms aggregation 和 top_hits(大小为 1,按创建时间排序)。以下是对您提供的示例数据的工作查询。

{
    "size": 0,
    "aggs": {
        "unique": {
            "terms": {
                "field": "name.keyword"
            },
            "aggs": {
                "unique_names": {
                    "top_hits": {
                        "sort": [
                            {
                                "createdAt": {
                                    "order": "asc"
                                }
                            }
                        ],
                        "size": 1
                    }
                }
            }
        }
    }
}

和搜索结果

"aggregations": {
        "unique": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "name",
                    "doc_count": 2,
                    "unique_names": {
                        "hits": {
                            "total": {
                                "value": 2,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "71625371",
                                    "_id": "1",
                                    "_score": null,
                                    "_source": {
                                        "name": "name",
                                        "createdAt": 1.6117508295E12
                                    },
                                    "sort": [
                                        1.61175083E12
                                    ]
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "name1",
                    "doc_count": 1,
                    "unique_names": {
                        "hits": {
                            "total": {
                                "value": 1,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "71625371",
                                    "_id": "2",
                                    "_score": null,
                                    "_source": {
                                        "name": "name1",
                                        "createdAt": 1.6117508296E12
                                    },
                                    "sort": [
                                        1.61175083E12
                                    ]
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }