Python Elasticsearch 连接超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28287261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:03:43  来源:igfitidea点击:

Connection Timeout with Elasticsearch

pythonpython-2.7elasticsearch

提问by Johann Gomes

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))

Very strange, because the server is ready and set (http://localhost:9200/is returning some json).

很奇怪,因为服务器已经准备好并设置好了(http://localhost:9200/正在返回一些 json)。

回答by Mir Ilias

elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): Read timed out. (read timeout=10))mean the request didn't end in the specified time (by default, timeout=10).

elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='localhost', port=9200): Read timed out. (read timeout=10))表示请求没有在指定的时间内结束(默认情况下,超时=10)。

This will work with 30 seconds :

这将在 30 秒内起作用:

res = es.index(index="test-index", doc_type='tweet', id=1, body=doc, timeout=30)

res = es.index(index="test-index", doc_type='tweet', id=1, body=doc, timeout=30)

回答by Rahul

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-timewhile creating the object.

默认情况下,超时值设置为 10 秒。如果要更改全局超时值,可以通过在创建对象设置标志timeout=your-time来实现。

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-timeflag in the query.

如果您已经在没有指定超时值的情况下创建了对象,那么您可以通过在查询中使用request_timeout=your-time标志为特定请求设置超时值。

es.search(index="my_index",
          doc_type="document",
          body=get_req_body(),
          request_timeout=30)

回答by Mukund

The connection timed out problem could occur if you are using Amazon Elastic Search service.

如果您使用 Amazon Elastic Search 服务,可能会出现连接超时问题。

es = Elasticsearch([{'host': 'xxxxxx.us-east-1.es.amazonaws.com', 'port': 443,  'use_ssl': True}])

The above python code where you override the default port from 9200 to 443 and setting the SSL to true will resolve the issue.

将默认端口从 9200 覆盖到 443 并将 SSL 设置为 true 的上述 python 代码将解决该问题。

If no port is specified, it is trying to connect to the port 9200 in the specified host and fails after time out

如果没有指定端口,则尝试连接指定主机的9200端口,超时失败

回答by whoopididoo

This is nothing to do with increasing your timeout to 30 seconds. Do people actually think that elastic search should need up to 30 seconds to return one tiny hit?

这与将超时增加到 30 秒无关。人们真的认为弹性搜索最多需要 30 秒才能返回一个微小的命中吗?

The way I fixed this problem was go to config/elasticsearch.ymluncomment the following

我解决这个问题的方法是去config/elasticsearch.yml取消注释以下内容

http.port: 9200
network.host: 'localhost' 

Network.host might be set to 192.168.0.1 which might work But I just changed it to 'localhost'

Network.host 可能设置为 192.168.0.1 这可能有效但我只是将其更改为“localhost”

回答by GGEv

my personal problem was solved with (timeout = 10000)which was practically never reached because the entries on server were only 7.000 but it had heavy traffic and its resources were being hogged and that was why the connection was dropping

我的个人问题得到了解决,(timeout = 10000)但实际上从未达到过,因为服务器上的条目只有 7.000,但流量很大,资源被占用,这就是连接断开的原因

回答by vlyubin

Note that one of the common reasons for timeouts when doing es.search(or es.index) is large query size. For example, in my case of a pretty large ES index size (> 3M documents), doing a search for a query with 30 words took around 2 seconds, while doing a search for a query with 400 words took over 18 seconds. So for a sufficiently large query even timeout=30 won't save you. An easy solution is to crop the query to the size that can be answered below the timeout.

请注意,执行es.search(或es.index)时超时的常见原因之一是查询大小过大。例如,在我的 ES 索引大小非常大(> 3M 文档)的情况下,搜索 30 个单词的查询需要大约 2 秒,而搜索 400 个单词的查询需要 18 秒以上。因此,对于足够大的查询,即使 timeout=30 也救不了你。一个简单的解决方案是将查询裁剪为可以在超时以下回答的大小。

Increasing timeout or doing retries on timeout will help you if the cause was in traffic, otherwise this might be your culprit.

如果原因是流量,增加超时或超时重试将帮助您,否则这可能是您的罪魁祸首。