今天在用 python 跑 elasticsearch 时,代码如下:

from elasticsearch import Elasticsearch

es = Elasticsearch(
    [{'host': 'localhost', 'port': 9200}], timeout=3600
)

query = {
    "query": {
        "match_all": {}
    }
}

result = es.search(index="shopping", body=query)
print(result)

此时我本地 es 里是有名为 shopping 的索引的,而且索引中也有些数据,但运行时,报错如下:

C:/Users/th1nker/PycharmProjects/orders/test/test.py:3: DeprecationWarning: The 'timeout' parameter is deprecated in favor of 'request_timeout'
  es = Elasticsearch(
Traceback (most recent call last):
  File "C:/Users/th1nker/PycharmProjects/orders/test/test.py", line 3, in <module>
    es = Elasticsearch(
  File "C:\Users\th1nker\AppData\Local\Programs\Python\Python38\lib\site-packages\elasticsearch\_sync\client\__init__.py", line 331, in __init__
    node_configs = client_node_configs(
  File "C:\Users\th1nker\AppData\Local\Programs\Python\Python38\lib\site-packages\elasticsearch\_sync\client\utils.py", line 105, in client_node_configs
    node_configs = hosts_to_node_configs(hosts)
  File "C:\Users\th1nker\AppData\Local\Programs\Python\Python38\lib\site-packages\elasticsearch\_sync\client\utils.py", line 154, in hosts_to_node_configs
    node_configs.append(host_mapping_to_node_config(host))
  File "C:\Users\th1nker\AppData\Local\Programs\Python\Python38\lib\site-packages\elasticsearch\_sync\client\utils.py", line 221, in host_mapping_to_node_config
    return NodeConfig(**options)  # type: ignore
TypeError: __init__() missing 1 required positional argument: 'scheme'

Process finished with exit code 1

这让我有点摸不着头脑,查了网上的 python 操作 es 的入门教程,都没有提到 scheme 这个参数。

最后一想,可能是版本问题,我使用的这个 python 中的 es 库版本为 8.1.0,本地的 es 服务器版本为 8.1.1。可能是 python 中 es 版本太高的原因。于是把本地的 es 服务器版本降到 5.1.1(此时用的是 elasticsearch-rtf,该 es 版本为 5.1.1),把 python 的 es 库版本降到 5.1.0,结果就可以正常运行了

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