在Elasticsearch中使用date_histogram聚合计算嵌套字段的总和

Cam*_*Cam 5 elasticsearch

我在使用 date_histogram 获取 Elasticsearch 中嵌套字段的总和时遇到困难,我希望有人能帮我一把。

我有一个如下所示的映射:

"client" : {
  // various irrelevant stuff here...

  "associated_transactions" : {
    "type" : "nested",
    "include_in_parent" : true,
    "properties" : {
      "amount" : {
        "type" : "double"
      },
      "effective_at" : {
        "type" : "date",
        "format" : "dateOptionalTime"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图获取一个 date_histogram,它显示所有客户按月的总收入 - 即一个时间序列,显示由 Associated_transactions. effective_date 确定的直方图中关联_transactions.amount 的总和。我尝试运行这个查询:

{
  "query": {
    // ...
  },
  "aggregations": {
    "revenue": {
      "date_histogram": {
        "interval": "month",
        "min_doc_count": 0,
        "field": "associated_transactions.effective_at"
      },
      "aggs": {
        "monthly_revenue": {
          "sum": {
            "field": "associated_transactions.amount"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但它给我的总和是不对的。ES 所做的似乎是找到在给定月份内有任何交易的所有客户,然后将这些客户的所有交易(任何时间)相加。也就是说,它是在给定月份内进行购买的客户一生中花费的金额总和,而不是给定月份内的购买总和。

有什么方法可以获取我正在寻找的数据,或者这是 ES 处理嵌套字段的限制?

预先非常感谢您的帮助!

大卫

svs*_*svs 1

尝试这个?

{
  "query": {
    // ...
  },
  "aggregations": {
    "revenue": {
      "date_histogram": {
        "interval": "month",
        "min_doc_count": 0,
        "field": "associated_transactions.effective_at"

        "aggs": {
          "monthly_revenue": {
            "sum": {
              "field": "associated_transactions.amount"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

即,将“aggs”键移至“date_histogram”字段中。