gpt4 book ai didi

elasticsearch - Elasticsearch-计算嵌套字段中元素的出现

转载 作者:行者123 更新时间:2023-12-02 23:06:48 25 4
gpt4 key购买 nike

我有一个具有这种简化结构的elasticsearch索引:

{
"id": "group1",
"users": [
{
"user_id": "user1"
},
{
"user_id": "user2"
}
]
},
{
"id": "group2",
"users": [
{
"user_id": "user1"
},
{
"user_id": "user3"
},
]
},
{
"id": "group3",
"users": [
{
"user_id": "user1"
},
{
"user_id": "user3"
},
]
}
我需要获取每个用户出现的文档数量。像这样:
[
{
"key": "user1",
"doc_count": 3
},
{
"key": "user2",
"doc_count": 1
},
{
"key": "user3",
"doc_count: 2
}
]

最佳答案

You need to use nested aggregation with the termsaggregation


添加带有索引映射,搜索查询和搜索结果的工作示例
索引映射:
{
"mappings":{
"properties":{
"users":{
"type":"nested"
}
}
}
}
搜索查询:
{
"size":0,
"aggs": {
"resellers": {
"nested": {
"path": "users"
},
"aggs": {
"unique_user": {
"terms": {
"field": "users.user_id.keyword"
}
}
}
}
}
}
搜索结果:
"aggregations": {
"resellers": {
"doc_count": 6,
"unique_user": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "user1",
"doc_count": 3
},
{
"key": "user3",
"doc_count": 2
},
{
"key": "user2",
"doc_count": 1
}
]
}
}
}

关于elasticsearch - Elasticsearch-计算嵌套字段中元素的出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64576311/

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