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

在较新版本的 Elasticsearch Python 客户端中,确实没有名为 transport_options 的参数。要设置传输选项,需要使用 transport_class 参数来创建自定义传输类,并在传输类中设置选项。

以下是一个示例,展示如何创建自定义传输类以设置传输选项:

python 复制代码
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}")

在这个示例中,我们创建了一个名为 CustomTransport 的自定义传输类,并在其中设置了传输选项。然后,我们在初始化 Elasticsearch 客户端时,通过 transport_class 参数传递了这个自定义传输类。

请根据您的需求来调整传输选项的设置,并确保在自定义传输类中进行适当的配置。这样,您可以自定义传输行为以满足您的要求。

相关推荐
二十雨辰13 小时前
[爬虫]-request
爬虫
Elasticsearch20 小时前
跳过编写告警规则:NGINX OTel 集成中内置 6 个现成的 ES|QL 模板
elasticsearch
狗都不学爬虫_20 小时前
AI逆向 - 99aq中心滑块验证+登录(wasm纯算)
爬虫·python·网络爬虫
Elasticsearch1 天前
缩小 AI 差距:下一代知识访问如何为政府解锁任务成果
elasticsearch
Elastic 中国社区官方博客1 天前
Elasticsearch:搜索教程 - 语义搜索(三)
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
hyf3266332 天前
泛程序:从零开始搭建稳定程序项目框架
运维·服务器·爬虫·百度·seo
西邮彭于晏2 天前
图文详解:Git分支创建、合并与冲突解决|新手零门槛完整教程
大数据·git·elasticsearch
Elastic 中国社区官方博客2 天前
Elastic 和 OpenAI 合作,将前沿智能引入非结构化企业数据
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai
Elastic 中国社区官方博客2 天前
Elasticsearch:搜索教程 - 全文搜索(一)
大数据·python·elasticsearch·搜索引擎·全文检索
西邮彭于晏2 天前
Git 标签(Tag)与版本发布完整指南|附全场景命令速查表
大数据·git·elasticsearch