Kibana 中的 Vega 可视化使用动态信号作为 Elasticsearch 查询参数

Vega visualization in Kibana using dynamic Signal as Elasticsearch query parameter

我正在尝试构建从 Elasticsearch 索引中查询数据并通过 Vega 信号与用户交互的 Vega 可视化。我有一个跟随用户鼠标拖动事件的信号“handleTime”。我想使用这个值作为弹性查询的参数来过滤所有小于信号值的事件。 这是我的代码示例:

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "signals": [
    {
      "name": "handleTime",
      "value": 60,
      "on": [{
        "events": "[@handle:mousedown, window:mouseup] > window:mousemove!",
        "update": "invert('x', clamp(x(), 0, width))"
      }]
    },
  ],
  // Define the data source
  "data": [{
    "name": "elastic",
    "url": {
      "index": "my-index-name",
      "body": {
        "query": {
          "bool": {
            "must": [
              {
                // ToDo: with constant '48' query works
                // "range" : { "totalTimeOnTarget": { "lte": 48 } }

                // But I want to use my signal value instead of constant, but next line of code doesn't work.
                "range" : { "totalTimeOnTarget": { "lte": { "%signal%": "handleTime" } } }
              }
            ],
        },
        "size": 10000
      }
    }
  ],

 // The rest vega chart code...
}

有人知道如何使用信号动态值作为查询参数吗?

我找到了答案。 In the documentation 可以使用表达式函数 kibanaAddFilter。重点是此函数将过滤器添加到 Kibana 上下文,然后在查询中使用此过滤器。但是要使用此功能,应该从 URL 中删除 query 部分并添加 "%context%": true 来代替。示例代码:

{
 "$schema": "https://vega.github.io/schema/vega/v5.json",
 "signals": [
  {
    "name": "deleteQuery",
    "on": [{
      "events": "mouseup",
      "update": '''kibanaRemoveAllFilters()'''
    }]
  },
  {
    "name": "updateQuery",
    "on": [{
      "events": "mouseup",
      "update": '''kibanaAddFilter({"bool": {"must": [{"range" : { "totalTimeOnTarget": { "lte": invert('x', x())} }}]}}}}, 'my-index-name')'''
    }]
  },
],
// Define the data source
"data": [{
  "name": "elastic",
  "url": {
    "index": "my-index-name",
    "body": {
      "size": 10000
    }
  }
],
// The rest vega chart code...
}