Elastic Search 通过比较不同文档中的相同字段进行过滤

发布于 2025-01-17 01:39:43 字数 511 浏览 4 评论 0原文

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

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

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

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

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

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

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

我也在我的弹性查询中使用 from 和 size。 我尝试过使用折叠,但是根据大小,这给我带来的结果数量较少。

我使用的是弹性7.15.2

In my index, I have documents like this:

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

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

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

I want to write a query in such a way so that I can compare between between the name field between any 2 documents and get unique results. The result should be like this:

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

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

I am also using from and size in my elastic query.
I have tried using collapse but that gives me less number of results as per the size.

I am using elastic 7.15.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2025-01-24 01:39:43

您只需使用 条款聚合与top_hits(大小=1,按createdAt排序)。以下是对您提供的示例数据的工作查询。

{
    "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
                                    ]
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }

You can simply use the terms aggregation with top_hits(with size=1, sorted by createdAt). Below is the working query on sample data your provided.

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

And search result

"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
                                    ]
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文