3

每次执行此查询需要 200+ 毫秒:

{
  "filter": {
    "term": {
      "id": "123456",
      "_cache": true
    }
  }
}

但是这个每次在第一次查询后执行只需要 2-3 毫秒:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "id": "123456"
        }
      }
    }
  }
}

请注意两个查询中的相同 ID 值。看起来第二个查询使用了第一个查询的缓存结果。但是为什么第一个查询不能使用缓存结果本身呢?从第一个查询中删除"_cache" : true不会改变任何内容。

当我使用其他 ID 执行第二个查询时,第一次执行大约需要 40 毫秒,之后每次执行大约需要 2-3 毫秒。因此,第二个查询不仅运行速度更快,而且还缓存了结果并将缓存用于后续调用。

这一切有什么解释吗?

4

1 回答 1

5

The top-level filter element in the first request has very special function in Elasticsearch. It's used to filter search result without affecting facets. In order to avoid interfering with facets, this filter is applied during collection of results and not during searching, which causes its slow performance. Using top-level filter without facets makes very little sense because filtered and constant_score queries typically provide much better performance. If verbosity of filtered query with match_all bothers you, you can rewrite your second request into equivalent constant_score query:

{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "id": "123456"
        }
      }
    }
  }
}
于 2012-12-23T03:14:43.727 回答