两个字段上的 Elasticsearch 百分位数聚合

Elasticsearch Percentile Aggregation on two fields

我想从分布在两个字段中的一组值中检索 Q1 和 Q2。

我的文件看起来像:

{
   amount: 100,
   type: "CREDIT"
}
{
   amount: 80,
   type: "DEBIT"
}

这些金额分别代表 100 和 -80 的实际值。

假设我的 collection 的所有值都是 [100, -80, 20, -30, 50],那么我的百分位数将是 Q1=-30Q3=-50

我如何编写一个查询来告诉 elasticsearchhc type: "DEBIT" 的文档需要被视为负值,然后执行百分位数聚合?

您可以使用脚本来执行百分位数聚合。下面的脚本检查类型是否为借记然后 return 负值否则 return 正值

"aggs": {
    "percentile_val": {
      "percentiles": {
        "script": {
          "lang": "painless",
          "source": "if( doc['type.keyword'].value=='DEBIT') { return -doc['amount'].value;} else {doc['amount'].value}"
        }
      }
    }
  }