In newer versions of the Elasticsearch Python client, there is indeed no transport_options
parameter named . To set transport options, transport_class
create a custom transport class using the parameters and set the options in the transport class.
Here is an example showing how to create a custom transport class to set transport options:
from elasticsearch import Elasticsearch, Transport
from elasticsearch.connection import RequestsHttpConnection
# 创建自定义传输类
class CustomTransport(Transport):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 设置传输选项
self.max_retries = 3 # 例如,设置最大重试次数
self.retry_on_timeout = True # 设置超时时是否重试
# 创建 Elasticsearch 客户端并使用自定义传输类
es = Elasticsearch(
hosts=['http://localhost:9200'], # 替换为您的 Elasticsearch 主机地址和端口
transport_class=CustomTransport
)
# 使用 create 方法创建索引
try:
result = es.indices.create(index='news', ignore=400)
print(result)
except Exception as e:
print(f"An error occurred: {
e}")
In this example, we create a CustomTransport
custom transport class named and set the transport options in it. transport_class
Then, we passed this custom transport class through the parameter when initializing the Elasticsearch client .
Please adjust the settings of the transport options according to your needs and make sure to configure them appropriately in the custom transport class. This way you can customize the transfer behavior to suit your requirements.