本文介绍了Elasticsearch的连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime(2010, 10, 10, 10, 10, 10)
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])
此简单代码将返回以下错误:
This simples code is returning the following error:
elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): Read timed out. (read timeout=10))
非常奇怪,因为服务器已准备就绪并已设置(返回了一些json)。
Very strange, because the server is ready and set (http://localhost:9200/ is returning some json).
推荐答案
默认情况下,超时值设置为10秒。如果要更改全局超时值,可以通过在创建对象时设置标志 timeout =您的时间来实现。
By default, the timeout value is set to 10 secs. If one wants to change the global timeout value, this can be achieved by setting the flag timeout=your-time while creating the object.
如果您已经创建了对象但未指定超时值,则可以使用查询中的 request_timeout =您的时间标志来为特定请求设置超时值。
If you have already created the object without specifying the timeout value, then you can set the timeout value for particular request by using request_timeout=your-time flag in the query.
es.search(index="my_index",
doc_type="document",
body=get_req_body(),
request_timeout=30)
这篇关于Elasticsearch的连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!