Elasticsearch:基本 CRUD 操作 - Python

在我之前的文章 "Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x",我详细讲述了如何建立 Elasticsearch 的客户端连接。我们也详述了如何对数据的写入及一些基本操作。在今天的文章中,我们针对数据的 CRUD (create, read, update 及 delete) 做更进一步的描述。

创建客户端连接接

我们需要安装 Elasticsearch 的依赖包:

复制代码
pip3 install elasticsearch
bash 复制代码
1.  $ pip3 install elasticsearch
2.  Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
3.  Requirement already satisfied: elasticsearch in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (8.12.0)
4.  Requirement already satisfied: elastic-transport<9,>=8 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from elasticsearch) (8.10.0)
5.  Requirement already satisfied: urllib3<3,>=1.26.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from elastic-transport<9,>=8->elasticsearch) (2.1.0)
6.  Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from elastic-transport<9,>=8->elasticsearch) (2023.11.17)
7.  $ pip3 list | grep elasticsearch
8.  elasticsearch                            8.12.0
9.  rag-elasticsearch                        0.0.1        /Users/liuxg/python/rag-elasticsearch/my-app/packages/rag-elasticsearch

我们使用如下的代码来建立一个客户端连接:

ini 复制代码
1.  from elasticsearch import Elasticsearch

3.  elastic_user = "elastic"
4.  elastic_password = "xnLj56lTrH98Lf_6n76y"

7.  url = f"https://{elastic_user}:{elastic_password}@localhost:9200"
8.  es = Elasticsearch(url, ca_certs = "./http_ca.crt", verify_certs = True)

10.  print(es.info())

在上面,我们需要使用自己的 Elasticsearch 集群的用户信息及证书代替上面的值。更多信息,请详细参阅文章 "Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x"。

创建文档

要添加新文档,你可以使用索引 API。 如果未指定文档 ID,Elasticsearch 会生成一个。 使用索引 API,你可以一次添加一个文档。

javascript 复制代码
1.  # Data to be indexed
2.  document = {
3.    "emp_id": 1,
4.    "age": 30,
5.    "email": "example@example.com",
6.    "name": "John Doe",
7.    "role": "Developer",
8.    "dob": "1992-01-01",
9.    "mobile_no": "1234567890",
10.    "educational": {
11.      "10": 87.5,
12.      "12": 90.0,
13.      "graduation": 8.4,
14.      "post_graduation": 9.1
15.    },
16.    "stack": ["Python", "Elasticsearch", "React"]
17.  }

19.  # Indexing the document
20.  response = es.index(index="emp_db", document=document)

我们可以在 Kibana 中进行查看:

bash 复制代码
GET emp_db/_search

响应将有以下内容:

  • _index:存储文档的索引的名称。
  • _id:分配给文档的唯一标识符。 如果您在为文档建立索引时未指定 ID,Elasticsearch 会自动生成一个 ID,如本例所示。
  • _version:文档的版本号。 对于新创建的文档,该值从 1 开始,并随着每次更新而递增。
  • result:指示操作完成,在本例中文档已创建。 如果文档已经存在并且已更新,则会显示 "updated"。

要一次添加多个文档,请使用 bulk API。 为了使用 bulk API,数据需要采用特定格式。 示例实际文档应位于 _source 中,每个文档应具有 _op_type 和 _index 。 它应该是这样的:

通过 bulk API 添加数据:

ini 复制代码
1.  actions = [ 
2.    {"_index": "emp_db", "_op_type": "create", "_source": {"field1": "value1"}}, 
3.    {"_index": "emp_db", "_op_type": "create", "_source": {"field2": "value2"}} 
4.    # Add more actions as needed 
5.  ]

7.  # List of data to be indexed, this could be in thousands.
8.  documents = [
9.    {
10.        "emp_id": 250349,
11.        "age": 26,
12.        "email": "abc@xyz.com",
13.        "name": "abc",
14.        "role": "Developer",
15.        "dob": "1997-01-01",
16.        "mobile_no": "12345678",
17.        "educational": {
18.          "10": 87.5,
19.          "12": 90.0,
20.          "graduation": 8.4,
21.          "post_graduation": 9.1
22.        },
23.        "stack": ["Python", "PySpark", "AWS"]
24.    },
25.    {
26.        "emp_id": 10789,
27.        "name": "abc",
28.        "age": 27,
29.        "email": "abc@xyz.com",
30.        "role": "linux admin",
31.        "dob": "1996-12-10",
32.        "mobile_no": "12345678",
33.        "educational": {
34.          "10": 87.5,
35.          "12": 90.0,
36.          "graduation": 8.4,
37.          "post_graduation": 9.1
38.        },
39.        "stack": ["Linux", "AWS"]
40.    },
41.    {
42.        "emp_id": 350648,
43.        "name": "Sandeep",
44.        "age": 27,
45.        "email": "def@xyz.com",
46.        "role": "seller support"
47.    }
48.  ]

50.  # Define your actions
51.  actions = [dict(**{'_index':'emp_db'}, **{'_op_type':'create'}, **{'_id':str(item['emp_id'])}, **{'_source':item}) for item in documents]

53.  # Import helpers for using bulk API
54.  from elasticsearch import helpers

