gpt4 book ai didi

python - Docker 中的 Elasticsearch : elasticsearch. 异常。ConnectionError:ConnectionError - Python

转载 作者:行者123 更新时间:2023-12-03 02:22:52 26 4
gpt4 key购买 nike

我正在使用 elasticsearch.helpers.scan 函数在 python 中检索文档。它在本地运行良好,但是在 docker 上运行时出现以下 ConnectionError:

Traceback (most recent call last):
File "main.py", line 9, in <module>
from search_results import *
File "/app/search_results.py", line 9, in <module>
from BM25_Indexing import *
File "/app/BM25_Indexing.py", line 13, in <module>
results = get_data_es()
File "/app/Text_Cleaning.py", line 45, in get_data_es
return list(results_gen)
File "/usr/local/lib/python3.7/site-packages/elasticsearch/helpers/actions.py", line 439, in scan
body=query, scroll=scroll, size=size, request_timeout=request_timeout, **kwargs
File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 84, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/__init__.py", line 1548, in search
"GET", _make_path(index, doc_type, "_search"), params=params, body=body
File "/usr/local/lib/python3.7/site-packages/elasticsearch/transport.py", line 358, in perform_request
timeout=timeout,
File "/usr/local/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py", line 254, in perform_request
raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f5c77b6bd90>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f5c77b6bd90>: Failed to establish a new connection: [Errno 111] Connection refused)

此错误的python代码如下:
   def get_data_es():
ES_HOST = {
"host": "elastic",
"port": 9200
}

ES_INDEX = "radius_ml_posts"
ES_TYPE = {'author': 'vaibhav'}

es = Elasticsearch(hosts=[ES_HOST,])
count = 0
while True:
try:
es.ping()
print("Elasticsearch server bounded at url: {}".format(es.cat.master().split()[-2]))
results_gen = elasticsearch.helpers.scan(
es,
query={"query": {"match_all": {}}},
index=ES_INDEX
)
break
except elasticsearch.exceptions.ConnectionError:
time.sleep(5)
count += 1
print("Trying to connect to elasticsearch server. Retry number: {}".format(count))
print("Connections info: {}".format(es.transport.connection_pool.connections))
return list(results_gen)

我不知道如何解决这个问题。请帮忙。

编辑:采用@Lupanoide 建议的解决方案后,如下所示:
ES_HOST = os.environ['ES_HOST']

并按如下方式运行docker:
docker run -p 8080:8080 -e ES_HOST='192.168.1.101:9200' myimage

我收到以下连接超时错误:
Traceback (most recent call last):
File "main.py", line 9, in <module>
from search_results import *
File "/app/search_results.py", line 9, in <module>
from BM25_Indexing import *
File "/app/BM25_Indexing.py", line 13, in <module>
results = get_data_es()
File "/app/Text_Cleaning.py", line 44, in get_data_es
return list(results_gen)
File "/usr/local/lib/python3.7/site-packages/elasticsearch/helpers/actions.py", line 439, in scan
body=query, scroll=scroll, size=size, request_timeout=request_timeout, **kwargs
File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 84, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/__init__.py", line 1548, in search
"GET", _make_path(index, doc_type, "_search"), params=params, body=body
File "/usr/local/lib/python3.7/site-packages/elasticsearch/transport.py", line 358, in perform_request
timeout=timeout,
File "/usr/local/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py", line 254, in perform_request
raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError((<urllib3.connection.HTTPConnection object at 0x7fd4d21d4ed0>, 'Connection to 192.168.1.101 timed out. (connect timeout=10)')) caused by: ConnectTimeoutError((<urllib3.connection.HTTPConnection object at 0x7fd4d21d4ed0>, 'Connection to 192.168.1.101 timed out. (connect timeout=10)'))

最佳答案

确保在运行容器时暴露端口 9200。

例如,

如果主机系统中未使用该端口,

$ docker run --expose 80 ubuntu bash

或者,如果它已在主机中使用,请使用端口映射,
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

这会暴露容器的 80 端口,而不会将端口发布到主机系统的接口(interface)。

关于python - Docker 中的 Elasticsearch : elasticsearch. 异常。ConnectionError:ConnectionError - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61905342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com