【问题标题】:Elasticsearch Group by a field and get the count of buckets which are having more than 2 documentsElasticsearch 按字段分组并获取包含 2 个以上文档的存储桶的数量
【发布时间】:2021-07-27 01:00:19
【问题描述】:

试图找到一种方法来获取聚合方面的存储桶计数,其中每个存储桶中至少包含两个文档。

能够获取存储桶,并保持足够大的大小以获取所有存储桶,但我真的很想知道如何获取存储桶总数:

"aggregations": {
    "by_universalId": {
        "terms": {
          "size": 10, 
            "field": "universalId",
            "min_doc_count": 2,
            "order": [
                {
                    "_count": "desc"
                },
                {
                    "_key": "asc"
                }
            ]
        }
    }
}

This GitHub 线程说,桶选择器是获取计数的正确方法,但无法找到方法。任何帮助表示赞赏,谢谢。

编辑1: 这是索引数据的样子:

{"id":"1", "universalId": "a"}
{"id":"2", "universalId": "a"}
{"id":"3", "universalId": "b"}
{"id":"4", "universalId": "b"}
{"id":"5", "universalId": "c"}
{"id":"6", "universalId": "c"}
{"id":"7", "universalId": "d"}
{"id":"8", "universalId": "d"}
{"id":"9", "universalId": "e"}
{"id":"10", "universalId": "e"}
{"id":"11", "universalId": "f"}
{"id":"12", "universalId": "f"}
{"id":"13", "universalId": "f"}
{"id":"14", "universalId": "g"}
{"id":"15", "universalId": "g"}
{"id":"16", "universalId": "g"}
{"id":"17", "universalId": "g"}
{"id":"18", "universalId": "h"}
{"id":"19", "universalId": "i"}
{"id":"20", "universalId": "j"}

当我运行这个查询时,我得到的计数是 5,而不是 7:

{
  "aggregations": {
      "by_universalId": {
        "terms": {
          "size": 5,
          "field": "universalId",
          "min_doc_count": 2,
          "order": [
            {
              "_count": "desc"
            },
            {
              "_key": "asc"
            }
          ]
        }
      },
      "bucketcount": {
        "stats_bucket": {
          "buckets_path": "by_universalId._count"
        }
      }
    }
}

这是我得到的:

"aggregations" : {
    "by_universalId" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 4,
      "buckets" : [
        {
          "key" : "g",
          "doc_count" : 4
        },
        {
          "key" : "f",
          "doc_count" : 3
        },
        {
          "key" : "a",
          "doc_count" : 2
        },
        {
          "key" : "b",
          "doc_count" : 2
        },
        {
          "key" : "c",
          "doc_count" : 2
        }
      ]
    },
    "bucketcount" : {
      "count" : 5,
      "min" : 2.0,
      "max" : 4.0,
      "avg" : 2.6,
      "sum" : 13.0
    }
  }

如果我将大小更改为 10,那么我会得到正确的计数,即 7。

无论我在聚合方面传递的大小如何,我都希望计数为 7。

Elasticsearch 版本详情:

"version" : {
    "number" : "7.9.2",
    "build_flavor" : "default",
    "build_type" : "deb",

【问题讨论】:

  • 您能否借助示例解释一下您的用例。如果您可以分享一些示例索引数据和预期的搜索结果,那就太好了
  • 我试图找到一个实体的重复项。如果两个文档具有相同的universalId,它们将被视为重复。现在我需要拉起至少有一个重复的实体的数量。类似于谷歌在安卓手机中显示的重复联系人。我将尝试创建示例索引数据和预期结果并更新帖子。

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

如果您想获得由术语聚合形成的桶的总数(最小文档计数为 2),那么您需要使用stats bucket aggregation

{
  "size": 0,
  "aggregations": {
    "by_universalId": {
      "terms": {
        "size": 10,
        "field": "universalId",
        "min_doc_count": 2,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    },
    "bucketcount": {
      "stats_bucket": {
        "buckets_path": "by_universalId._count"
      }
    }
  }
}

更新 1:

您正在使用 stats_bucket 聚合(使用上述搜索查询)获取存储桶数。默认情况下,terms 聚合中的 size 参数默认为 10。

size参数用于设置聚合结果中应该返回的桶数。如果您指定 "size :5",则只会返回 5 个存储桶,并且基于该统计信息,存储桶聚合将返回计数为 5。

在您的情况下,如果您甚至没有在 terms 聚合中传递 size 参数,您将得到计数为 7

【讨论】:

  • stats_bucket 是否依赖于术语聚合(by_universalId)中的size?因为如果提供了sizebucketcountsize 相同,否则它将回退到默认页面大小(在我的情况下是 10)
  • @Madhu stats_bucket 将为您提供由术语聚合形成的桶数。
  • @Madhu 现在您正在使用统计桶聚合来获取术语桶的数量。我已经更新了与size 参数相关的问题的答案。请仔细阅读更新后的答案,如果这能解决您的问题,请告诉我?
  • 是的,不通过size 在这种情况下有效,但如果桶的数量很大(比如 10,000),那么只需计算桶数,我将获取所有桶。有没有其他方法可以做到这一点,无需获取所有存储桶或将我随时获取的存储桶数量限制为一个小数字,但仍可以获得符合条件的存储桶总数?
  • @Madhu 据我所知,除了使用桶聚合(这里称为聚合)之外,没有其他方法可以找到重复项(最小文档计数为 2)。为此,如果您有 10k 个文档,您必须获取所有存储桶并相应地设置 size 参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多