在Elasticsearch DSL中设置请求特定超时时间出现问题

7

我正在尝试使用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

注:

  • 当我将 doc_type 更改为包含一百万个实体的类型时,查询运行良好。
  • 当我将 doc_type 指向数十亿个实体时,会出现超时错误,显示默认的10秒超时。

elasticsearch_dsl 文档 中,我甚至尝试设置默认连接超时时间:

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

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

我仍然收到了10秒超时错误。
1个回答

12

因某种原因,通过.params()添加参数似乎起作用:

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

真正有趣的部分是现在查询只需要不到一秒钟就能运行了,且索引仅在单个节点上。


3
参数 timeout=30 会导致以下错误:TransportError(400, 'parse_exception', 'Failed to parse setting [timeout] with value [30] as a time value: unit is missing or unrecognized')。您应该使用 request_timeout=30 代替。 - coda
1
@coda,你确定这是真的吗?也许只是因为它被缓存了。你重复搜索了吗,还是搜索了不同的内容? - toto_tico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接