如何按日期和搜索词细化弹性搜索查询

How to refine elasticsearch query by date and search term

我有 Elastic 7。11.x - 我是 运行 下面的查询,它正在运行,但它 return 处理的数据太多了 - 如何添加日期范围过滤器到这种类型的查询?我在这里找到了一些示例,但它们已经过时且无法正常工作。

我想要return 匹配“源”字段并且在@timestamp 范围内(8/20/2021 - 8/21/2021 MST)的记录

POST metrics-general-dev-000049/_search
{
  "query": {
    "match": {
      "source": {
        "query": "investigation-data-depot"
      }
    }
  },
  "aggs": {
    "time_buckets": {
      "date_histogram": {
        "field": "@timestamp",
        "fixed_interval": "1d",
        "extended_bounds": {
          "min": "now-2d"
        },
        "min_doc_count": 0
      }
    }
  },
  "size": 0
}

您可以使用布尔查询结合范围过滤器:

"query": {
  "bool": {
    "filter": [
      { "match": ... },
      { "range": ... }
    ]
  }
}

参考:

https://www.elastic.co/guide/en/elasticsearch/reference/7.11/query-dsl-range-query.html#ranges-on-dates

https://www.elastic.co/guide/en/elasticsearch/reference/7.11/query-dsl-bool-query.html