如何在范围聚合中使用bucket脚本?

jrcvhitl  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(0)|浏览(216)

我的问题:

GET orders_2020_06_11/_search
{
  "size": 0,
  "track_total_hits": true,
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "date1"
          }
        },
        {
          "exists": {
            "field": "date2"
          }
        }
      ]
    }
  },
  "aggs": {
    "all_data": {
      "value_count": {
        "field": "id_data"
      }
    },
    "rates": {
      "range": {
        "script": {
          "lang": "painless",
          "source": "(doc['date2'].value.toInstant().toEpochMilli() - doc['date1'].value.toInstant().toEpochMilli()) / 3600000"
        },
        "ranges": [
          {
            "to": 24,
            "key": "< 24h"
          },
          {
            "from": 24,
            "to": 48,
            "key": "24h-48h"
          },
          {
            "from": 48,
            "to": 72,
            "key": "48h-72h"
          },
          {
            "from": 72,
            "to": 96,
            "key": "72h-96h"
          },
          {
            "from": 96,
            "key": "> 96h"
          }
        ]
      }
    }
  }
}

通过对每个范围的查询,我得到了文档的数量,但我想计算百分比。
我尝试在我的范围脚本中执行此操作:

"script": {
          "lang": "painless",
          "params": {                     
            "allData": "all_data.value"
          },
          "source": "((doc['otd_datetime_start_load'].value.toInstant().toEpochMilli() - doc['datetime_first_liv'].value.toInstant().toEpochMilli()) / 3600000) / allData * 100"
        },

直接在脚本中,但我不能访问所有的\u数据,也不能使用bucket脚本。你有办法解决这个问题吗?
谢谢:)
样品:

"hits" : [
    {
        "_index" : "my_index",
        "_type" : "_doc",
        "_score" : 0.0,
        "_source" : {
          "id_data" : "1",
          "date1" : "2020-10-30 11:25",
          "date2" : "2020-10-30 00:00"
        }
    },
    {
        "_index" : "my_index",
        "_type" : "_doc",
        "_score" : 0.0,
        "_source" : {
          "id_data" : "2",
          "date1" : "2020-10-30 11:25",
          "date2" : "2020-10-30 00:00"
        }
    }
]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题