Joh*_*mes 67 python python-2.7 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'])
Run Code Online (Sandbox Code Playgroud)
这个简单的代码返回以下错误:
elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): Read timed out. (read timeout=10))
Run Code Online (Sandbox Code Playgroud)
很奇怪,因为服务器已准备好并设置(http:// localhost:9200 /正在返回一些json).
提前致谢!
Rah*_*hul 67
默认情况下,超时值设置为10秒.如果想要更改全局超时值,可以通过在创建对象时设置标志timeout = your-time来实现.
如果已创建对象而未指定超时值,则可以通过在查询中使用request_timeout = your-time标志来设置特定请求的超时值.
es.search(index="my_index",
doc_type="document",
body=get_req_body(),
request_timeout=30)
Run Code Online (Sandbox Code Playgroud)
Ale*_*lig 19
尝试在 Elasticsearch 初始化中设置超时:
es = Elasticsearch([{'host': HOST_ADDRESS, 'port': THE_PORT}], timeout=30)
Run Code Online (Sandbox Code Playgroud)
您甚至可以设置retry_on_timeout为True并提供max_retries一个可选数字:
es = Elasticsearch([{'host': HOST_ADDRESS, 'port': THE_PORT}], timeout=30, max_retries=10, retry_on_timeout=True)
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Amazon Elastic Search服务,则可能会出现连接超时问题.
es = Elasticsearch([{'host':'xxxxxx.us-east-1.es.amazonaws.com','port':443,'use_ssl':True}])
上面的python代码覆盖默认端口从9200到443并将ssl设置为true将解决此问题.
如果未指定端口,则它尝试连接到指定主机中的pprt 9200,并在超时后失败
这与将超时增加到30秒无关.人们真的认为弹性搜索需要长达30秒才能返回一个微小的命中吗?
我解决这个问题的方法是转到config/elasticsearch.yml 取消注释以下内容
http.port: 9200
network.host: 'localhost'
Run Code Online (Sandbox Code Playgroud)
Network.host可能设置为192.168.0.1可能有效但我只是将其更改为"localhost"
请注意,执行es.search(或es.index)时超时的常见原因之一是查询大小过大。例如,在我的 ES 索引大小非常大(> 3M 文档)的情况下,搜索 30 个单词的查询需要大约 2 秒,而搜索 400 个单词的查询需要 18 秒以上。因此,对于足够大的查询,即使 timeout=30 也救不了你。一个简单的解决方案是将查询裁剪为可以在超时以下回答的大小。
如果原因是流量,增加超时或超时重试将帮助您,否则这可能是您的罪魁祸首。
小智 5
elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): Read timed out. (read timeout=10))
表示请求没有在指定的时间内结束(默认情况下,超时=10)。
这将在 30 秒内起作用:
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc, timeout=30)
超时的原因可能有很多,似乎值得检查 elasticsearch 端的日志( logs/elasticsearch.log) 以查看详细的错误。在我们的例子中,ES 上的错误是:
primary shard is not active Timeout: [1m]
Run Code Online (Sandbox Code Playgroud)
正如这篇文章中所述,这是因为我们的磁盘已满。我们一天前调整了它(和分区)的大小来解决这个问题,但是如果高/低水位线被触及一次(我们使用的是 5.5.x),则需要重新启动 ES,但我们没有这样做。
只需在生产环境中重新启动 ES 即可解决我们的问题。
| 归档时间: |
|
| 查看次数: |
61385 次 |
| 最近记录: |