Python操作Elasticsearch完全手册,使用elasticsearch6.8.2库包

鬼谷子叔叔 - 2023-12-14 10:28

介绍

介绍


环境


安装

安装

安装最新6.8.2版本的elasticsearch库包。

pip install elasticsearch==6.8.2

使用

初始化

导入elasticsearch包里的Elasticsearch对象,初始化Elasticsearch对象。

如果你的环境里的Elasticsearch不需要用户名和密码的话,参数http_auth可以省略。

from elasticsearch import Elasticsearch

tfes = Elasticsearch(hosts=["192.168.168.68"], http_auth=("elastic", "abcdef"))

查看索引列表

使用indices查看索引列表,这里还可以得到索引的settings、mappings以及aliases信息。

allIndexes = tfes.indices.get("*")
for index in allIndexes:
    print(index)
    print("aliases", allIndexes[index].get("aliases"))
    print("mappings", allIndexes[index].get("mappings"))
    print("settings", allIndexes[index].get("settings"))

创建索引


插入数据


批量插入数据


查询数据


更新数据


批量更新数据


创建索引别名


使用索引别名查询数据


删除数据


批量删除数据


删除索引


重建索引reindex

使用reindex方法重建索引,这里需要我们组织body内容,请求地址参数通过params传入。

reindexRet = tfes.reindex({
    "source": {
        "index": "tfsearch",
        "size": 1000
    },
    "dest": {
        "index": "tfsearch_v2"
    }
}, params={
    "wait_for_completion": "false"
})

查看任务


总结