两个日期字段的结果之差然后取平均值

时间:2019-06-20 13:22:32

标签: elasticsearch kibana

我正在寻找弹性数据库中两个不同字段之间差异的平均值,我已经能够编写查询以返回最后1000个结果,但是我不确定如何去获得两个之间的差异每个结果然后获得总体平均值。

下面的弹性查询:

POST my_index/_search
{
  "size":1000,
  "_source": ["date.time.received","date.time.sent"],
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date.time.received": {
              "gte": "2019-06-19"
            }
          }
        },
        {
          "range": {
            "date.time.sent": {
              "gte": "2019-06-19"
            }
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我正在使用平均汇总和脚本

POST testindex5/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date.time.received": {
              "gte": "2019-06-19"
            }
          }
        },
        {
          "range": {
            "date.time.sent": {
              "gte": "2019-06-19"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "avg_resp": {
      "avg": {
        "script": "(doc['date.time.received'].value.toInstant().toEpochMilli()- doc['date.time.sent'].value.toInstant().toEpochMilli())/1000/86400"  ---> convert to days
      }
    }
  }
}