es restful api

Elasticsearch RESTful API 指南
本文介绍了如何使用Elasticsearch的RESTful API进行健康检查、节点检测和索引操作。通过/_cat/health获取集群健康状态,status为green、yellow、red分别代表健康、亚健康和危险。/_cat/nodes用于查看节点信息,/_cat/indices用于查看索引。还提到了如何使用POST方法添加文档。

健康类的api


集群文档数量

curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'

像上面这种请求你可以用curl,但我不是很习惯,我更习惯用jmeter去访问。



健康检测

/_cat/health?v 


最重要的就是 status了,green代表健康,yellow代表亚健康,red代表岌岌可危,要赶快处理了


节点检测

/_cat/notes?v



查看索引

/_cat/indices?v


啥都看不到,是因为我们还没有索引。


增删改查api

PUT /customer               #添加索引
PUT /customer/external/1    #添加document
{
  "name": "John Doe"
}
GET /customer/external/1    #获取document
DELETE /customer	    #删除索引


添加文档时,如果不传具体的id用post进行提交。

要在 Python 中连接 ElasticsearchRESTful API,通常使用 `elasticsearch` 官方 Python 客户端库。它封装了对 ElasticsearchREST API 调用,可以更方便地进行数据操作。 ### 1. 安装 Elasticsearch 客户端库 首先,你需要安装官方的 Python 客户端: ```bash pip install elasticsearch ``` ### 2. 连接 Elasticsearch 的基本代码 以下是一个简单的示例,展示如何使用 Python 连接到 Elasticsearch 服务: ```python from elasticsearch import Elasticsearch # 连接到本地的 Elasticsearch 实例 es = Elasticsearch(hosts=["http://localhost:9200"]) # 检查是否连接成功 if es.ping(): print("连接成功!") else: print("连接失败!") # 示例:获取集群信息 info = es.cluster.health() print(info) ``` ### 3. 使用用户名和密码认证连接 如果你的 Elasticsearch 启用了安全认证(如 Basic Auth),你可以这样连接: ```python es = Elasticsearch( hosts=["https://localhost:9200"], basic_auth=("username", "password") ) if es.ping(): print("认证连接成功!") else: print("认证连接失败!") ``` ### 4. 使用 API Key 连接(推荐用于生产环境) Elasticsearch 支持使用 API Key 进行身份验证: ```python from elasticsearch import Elasticsearch api_key = "base64encodedApiKey" # 替换为你的 API Key es = Elasticsearch( hosts=["http://localhost:9200"], api_key=api_key ) if es.ping(): print("使用 API Key 连接成功!") else: print("使用 API Key 连接失败!") ``` ### 解释: - `Elasticsearch()` 是构造函数,接受多个参数,包括主机地址、认证方式、超时设置等。 - `ping()` 方法用于测试连接。 - `cluster.health()` 是一个示例 API,用于获取集群的健康状态信息。 - 如果你使用的是远程服务器或启用了 HTTPS 的 Elasticsearch,需要正确配置 `hosts` 和协议。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值