在 Elasticsearch DSL 中设置请求特定超时时遇到问题

Trouble setting request specific timeout in Elasticsearch DSL

我正在尝试使用 elasticsearch_dsl 为特定请求设置 timeout。我尝试了以下方法:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, F

...

def do_stuff(self, ids):
    client = Elasticsearch(['localhost'], timeout=30)
    s = Search(using=client,
               index= 'my_index',
               doc_type=['my_type'])
    s = s[0:100]
    f = F('terms', my_field=list(ids))
    s.filter(f)

    response = s.execute()
    return response.hits.hits

备注:

来自 elasticsearch_dsl docs 我什至尝试设置默认连接超时:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, F
from elasticsearch_dsl import connections

connections.connections.create_connection(hosts=['localhost'], timeout=30)

我仍然收到 10 秒超时错误。

所以出于某种原因,通过 .params() 添加参数似乎可以解决问题:

s = Search(using=client,
           index= 'my_index',
           doc_type=['my_type'])
    .params(request_timeout=30)

真正有趣的部分是查询现在只需不到一秒的时间 运行 并且索引仅在单个节点上。