TypeError: __init__() got an unexpected keyword argument ‘transport_options‘

In newer versions of the Elasticsearch Python client, there is indeed no transport_optionsparameter named . To set transport options, transport_classcreate 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 CustomTransportcustom transport class named and set the transport options in it. transport_classThen, 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.

Guess you like

Origin blog.csdn.net/rubyw/article/details/132809337