1

问题

我在观察者中链接不同的弹性搜索查询。我简短我想这样做:

  • 找到 container_ids
  • 对具有任何这些 container_id 的所有条目运行查询

第一个查询给了我一个数组。所以我想将该数组作为输入传递给terms query。麻烦的是ctx.payload...变量似乎只在字符串中扩展。

"query": {
  "terms" : {
    "container_id" : ctx.payload.first_query._value,
  }
}

会给我这样的错误:

无法识别的令牌“ctx”

有没有办法在不扩展为字符串的情况下插入数组?如果我使用“ctx.payload.first_query._value”,他将查询字符串"[id1, id2]"...

当前的解决方法

我目前已经实现了一个通过正则表达式传递的解决方法,但这似乎有点笨拙:

"input": {
  "chain": {
    "inputs" : [ {
      "container_ids" : {
        ...
      }
    }, {
      "container_id_regex": {
        "transform" : {
          "script": """
            def container_id_rexexp = "";
            def regexp_separator = "";
            for(def hit : ctx.payload.container_ids.hits.hits){
              container_id_rexexp += regexp_separator + hit._source.container_id;
              regexp_separator = "|";
            }
            return container_id_rexexp;
          """
        }
      }
    }, {
      "container_details": {
        "search": {
          "request": {
            ...
            "body": {
              "query": {
                "regexp": {
                  "container_id": {
                    "value": "{{ '{{ctx.payload.container_id_regex._value}}' }}"
                  }
                }
              }
            }
          }
        }
      }
    } ]
  }
}
4

0 回答 0