python dict.get(key, default=None)/ XXX in dic:

本文详细介绍了Python字典中get方法的使用,包括如何通过指定键获取值,以及当键不存在时如何设置默认返回值。get方法是字典操作中非常实用的功能,能够有效避免程序因键不存在而引发错误。

one :dict.get(key, default=None)
返回指定键的值,如果值不在字典中返回default值

在这里插入图片描述

two:
在这里插入图片描述

class Elasticsearch(BaseClient): """ Elasticsearch low-level client. Provides a straightforward mapping from Python to Elasticsearch REST APIs. The client instance has additional attributes to update APIs in different namespaces such as ``async_search``, ``indices``, ``security``, and more: .. code-block:: python client = Elasticsearch("http://localhost:9200") # Get Document API client.get(index="*", id="1") # Get Index API client.indices.get(index="*") Transport options can be set on the client constructor or using the :meth:`~elasticsearch.Elasticsearch.options` method: .. code-block:: python # Set 'api_key' on the constructor client = Elasticsearch( "http://localhost:9200", api_key="api_key", ) client.search(...) # Set 'api_key' per request client.options(api_key="api_key").search(...) """ def __init__( self, hosts: t.Optional[_TYPE_HOSTS] = None, *, # API cloud_id: t.Optional[str] = None, api_key: t.Optional[t.Union[str, t.Tuple[str, str]]] = None, basic_auth: t.Optional[t.Union[str, t.Tuple[str, str]]] = None, bearer_auth: t.Optional[str] = None, opaque_id: t.Optional[str] = None, # Node headers: t.Union[DefaultType, t.Mapping[str, str]] = DEFAULT, connections_per_node: t.Union[DefaultType, int] = DEFAULT, http_compress: t.Union[DefaultType, bool] = DEFAULT, verify_certs: t.Union[DefaultType, bool] = DEFAULT, ca_certs: t.Union[DefaultType, str] = DEFAULT, client_cert: t.Union[DefaultType, str] = DEFAULT, client_key: t.Union[DefaultType, str] = DEFAULT, ssl_assert_hostname: t.Union[DefaultType, str] = DEFAULT, ssl_assert_fingerprint: t.Union[DefaultType, str] = DEFAULT, ssl_version: t.Union[DefaultType, int] = DEFAULT, ssl_context: t.Union[DefaultType, t.Any] = DEFAULT, ssl_show_warn: t.Union[DefaultType, bool] = DEFAULT, # Transport transport_class: t.Type[Transport] = Transport, request_timeout: t.Union[DefaultType, None, float] = DEFAULT, node_class: t.Union[DefaultType, t.Type[BaseNode]] = DEFAULT, node_pool_class: t.Union[DefaultType, t.Type[NodePool]] = DEFAULT, randomize_nodes_in_pool: t.Union[DefaultType, bool] = DEFAULT, node_selector_class: t.Union[DefaultType, t.Type[NodeSelector]] = DEFAULT, dead_node_backoff_factor: t.Union[DefaultType, float] = DEFAULT, max_dead_node_backoff: t.Union[DefaultType, float] = DEFAULT, serializer: t.Optional[Serializer] = None, serializers: t.Union[DefaultType, t.Mapping[str, Serializer]] = DEFAULT, default_mimetype: str = "application/json", max_retries: t.Union[DefaultType, int] = DEFAULT, retry_on_status: t.Union[DefaultType, int, t.Collection[int]] = DEFAULT, retry_on_timeout: t.Union[DefaultType, bool] = DEFAULT, sniff_on_start: t.Union[DefaultType, bool] = DEFAULT, sniff_before_requests: t.Union[DefaultType, bool] = DEFAULT, sniff_on_node_failure: t.Union[DefaultType, bool] = DEFAULT, sniff_timeout: t.Union[DefaultType, None, float] = DEFAULT, min_delay_between_sniffing: t.Union[DefaultType, None, float] = DEFAULT, sniffed_node_callback: t.Optional[ t.Callable[[t.Dict[str, t.Any], NodeConfig], t.Optional[NodeConfig]] ] = None, meta_header: t.Union[DefaultType, bool] = DEFAULT, http_auth: t.Union[DefaultType, t.Any] = DEFAULT, # Internal use only _transport: t.Optional[Transport] = None, ) -> None: 这是Elasticsearch9.0.2库的初始化参数
最新发布
07-10
<think>我们正在详细解释Elasticsearch 9.0.2 Python客户端中`Elasticsearch`类的`__init__`方法的各个参数。根据用户的问题,他想要了解这些参数的用途和配置方式。注意,用户之前提到了`url_prefix`被移除,现在使用`path_prefix`,但我们在参数列表中并没有直接看到`path_prefix`参数。实际上,`path_prefix`是通过`hosts`参数中的URL来设置的,或者通过`NodeConfig`来配置。不过,在初始化参数中,我们并没有一个独立的`path_prefix`参数,而是通过`hosts`字符串中包含路径来实现。 然而,在查看参数列表时,我们发现并没有`path_prefix`参数。实际上,在Elasticsearch Python客户端中,路径前缀是通过在`hosts`参数中指定基础路径来实现的。例如: hosts = "https://example.com/arius" 另外,在8.x版本之后,客户端使用`NodeConfig`来配置节点,其中可以设置`path_prefix`。但是,在`Elasticsearch`类的初始化参数中,我们并没有直接看到`path_prefix`,而是通过`hosts`字符串来传递。 因此,我们主要关注`__init__`方法中列出的参数。我们将参数分类进行解释,并给出配置示例。 参数分类: 1. 认证相关参数(API) 2. 节点连接相关参数(Node) 3. 传输层相关参数(Transport) 4. 其他内部参数 注意:有些参数使用了`DEFAULT`,表示使用库中定义的默认值。 下面我们逐一解释参数:</think>### Elasticsearch 9.0.2 Python 客户端 `__init__` 参数详解 #### 一、核心连接参数 1. **`hosts`** - 用途:指定 ES 集群节点地址列表 - 格式:字符串/列表,支持多种格式: ```python # 单节点 hosts="https://node1:9200" # 多节点 hosts=["https://node1:9200", "https://node2:9200"] # 带路径前缀 hosts="https://node1:9200/arius" # 替代旧版url_prefix ``` 2. **`cloud_id`** - 用途:连接 Elastic Cloud 服务的唯一标识 - 示例: ```python cloud_id="my_deployment:ZXVyb3BlLXdlc3Qx..." ``` --- #### 二、认证参数 3. **`api_key`** - 用途:API 密钥认证 - 格式:字符串或元组 `(id, secret)` ```python api_key="base64_encoded_key" # 或 api_key=("my_id", "my_secret") ``` 4. **`basic_auth`** - 用途:基础用户名/密码认证 - 格式:字符串 `"user:pass"` 或元组 `("user", "pass")` ```python basic_auth=("admin", "password123") ``` 5. **`bearer_auth`** - 用途:Bearer Token 认证 - 示例: ```python bearer_auth="jwt_token_string" ``` > **安全提示**:9.x 强制 TLS 加密,自签名证书需配置: > ```python > verify_certs=False, # 跳过证书验证 > ssl_show_warn=True # 仍显示警告 > ``` --- #### 三、节点配置参数 6. **`http_compress`** - 用途:启用请求压缩(节省带宽) - 默认:`False` ```python http_compress=True # 启用gzip压缩 ``` 7. **`ca_certs`** & **`client_cert`** - 用途:自定义 TLS 证书 - 示例: ```python ca_certs="/path/to/ca.pem", client_cert="/path/to/client.crt", client_key="/path/to/client.key" ``` 8. **`ssl_assert_fingerprint`** - 用途:证书指纹校验(避免 MITM 攻击) - 格式:SHA256 指纹字符串 ```python ssl_assert_fingerprint="A5:2B:...:EF" ``` --- #### 四、传输层优化参数 9. **`max_retries`** - 用途:请求失败最大重试次数 - 默认:`3` ```python max_retries=5 # 网络不稳定时增加 ``` 10. **`sniff_on_start`** - 用途:启动时自动发现集群节点 - 默认:`False` ```python sniff_on_start=True # 适用于动态集群 ``` 11. **`dead_node_backoff_factor`** - 用途:故障节点重试等待时间因子 - 公式:`backoff_time = min(dead_node_backoff_factor * 2^(retry_count), max_dead_node_backoff)` --- #### 五、高级配置示例 ```python from elasticsearch import Elasticsearch client = Elasticsearch( hosts=["https://es-node1:9200/arius", "https://es-node2:9200/arius"], basic_auth=("admin", "s3cret"), http_compress=True, max_retries=5, sniff_on_start=True, ssl_assert_fingerprint="B3:A2:...:D9", verify_certs=False # 自签名证书环境 ) ``` > **关键变更**: > - 9.x 移除 `url_prefix`,路径前缀需在 `hosts` 中直接指定 > - 默认强制 HTTPS,需显式处理证书验证[^1] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值