【问题标题】:Elasticsearch : How to do 'group by' with painless in scripted fields?Elasticsearch:如何在脚本字段中轻松进行“分组”?
【发布时间】:2020-05-20 08:35:17
【问题描述】:

我想使用无痛做以下事情:

select day,sum(price)/sum(quantity) as ratio
from data 
group by day

有可能吗?

我想这样做是为了在 kibana 中可视化比率字段,因为 kibana 本身没有划分聚合值的能力,但我很乐意听取脚本字段之外的替代解决方案。

【问题讨论】:

    标签: elasticsearch group-by elasticsearch-painless


    【解决方案1】:

    是的,有可能,您可以使用 bucket_script pipeline 聚合来实现:

    {
      "aggs": {
        "days": {
          "date_histogram": {
            "field": "dateField",
            "interval": "day"
          },
          "aggs": {
            "price": {
              "sum": {
                "field": "price"
              }
            },
            "quantity": {
              "sum": {
                "field": "quantity"
              }
            },
            "ratio": {
              "bucket_script": {
                "buckets_path": {
                  "sumPrice": "price",
                  "sumQuantity": "quantity"
                },
                "script": "params.sumPrice / params.sumQuantity"
              }
            }
          }
        }
      }
    }
    

    更新:

    您可以通过Transform API 使用上述查询,这将在源索引之外创建一个聚合索引。

    例如,我在测试索引中索引了一些文档,然后我们可以空运行上述聚合查询,以查看目标聚合索引的样子:

    POST _transform/_preview
    {
      "source": {
        "index": "test2",
        "query": {
          "match_all": {}
        }
      },
      "dest": {
        "index": "transtest"
      },
      "pivot": {
        "group_by": {
          "days": {
            "date_histogram": {
              "field": "@timestamp",
              "calendar_interval": "day"
            }
          }
        },
        "aggregations": {
          "price": {
            "sum": {
              "field": "price"
            }
          },
          "quantity": {
            "sum": {
              "field": "quantity"
            }
          },
          "ratio": {
            "bucket_script": {
              "buckets_path": {
                "sumPrice": "price",
                "sumQuantity": "quantity"
              },
              "script": "params.sumPrice / params.sumQuantity"
            }
          }
        }
      }
    }
    

    响应如下所示:

    {
      "preview" : [
        {
          "quantity" : 12.0,
          "price" : 1000.0,
          "days" : 1580515200000,
          "ratio" : 83.33333333333333
        }
      ],
      "mappings" : {
        "properties" : {
          "quantity" : {
            "type" : "double"
          },
          "price" : {
            "type" : "double"
          },
          "days" : {
            "type" : "date"
          }
        }
      }
    }
    

    您在preview 数组中看到的是将在transtest 目标索引中编入索引的文档,然后您可以在 Kibana 中将其可视化为任何其他索引。

    所以转换的实际作用是运行我在上面给你的聚合查询,然后它将每个桶存储到另一个可以使用的索引中。

    【讨论】:

    • 感谢您的回答!我不明白在 json 中创建的比率如何在 kibana 中可视化...我应该使用比率创建一个新索引吗?
    • 我用一个更具说明性的例子更新了我的答案
    • 非常感谢!!!这几天我一直在找这个! :)
    【解决方案2】:

    我找到了一个在 kibana 中通过 TSVB 可视化获得总和比率的解决方案。 您可以查看图片here 以查看示例。

    首先,您必须创建两个总和聚合,一个对价格求和,另一个对数量求和。然后,您选择“桶脚本”聚合来划分上述总和,使用无痛脚本。

    我发现的唯一缺点是您不能在多个列上进行聚合。

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2022-08-11
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      相关资源
      最近更新 更多