【问题标题】:How to get bucket count with condition in elasticsearch?如何在elasticsearch中获取带有条件的桶数?
【发布时间】:2021-01-03 22:23:29
【问题描述】:

我想统计所有销售总额应大于 100 的唯一卖家。

我已经尝试过使用 术语聚合 的方法,但这会返回一个存储桶列表。但我想要的只是销售总额应大于 100 的卖家总数。

有没有办法使用 cardinality 或其他任何方法来做到这一点?

我也尝试过 cardinality 但没有奏效。

查询

{
    "size": 0,
    "aggregations": {
        "seller_count": {
            "terms": {
                "field": "seller_id"
            },
            "aggregations": {
                "total_sell": {
                    "sum": {
                        "field": "sell"
                    }
                },
                "sell_bucket_filter": {
                    "bucket_selector": {
                        "buckets_path": {
                            "totalSell": "total_sell"
                        },
                        "script": {
                            "source": "params.totalSell > 100"
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用stats bucket aggregation,获取seller_idsell 总和大于100 的计数

    添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例。

    索引映射:

    {
      "mappings": {
        "properties": {
          "sell": {
            "type": "integer"
          }
        }
      }
    }
    

    索引数据:

    {
      "seller_id": 1,
      "sell": 50
    }
    {
      "seller_id": 2,
      "sell": 50
    }
    {
      "seller_id": 2,
      "sell": 60
    }
    {
      "seller_id": 3,
      "sell": 60
    }
    {
      "seller_id": 3,
      "sell": 100
    }
    

    搜索查询:

    {
      "size": 0,
      "aggregations": {
        "seller_count": {
          "terms": {
            "field": "seller_id"
          },
          "aggregations": {
            "total_sell": {
              "sum": {
                "field": "sell"
              }
            },
            "sell_bucket_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "totalSell": "total_sell"
                },
                "script": {
                  "source": "params.totalSell > 100"
                }
              }
            }
          }
        },
        "bucketcount":{
          "stats_bucket":{
            "buckets_path":"seller_count._count"
          }
        }
      }
    }
    

    搜索结果:

    "aggregations": {
        "seller_count": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": 2,
              "doc_count": 2,
              "total_sell": {
                "value": 110.0
              }
            },
            {
              "key": 3,
              "doc_count": 2,
              "total_sell": {
                "value": 160.0
              }
            }
          ]
        },
        "bucketcount": {
          "count": 2,                 // note this
          "min": 2.0,
          "max": 2.0,
          "avg": 2.0,
          "sum": 4.0
        }
      }
    

    【讨论】:

    • @Rohit 你有没有机会通过答案,期待得到你的反馈:)
    • 感谢您的快速响应,但如果桶数超过 10000,则此方法存在一个问题,那么我必须使用复合聚合。复合聚合的问题在于,在索引中还有一个字段是“product_id”,我想计算一次查询中所有产品的卖家计数。
    • @Rohit 感谢您的回复 :) 但是您在问题中没有提到关于复合聚合或存储桶计数(大于 10000)的任何内容。由于这是您的不同用例,并且与您所要求的不同,因此如果您针对相同的问题发布一个单独的问题会很棒。由于最初发布的问题是仅计算存储桶的数量。
    • @Rohit 如果对您有帮助,请不要忘记投票并接受答案:)
    • 好的,我再问一个问题,谢谢。
    猜你喜欢
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多