python引用elasticsearch库,初始化连接报错TypeError: init() missing 1 required positional argument: ‘scheme’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8 -*-

# 生成导入导出es的执行脚本

import requests
import re
from elasticsearch import Elasticsearch



es_source_host = "193.169.200.169"

es_source_port = 32155

es_source = Elasticsearch(

[{'host': es_source_host, 'port': es_source_port}])


indexs_source = es_source.indices.get_alias("*")
1
2
3
4
5
6
7
8
9
10
11
12
Traceback (most recent call last):
File "/Users/hy/Documents/code/scripts/elasticsearch-backup.py", line 10, in <module>
es_source = Elasticsearch(
File "/Users/hy/opt/anaconda3/envs/sygl/lib/python3.8/site-packages/elasticsearch/_sync/client/__init__.py", line 331, in __init__
node_configs = client_node_configs(
File "/Users/hy/opt/anaconda3/envs/sygl/lib/python3.8/site-packages/elasticsearch/_sync/client/utils.py", line 105, in client_node_configs
node_configs = hosts_to_node_configs(hosts)
File "/Users/hy/opt/anaconda3/envs/sygl/lib/python3.8/site-packages/elasticsearch/_sync/client/utils.py", line 154, in hosts_to_node_configs
node_configs.append(host_mapping_to_node_config(host))
File "/Users/hy/opt/anaconda3/envs/sygl/lib/python3.8/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'

根据提示,在连接信息中加入shema参数

1
es_target = Elasticsearch([{'host': es_target_host, 'port': es_target_port}],scheme='http')

仍然报错

1
2
3
4
Traceback (most recent call last):
File "/Users/hy/Documents/code/scripts/elasticsearch-backup.py", line 10, in <module>
es_source = Elasticsearch(
TypeError: __init__() got an unexpected keyword argument 'scheme'

查看elasticsearch包版本和服务端版本,发现pip安装的包为8.3.3,服务端为7.6.0

1
2
pip list |grep elasticsearch
elasticsearch 8.3.3

卸载并重新安装指定版本,代码正常运行

1
2
3
4
5
6
7
8
9
10
11
12
# 卸载
pip uninstall elasticsearch

# 查看可用版本
pip index versions elasticsearch
WARNING: pip index is currently an experimental command. It may be removed/changed in a future release without prior warning.
elasticsearch (8.10.1)
Available versions: 8.10.1, 8.10.0, 8.9.0, 8.8.2, 8.8.1, 8.8.0, 8.7.0, 8.6.2, 8.6.1, 8.6.0, 8.5.3, 8.5.2, 8.5.1, 8.5.0, 8.4.3, 8.4.2, 8.4.1, 8.4.0, 8.3.3, 8.3.2, 8.3.1, 8.3.0, 8.2.3, 8.2.2, 8.2.1, 8.2.0, 8.1.3, 8.1.2, 8.1.1, 8.1.0, 8.0.1, 8.0.0, 7.17.9, 7.17.8, 7.17.7, 7.17.6, 7.17.5, 7.17.4, 7.17.3, 7.17.2, 7.17.1, 7.17.0, 7.16.3, 7.16.2, 7.16.1, 7.16.0, 7.15.2, 7.15.1, 7.15.0, 7.14.2, 7.14.1, 7.14.0, 7.13.4, 7.13.3, 7.13.1, 7.13.0, 7.12.1, 7.12.0, 7.11.0, 7.10.1, 7.10.0, 7.9.1, 7.9.0, 7.8.1, 7.8.0, 7.7.1, 7.7.0, 7.6.0, 7.5.1, 7.1.0, 7.0.5, 7.0.4, 7.0.3, 7.0.2, 7.0.1, 7.0.0, 6.8.2, 6.8.1, 6.8.0, 6.4.0, 6.3.1, 6.3.0, 6.2.0, 6.1.1, 6.0.0, 5.5.3, 5.5.2, 5.5.1, 5.5.0, 5.4.0, 5.3.0, 5.2.0, 5.1.0, 5.0.1, 5.0.0, 2.4.1, 2.4.0, 2.3.0, 2.2.0, 2.1.0, 2.0.0, 1.9.0, 1.8.0, 1.7.0, 1.6.0, 1.5.0, 1.4.0, 1.3.0, 1.2.0, 1.1.1, 1.1.0, 1.0.0, 0.4.5, 0.4.4, 0.4.3, 0.4.2, 0.4.1


# 安装指定版本
pip install elasticsearch==7.6.0