elasticsearch 有可能是聚合吗?

rqmkfv5c  于 2023-01-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(107)

我有一个关于聚合的问题。
我想对一个声明为对象数组的字段进行聚合。它不是对每个元素进行聚合,而是对整个值进行聚合。
我有以下文件:

PUT value-list-index
{
    "mappings": {
        "properties": {
            "server": {
                "type": "keyword"
            },
            "users": {
                "type": "keyword",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

PUT value-list-index/_doc/1
{
    "server": "server1",
    "users": ["user1"]
}

PUT value-list-index/_doc/2
{
    "server": "server2",
    "users": ["user1","user2"]
}

PUT value-list-index/_doc/3
{
    "server": "server3",
    "users": ["user2", "user3"]
}

PUT value-list-index/_doc/4
{
    "server": "server4",
    "users": ["user1","user2", "user3","user4"]
}

PUT value-list-index/_doc/5
{
    "server": "server5",
    "users": ["user2", "user3","user4"]
}

PUT value-list-index/_doc/6
{
    "server": "server6",
    "users": ["user3","user4"]
}

PUT value-list-index/_doc/7
{
    "server": "server7",
    "users": ["user1","user2", "user3","user4"]
}

PUT value-list-index/_doc/8
{
    "server": "server8",
    "users": ["user1","user2", "user3","user4"]
}

PUT value-list-index/_doc/9
{
    "server": "server9",
    "users": ["user1","user2", "user3","user4"]
}
get value-list-index/_search
{
  "size" : 0,
  "aggs": {
    "words": {
      "terms": {
        "field": "users"
      },
      "aggs": {
        "total": {
          "value_count": {
            "field": "users"
          }
        }
      }
    }
  }
}

我想要跟随

"aggregations" : {
    "words" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          **"key" : "user1",
          "doc_count" : 1,**
          "total" : {
            "value" : xx
          }
        },
        {
          **"key" : "user1","user2",
          "doc_count" : 1,**
          "total" : {
            "value" : xx
          }
        },
        {
          "key" : "user1","user2","user3","user4",
          "doc_count" : 4,
          "total" : {
            "value" : xx
          }
        }
      ]
    }
  }

但返回每个元素分组结果如下

"aggregations" : {
    "words" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "user2",
          "doc_count" : 7,
          "total" : {
            "value" : 23
          }
        },
        {
          "key" : "user3",
          "doc_count" : 7,
          "total" : {
            "value" : 23
          }
        },
        {
          "key" : "user1",
          "doc_count" : 6,
          "total" : {
            "value" : 19
          }
        },
        {
          "key" : "user4",
          "doc_count" : 6,
          "total" : {
            "value" : 21
          }
        }
      ]
    }
  }

我想要的聚合是否可行?

uajslkp6

uajslkp61#

也许这个aggs可以帮助你:Frequent items aggregation
但是要小心表演。
看看这个结果:

"aggregations": {
    "words": {
      "buckets": [
        {
          "key": {
            "users": [
              "user2"
            ]
          },
          "doc_count": 7,
          "support": 0.7777777777777778
        },
        {
          "key": {
            "users": [
              "user2",
              "user3"
            ]
          },
          "doc_count": 6,
          "support": 0.6666666666666666
        },
        {
          "key": {
            "users": [
              "user3",
              "user4"
            ]
          },
          "doc_count": 6,
          "support": 0.6666666666666666
        },
        {
          "key": {
            "users": [
              "user1"
            ]
          },
          "doc_count": 6,
          "support": 0.6666666666666666
        },
        {
          "key": {
            "users": [
              "user2",
              "user3",
              "user4"
            ]
          },
          "doc_count": 5,
          "support": 0.5555555555555556
        },
        {
          "key": {
            "users": [
              "user2",
              "user1"
            ]
          },
          "doc_count": 5,
          "support": 0.5555555555555556
        },
        {
          "key": {
            "users": [
              "user2",
              "user3",
              "user4",
              "user1"
            ]
          },
          "doc_count": 4,
          "support": 0.4444444444444444
        }
      ]
    }
  }

相关问题