弹性搜索总和聚合值

时间:2021-05-31 05:07:51

标签: elasticsearch

我是 elasticsearch 的新手,现在正在努力解决这个问题。 我的文档如下所示:

{
  "productId": "156"
  "price": "101.23"
  "discount": "2.23"
  "marketplace": "some_marketplace"
  "categoryId": "256"
  "brandId": "356"
  "quantity": "10"
  "timestamp": "1622435597"
}

我想要达到的目标: 使用给定间隔内每天(或每月)给定类别的计算加权平均价格和折扣制作日期直方图,并计算所有产品平均数量的总和。 我当前的请求如下所示:

POST products/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "categoryId": "256"
          }
        }
      ], 
      "filter": [
        {
          "range": {
            "timestamp": {
              "gte": "1622037600",
              "lte": "1622246399"
            }
          }
        }
      ]
    } 
  },
  "aggs": {
    "group_day": {
      "date_histogram": {
        "field": "timestamp",
        "calendar_interval": "day",
        "format": "yyyy-MM-dd"
      },
      "aggs": {
        "avg_weight_price": {
          "weighted_avg": {
            "value": {
              "field": "price"
            },
            "weight": {
              "field": "quantity"
            }
          }
        },
        "avg_weight_discount": {
          "weighted_avg": {
            "value": {
              "field": "discount"
            },
            "weight": {
              "field": "quantity"
            }
          }
        },
        "group_by_product": {
          "terms": {
            "field": "productId"
          },
          "aggs": {
            "avg_quantity": {
              "avg": {
                "field": "quantity"
              }
            }
          }
        }
      }
    }
  }
}

这个查询的结果是:

"aggregations" : {
    "group_day" : {
      "buckets" : [
        {
          "key_as_string" : "2021-05-27",
          "key" : 1622073600000,
          "doc_count" : 18,
          "avg_weight_discount" : {
            "value" : 20.23
          },
          "group_by_product" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "100",
                "doc_count" : 6,
                "avg_quantity" : {
                  "value" : 9.5
                }
              },
              {
                "key" : "101",
                "doc_count" : 6,
                "avg_quantity" : {
                  "value" : 9.5
                }
              },
              {
                "key" : "102",
                "doc_count" : 6,
                "avg_quantity" : {
                  "value" : 9.5
                }
              }
            ]
          },
          "avg_weight_price" : {
            "value" : 130.20309941520466
          }
        },
        {
          "key_as_string" : "2021-05-28",
          "key" : 1622160000000,
          "doc_count" : 6,
          "avg_weight_discount" : {
            "value" : 2.23
          },
          "group_by_product" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "100",
                "doc_count" : 6,
                "avg_quantity" : {
                  "value" : 9.5
                }
              }
            ]
          },
          "avg_weight_price" : {
            "value" : 101.23
          }
        }
      ]
    }
  }

因此,我不希望在响应中包含那些 group_by_product 聚合和存储桶,我只需要每天计算一个值 - 存储桶中这些平均值的总和。例如,对于 27-05-2021,如果应该给我 28.5 和 28-05-2021 - 9.5(我每天为每个产品准备几个文档,这些平均值是每天的平均值,我想对所有产品的平均值求和以获得类别总和)

感谢您阅读本文。任何帮助将不胜感激

UPD:我试图将 sum_bucket 添加到最后

        "group_by_product": {
          "terms": {
            "field": "productId"
          },
          "aggs": {
            "avg_quantity": {
              "avg": {
                "field": "quantity"
              }
            }
          }
        }
      }
    },
    "avg_product_quantity_sum": {
      "sum_bucket": {
        "buckets_path": "group_day>group_by_product>avg_quantity"
      }
    }

但我收到以下错误

      "type" : "aggregation_execution_exception",
      "reason" : "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Object[]] at aggregation [group_by_product]"

看起来这不可能在一个表达式中写多个“>”,否则我错过了一些东西

0 个答案:

没有答案