56.  # Use the bulk helper to perform the actions
57.  bulk_response = helpers.bulk(es, actions)

所有批量操作都具有相同的响应结构。

Elasticsearch Python 客户端中 helpers.bulk 方法的输出 (3, []) 表明批量操作已成功执行。 让我们分解一下响应:

3 :这是在批量操作中成功处理的操作(例如索引、更新或删除文档)的数量。

\] :这个空列表表明批量操作期间没有错误。 如果存在任何错误,此列表将包含失败操作的错误详细信息。 * 3 :这是在批量操作中成功处理的操作(例如索引、更新或删除文档)的数量。 * \[\] :这个空列表表明批量操作期间没有错误。 如果存在任何错误,此列表将包含失败操作的错误详细信息。 ## 读写操作 要检索文档,请使用 get API 和文档 ID。 ```ini response = es.get(index="emp_db", id=250349) ``` ![](https://file.jishuzhan.net/article/1754326962874093570/7afd6b89f7b832d76629cccb65e9e2a6.webp) 除了 _index 、 _id 、 _version 之外 * found :表示在索引中找到了具有给定 id 的文档。 * _source :包含文档的实际数据。 使用 mget API 检索多个文档: ```ini 1. doc_ids = [ 2. {"emp_id":250349}, 3. {"emp_id":350648} 4. ] 6. # Define your actions 7. docs = [dict(**{'_index':'emp_db'}, **{'_id':str(item['emp_id'])}) for item in doc_ids] 9. # Retrieve the documents 10. response = es.mget(body={"docs": docs}) ``` ![](https://file.jishuzhan.net/article/1754326962874093570/a025faaefee6706bde70e06fe877d278.webp) ## 更新文档 要更新现有文档,请使用 update API。 这可以部分更新文档。 通过 update API,你可以一次添加一个文档。 ```javascript 1. document = { 2. "emp_id": 250349, 3. "role": "sr software engineer" 4. } 6. response = es.update(index="emp_db", id=document["emp_id"], doc=document) ``` 响应将有 _index 、 _id 、 _version 、 result 。 ![](https://file.jishuzhan.net/article/1754326962874093570/71798a5537a9a059533608105e861400.webp) 通过 bulk API 更新数据: ![](https://file.jishuzhan.net/article/1754326962874093570/3f6f9b06de5f638513a52aeb7d83f388.webp) ## 删除文档 要删除文档,请使用 delete API。 使用 delete API,你一次只能删除一个文档。 ```ini es.delete(index="emp_db", id=250349) ``` ![](https://file.jishuzhan.net/article/1754326962874093570/cc7e5527dad3642709ac170bbe3b7ed4.webp) 响应将有 _index 、 _id 、 _version 、 result 。 通过 bulk API 删除数据。 ```ini 1. # List of ids to be deleted, this could be in thousands. 2. documents = [ 3. { 4. "emp_id": 10789, 5. }, 6. { 7. "emp_id": 350648, 8. } 9. ] 11. # Define your actions 12. actions = [dict(**{'_index':'emp_db'}, **{'_op_type':'delete'}, **{'_id':str(item['emp_id'])}) for item in documents] 14. # Import helpers for using bulk API 15. from elasticsearch import helpers 17. # Use the bulk helper to perform the actions 18. response = helpers.bulk(es, actions) ``` ![](https://file.jishuzhan.net/article/1754326962874093570/439343e2e0f81e09fc001db5681e0385.webp) 恭喜,你已成功完成 CRUD 操作。 在这篇博客中,你了解了基本的 CRUD 操作。 完整的 jupyter notebook,请在地址下载:[github.com/liu-xiao-gu...](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fliu-xiao-guo%2Felasticsearch-python-notebooks%2Fblob%2Fmain%2Felasticsearch_crud.ipynb "https://github.com/liu-xiao-guo/elasticsearch-python-notebooks/blob/main/elasticsearch_crud.ipynb")

相关推荐
Keep Running *10 小时前
ElasticSearch_学习笔记
elasticsearch·搜索引擎
切糕师学AI10 小时前
Elasticsearch RRF(倒数排序融合)技术详解与实践
算法·elasticsearch·搜索引擎·混合搜索·rrf·归一化问题
Elasticsearch11 小时前
如何比较两个 Elasticsearch 索引并找出缺失的文档
elasticsearch
海兰11 小时前
使用 Elastic Workflows 监控 Kibana 仪表板访问数据
android·人工智能·elasticsearch·rxjava
希望永不加班12 小时前
SpringBoot 整合 Elasticsearch 实现全文检索
java·spring boot·后端·elasticsearch·全文检索
risc12345612 小时前
【Elasticsearch】副本分片(Replica Shard)的 globalCheckpoint 更新与推进机制
elasticsearch
Makoto_Kimur1 天前
Elasticsearch面试八股整理
elasticsearch
青稞社区.1 天前
Claude Code 源码深度解析:运行机制与 Memory 模块详解
大数据·人工智能·elasticsearch·搜索引擎·agi
Aktx20FNz1 天前
iFlow CLI 完整工作流指南
大数据·elasticsearch·搜索引擎
学习3人组1 天前
TortoiseGit冲突解决实战上机练习
大数据·elasticsearch·搜索引擎