【问题标题】:Elasticsearch Cardinality aggregation with condition带条件的 Elasticsearch 基数聚合
【发布时间】:2021-01-06 18:14:48
【问题描述】:

我想用条件进行基数聚合

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

有没有办法使用基数或其他方法来做到这一点?

我已尝试使用 Bucket selectorCardinality 以下查询,但没有成功。

映射

{
    "mappings": {
        "properties": {
            "product_id": {
                "type": "long"
            },
            "seller_id": {
                "type": "long"
            },
            "sell": {
                "type": "double"
            }
        }
    }
}

示例文档

[
    {
        "product_id": 1,
        "seller_id": 1,
        "sell": 70
    },
    {
        "product_id": 1,
        "seller_id": 1,
        "sell": 40
    },
    {
        "product_id": 1,
        "seller_id": 2,
        "sell": 10
    },
    {
        "product_id": 2,
        "seller_id": 1,
        "sell": 20
    },
    {
        "product_id": 2,
        "seller_id": 2,
        "sell": 120
    },
    {
        "product_id": 2,
        "seller_id": 3,
        "sell": 90
    },
    {
        "product_id": 2,
        "seller_id": 3,
        "sell": 20
    }
]

查询

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

预期样本结果

{
    "aggregations": {
        "products": {
            "buckets": [
                {
                    "key": 1,           // product_id
                    "seller_count": {
                        "value": 1      // 1 Seller is present whose sell sum is greater than 100
                    }
                },
                {
                    "key": 2,           // product_id
                    "seller_count": {
                        "value": 2      // 2 Seller is present whose sell sum is greater than 100
                    }
                }
            ]
        }
    }
}

【问题讨论】:

  • Rohit - 请添加 a) 示例文档 b) 映射 c) 确切需要获取的示例。
  • @SahilGupta 你现在可以帮忙吗?
  • @ECoder 你能帮忙吗?

标签: elasticsearch


【解决方案1】:

尝试以下查询(cmets 中的解释)

GET items/_search
{
  "size": 0,
  "aggs": {
    "products": {
      "terms": {
        "field": "product_id",
        "size": 10
      },
      "aggs": {
        "seller": {
          "terms": {
            "field": "seller_id",
            "size": 100
          },
          "aggs": {
            "sell_sum": {
              "sum": {
                "field": "sell"
              }
            },
            "sell_bucket_filter": { // <======== pipeline filter
              "bucket_selector": {
                "buckets_path": {
                  "totalSell": "sell_sum"
                },
                "script": {
                  "source": "params.totalSell > 100"
                }
              }
            }
          }
        },
        "seller_counts": { // <======== pipeline filter
          "stats_bucket": {
            "buckets_path": "seller>sell_sum"
          }
        }
      }
    }
  }
}

请注意:

  1. cardinality 聚合下不允许子聚合,即您的查询不起作用的原因

  2. bucket_selector 和 stats_bucket 都是管道聚合,适用于另一个聚合的输出。

  3. 如果卖家->size 设置为低于实际计数的值,seller_counts 将返回不正确的结果,因为聚合是管道聚合

【讨论】:

  • 感谢您的回答,但是没有固定尺寸,所以这不起作用有没有其他方法可以做到这一点?
猜你喜欢
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多