【问题标题】:Elasticsearch - Aggregate a script fieldElasticsearch - 聚合脚本字段
【发布时间】:2015-06-01 15:35:11
【问题描述】:

我正在尝试创建一个脚本字段,该字段将计算两个时间戳之间的时间差,然后在该脚本字段上聚合一个 avg

我第一次尝试:

{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "and": [
               {
                  "exists": {
                     "field": "time.new_time"
                  }
               },
               {
                  "exists": {
                     "field": "time.first_alert_time"
                  }
               }
            ]
         }
      }
   },
   "script_fields": {
      "timedifference": {
         "script": "doc['time.new_time'].value - doc['time.first_alert_time'].value"
      }
   },
   "aggs": {
      "avg_timedifference": {
         "avg": {
            "field" : "timedifference"
         }
      }
   }
}

这导致null 值低于聚合平均值avg_timedifference

然后我尝试了:

{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "and": [
               {
                  "exists": {
                     "field": "time.new_time"
                  }
               },
               {
                  "exists": {
                     "field": "time.first_alert_time"
                  }
               }
            ]
         }
      }
   },
   "script_fields": {
      "timedifference": {
         "script": "doc['time.new_time'].value - doc['time.first_alert_time'].value"
      }
   },
   "aggs": {
      "avg_timedifference": {
         "avg": {
            "script" : "doc['timedifference'].value"
         }
      }
   }
}

这会生成一条错误消息:“在映射中没有找到 [timedifference] 的字段”

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    简单地将脚本移动到聚合中怎么样?

    {
       "query": {
          "filtered": {
             "query": {
                "match_all": {}
             },
             "filter": {
                "and": [
                   {
                      "exists": {
                         "field": "time.new_time"
                      }
                   },
                   {
                      "exists": {
                         "field": "time.first_alert_time"
                      }
                   }
                ]
             }
          }
       },
       "aggs": {
          "avg_timedifference": {
             "avg": {
                "script" : "Math.ceil(doc['time.new_time'].value - doc['time.first_alert_time'].value)"
             }
          }
       }
    }
    

    【讨论】:

    • 这很好用,谢谢。这会产生一个浮点值,有没有办法运行另一个将数字舍入的脚本?
    • 我已经更新了我的答案,你可以使用任何Math 函数,如Math.ceil()Math.floor()Math.round(),只要你认为合适。
    • 嗯,当然可以,因为avg 聚合的结果很可能不是整数。你能在客户端处理它吗?
    • 如果没有其他办法,那么是的,我会在客户端处理。感谢您的所有帮助。
    • 感谢@Val,这太棒了,它给了我完成查询所需的提示。就我而言,我只是想根据脚本的输出创建一个直方图,并且一直失败。我迷失在 ES 文档中,希望能在 Histogram Agg 部分 (elastic.co/guide/en/elasticsearch/reference/current/…) 中找到有用的东西。但是,在该页面中没有对脚本的单一引用。您通常如何弄清楚如何在 ES 中编写查询?文档是否足够或您查看其他来源?
    猜你喜欢
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2016-11-23
    • 2015-07-30
    相关资源
    最近更新 更多