gpt4 book ai didi

elasticsearch - Elasticsearch获取不同行的计数

转载 作者:行者123 更新时间:2023-12-03 01:07:50 25 4
gpt4 key购买 nike

我需要使用elasticsearch查找不同字段ID的数量

我的数据格式是

{
"_index": "outboxprov1",
"_type": "user",
"_id": "16bcd4dc080f4c789018dd97f76741ef",
"_score": 1,
"_source": {
"first_name": "jinu",
"team_id": "500"
}
},
{
"_index": "outboxprov1",
"_type": "user",
"_id": "9ed8afe738aa63c28b66994cef1f83c6",
"_score": 1,
"_source": {
"first_name": "lal",
"team_id": "500"
}
},
{
"_index": "outboxprov1",
"_type": "user",
"_id": "1d238cd2f8c06790fc20859a16e3183b",
"_score": 1,
"_source": {
"first_name": "author1",
"team_id": "500"
}
},
{
"_index": "outboxprov1",
"_type": "user",
"_id": "616ee1c00a02564f71bb6c3067054d55",
"_score": 1,
"_source": {
"first_name": "kannan",
"team_id": "400"
}
},
{
"_index": "outboxprov1",
"_type": "user",
"_id": "d48132bfaed792f3c32d12e310d41c87",
"_score": 1,
"_source": {
"first_name": "author3",
"team_id": "400"
}
},
{
"_index": "outboxprov1",
"_type": "user",
"_id": "1a9d05586a8dc3f29b4c8147997391f9",
"_score": 1,
"_source": {
"first_name": "dibish",
"team_id": "100"
}
}

]
}

这里有三种不同的 team_id: 500, 400, 100。在这种情况下,我希望得到3。我尝试了基数聚合:
{
"size": 0,
"query" : {
"match_all" : { }
},
"aggs" : {
"team_id_count" : {
"cardinality" : {
"field" : "team_id"
}
}
}

}

这里得到正确的结果,但是我可以看到 elasticsearch文档指出基数是实验性功能,将来可能会发生变化。

没有使用基数聚合的方法,有什么方法可以实现?使用此实验性基数函数有什么问题吗?请指引我正确的方向。

最佳答案

您可以使用terms aggregation

像这样:

curl -XPOST http://localhost:9200/outboxprov1/user/_search -d '
{
"size": 0,
"query" : {
"match_all" : { }
},
"aggs" : {
"team_id_count" : {
"terms" : {
"field" : "team_id"
}
}
}

}'

关于elasticsearch - Elasticsearch获取不同行的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24669901/

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