gpt4 book ai didi

elasticsearch - Elasticsearch 唯一ID的唯一计数

转载 作者:行者123 更新时间:2023-12-03 00:44:33 24 4
gpt4 key购买 nike

假设我的数据如下所示:
项目6000的文件:
{“projectId”:“6000”,“price”:500000,“bedrooms”:2,“title”:“Affordable luxury”,“area”:“A”},
{“projectId”:“6000”,“价格”:700000,“卧室”:4,“标题”:“宽敞”,“区域”:“A”}
项目6001的文件:
{“projectId”:“6001”,“价格”:550000,“卧室”:2,“标题”:“令人惊叹”,“区域”:“A”}
{“projectId”:“6001”,“price”:650000,“bedrooms”:3,“title”:“Excellent views”。 “area”:“B”}
项目6002的文件:
{“projectId”:“6002”,“价格”:550000,“卧室”:2,“标题”:“令人惊叹”,“区域”:“B”}
{“projectId”:“6002”,“price”:650000,“bedrooms”:3,“title”:“Excellent views”。 “area”:“C”}
等等
现在,我想检索区域数,以获取存在项目的最流行区域,但仅考虑唯一的项目ID。我有这个查询:

    query = {
"aggs": {
"most_popular": {
"terms": {
"field": "area",
"size": 5,
"order": {
"_count": "desc"
},
}
}
}
结果是:
值区:[{'key':'A',doc_count:3},{'key':'B',doc_count:2} ...依此类推]
但是我想要的只是每个项目的唯一计数,因此A的doc_count应该是2而不是3,即避免在同一项目中重复。
在Elasticsearch中这怎么可能?

最佳答案

You need to use bucket sort aggregation that is a parent pipelineaggregation which sorts the buckets of its parent multi-bucketaggregation


添加带有索引数据,搜索查询和搜索结果的工作示例
索引数据:
{"projectId": "6000", "price": 500000, "bedrooms": 2, "title": "Affordable luxury", "area": "A"},

{"projectId": "6000", "price": 700000, "bedrooms": 4, "title": "Spacious", "area": "A"}

{"projectId": "6001", "price": 550000, "bedrooms": 2, "title": "Stunning", "area": "A"}

{"projectId": "6001", "price": 650000, "bedrooms": 3, "title": "Excellent views". "area": "B"}

{"projectId": "6002", "price": 550000, "bedrooms": 2, "title": "Stunning", "area": "B"}

{"projectId": "6002", "price": 650000, "bedrooms": 3, "title": "Excellent views". "area": "C"}

{"projectId": "6002", "price": 500000, "bedrooms": 2, "title": "Affordable luxury", "area": "C"}

{"projectId": "6002", "price": 500000, "bedrooms": 2, "title": "Affordable luxury", "area": "C"}
搜索查询:
    {
"size": 0,
"aggs": {
"most_popular": {
"terms": {
"field": "area",
"size": 5
},
"aggs": {
"unique": {
"cardinality": {
"field": "projectId"
}
},
"uniqueCount_bucket_sort": {
"bucket_sort": {
"sort": [
{
"unique": {
"order": "asc"
}
}
]
}
}
}
}
}
}
搜索结果:
"buckets": [
{
"key": "C",
"doc_count": 3,
"unique": {
"value": 1
}
},
{
"key": "B",
"doc_count": 2,
"unique": {
"value": 2
}
},
{
"key": "A",
"doc_count": 3,
"unique": {
"value": 2
}
}
]

关于elasticsearch - Elasticsearch 唯一ID的唯一计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64324071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com