我将搜索查询的超时设置为 10 毫秒,因此我预计 elasticsearch 搜索查询应在 10 毫秒内超时。
在响应中,我确实得到了 "timed_out":true
但查询似乎没有超时。它仍然会运行几百毫秒。
示例响应:
{
"took": 460,
"timed_out": true,
....
这是预期的行为还是我在这里遗漏了什么?我的目标是在查询花费的时间过长时终止查询,以免对集群造成负载。
最佳答案
查询超时
会发生什么?
在 timeout
设置下运行的 Elasticsearch 查询可能会从 Elasticsearch Guide 返回部分或空结果(如果超时已过期) :
The timeout parameter tells shards how long they are allowed to process data before returning a response to the coordinating node. If there was not enough time to process all data, results for this shard will be partial, even possibly empty.
Request Body Search parameters 的文档还告诉我们:
timeout
A search timeout, bounding the search request to be executed within the specified time value and bail with the hits accumulated up to that point when expired. Defaults to no timeout.
更多详情请咨询this page在指南中。
如何终止运行时间过长的查询?
看起来 Elasticsearch 没有最终答案,而是针对特定情况的几种解决方法。他们在这里。
没有办法保护系统免受 DoS 攻击(自 year 2015 起)。可以使用 timeout
或 terminate_after
查询参数来限制长时间运行的查询。 terminate_after
类似于 timeout
但它计算每个分片的文档数。这两个参数更像是对 Elasticsearch 的建议,意味着一些长时间运行的查询仍然可以通过所需的最大执行时间(例如 script
查询)。
从那以后Task Management API被引入并且监视和取消长时间运行的任务成为可能。这意味着您将不得不编写一些额外的代码来检查集群的健康状况并取消任务。
关于Elasticsearch 超时 true 但仍然得到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49167845/