Elasticsearch '[bool] 无法解析字段 [filter]' 异常

Elasticsearch '[bool] failed to parse field [filter]' exception

我正在尝试解决我的搜索查询的解析异常。 我想在那里得到一些帮助:)

异常原因: '[1:65] [bool] 无法解析字段 [filter]' 留言:'x_content_parse_exception'

我的搜索:

data = (await this.elasticClient.search({
        index: Indexes.Measurements,
        size: 10000,
        body: {
          query: {
            bool: {
              filter: {
                terms: {
                  "MachineId": ["mo3", "mo2"]
                },
                range: {
                  '@timestamp': {
                    gte: `now-${lastMinutes}m`,
                    lte: 'now'
                  }
                }
              }
            },
          },
          sort : [{ "@timestamp" : "desc" }]
      }})).body.hits.hits.map(data => data._source);

您在 filter 子句周围缺少 [],试试下面的查询

data = (await this.elasticClient.search({
        index: Indexes.Measurements,
        size: 10000,
        body: {
          query: {
            bool: {
              filter: [{
                terms: {
                  "MachineId": ["mo3", "mo2"]
                }},{
                range: {
                  '@timestamp': {
                    gte: `now-${lastMinutes}m`,
                    lte: 'now'
                  }
                }}]
              }
            },
          },
          sort : [{ "@timestamp" : "desc" }]
      }})).body.hits.hits.map(data => data._source);

在JSON格式中,会是这样

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "MachineId": [
              "mo3",
              "mo2"
            ]
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "now-${lastMinutes}m",
              "lte": "now"
            }
          }
        }
      ]
    }
  }
}