gpt4 book ai didi

python - 使用python脚本连接ES

转载 作者:行者123 更新时间:2023-12-01 05:04:09 28 4
gpt4 key购买 nike

我正在尝试连接 ES 引擎并检索数据。

我正在使用以下脚本:

from elasticsearch import Elasticsearch as ES

print "Setup connection..."
es=ES(['http://elasticsearch......com:9200/cuevents-2014.34,cuevents-2014.33/_search?pretty'])
print "Done!"

print "Count number of users..."
print es.count(index='cuevents-2014.34')

但我收到了以下返回的消息。

Setup connection...
No handlers could be found for logger "elasticsearch"
Done!
Count number of users...
Traceback (most recent call last):
File "/home/es.py", line 8, in <module>
print es.count(index='cuevents-2014.34')
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 68, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 622, in count
params=params, body=body)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 284, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py", line 51, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(('Connection aborted.', gaierror(-2, 'Name or service not known'))) caused by: ProtocolError(('Connection aborted.', gaierror(-2, 'Name or service not known')))

我正在尝试连接并返回索引cuevents-2014.34中的文档数量

最佳答案

好的,我又仔细看了一遍你的代码。 Elasticsearch 构造函数需要指向根 URL - 您不应该在 URL 上指定索引。

from elasticsearch import Elasticsearch as ES

print "Setup connection..."
es=ES(['http://elasticsearch......com:9200/'])
print "Done!"

print "Count number of users..."
print es.count(index='cuevents-2014.34')

参见http://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch

如果您只想限制对某些索引的访问 - Elasticsearch 根本不提供访问控制。您可以通过反向代理上的 URL 重写来完成此操作。

关于python - 使用python脚本连接ES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25414860/

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