使用 FastAPI 构建 Elasticsearch API

165 阅读10分钟

作者:来自 Elastic Jeffrey Rengifo

学习如何使用 Pydantic 模式和 FastAPI 后台任务,通过实际示例构建一个 Elasticsearch API。

想获得 Elastic 认证吗?查看下一期 Elasticsearch Engineer 培训的时间!

Elasticsearch 拥有丰富的新功能,能帮助你为你的使用场景构建最佳的搜索解决方案。深入了解我们的示例笔记本,开始免费云试用,或立即在本地机器上体验 Elastic。


从《Elasticsearch in JavaScript》这篇文章中我们了解到,你不应该将 Elasticsearch 实例暴露在互联网上,而是应该构建一个 API 层。暴露集群的 URL、索引名称或 API 密钥会让攻击者更容易瞄准你的数据,并不必要地扩大你的攻击面。即使你对请求进行了清洗,攻击者仍然可以发送复杂或高负载的查询使集群超载。因此,最好不仅隐藏集群本身,还要隐藏查询逻辑 —— 只让用户控制必要的部分,比如搜索关键字,而不是整个查询。

对于 JavaScript 开发者来说,可以选择使用 Node.js,而对于 Python 开发者来说,FastAPI 是一个不错的替代方案。FastAPI 因其简洁性和开箱即用的高性能而受到广泛欢迎。

在本文中,我们将使用 FastAPI 构建 Elasticsearch 与客户端应用程序(通常是网页浏览器)之间的 API 层。然后,我们将探索一些可以通过 FastAPI 原生功能实现的常见用例。

你可以在这里找到该应用程序的笔记本。

准备数据

本文将使用一个包含兽医就诊记录的数据集。下面是一个示例文档:

`

1.  {
2.      "owner_name": "Marco Rivera",
3.      "pet_name": "Milo",
4.      "species": "Cat",
5.      "breed": "Siamese",
6.      "vaccination_history": [
7.          "Rabies",
8.          "Feline Leukemia"
9.      ],
10.      "visit_details": "Slight eye irritation, prescribed eye drops."
11.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

首先,我们需要安装 Elasticsearch 客户端,以便能够查询我们的集群:

`pip install elasticsearch`AI写代码

现在,我们导入 Elasticsearch 客户端、helpers 和 getpass,用于从终端捕获环境变量。

`

1.  from elasticsearch import Elasticsearch, helpers
2.  from getpass import getpass

4.  os.environ["ELASTICSEARCH_ENDPOINT"] = getpass("Elasticsearch endpoint: ")
5.  os.environ["ELASTICSEARCH_API_KEY"] = getpass("Elasticsearch api-key: ")

`AI写代码

我们定义索引名称,并使用 Elasticsearch 端点和 API 密钥初始化 Elasticsearch 客户端。

`

1.  ES_INDEX = "vet-visits"

3.  es_client = Elasticsearch(
4.      hosts=[os.environ["ELASTICSEARCH_ENDPOINT"]],
5.      api_key=os.environ["ELASTICSEARCH_API_KEY"],
6.  )

`AI写代码

然后我们来创建映射:

`

1.  es_client.indices.create(
2.          index=ES_INDEX,
3.          body={
4.              "mappings": {
5.                  "properties": {
6.                      "breed": {"type": "keyword"},
7.                      "owner_name": {
8.                          "type": "text",
9.                          "fields": {"keyword": {"type": "keyword"}},
10.                      },
11.                      "pet_name": {
12.                          "type": "text",
13.                          "fields": {"keyword": {"type": "keyword"}},
14.                      },
15.                      "species": {"type": "keyword"},
16.                      "vaccination_history": {"type": "keyword"},
17.                      "visit_details": {"type": "text"},
18.                  }
19.              }
20.          },
21.      )

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

最后,下载数据集并将其放在脚本所在的同一文件夹。然后,我们可以使用 Bulk API 将数据导入 Elasticsearch:

`

1.  def build_data(ndjson_file, index_name):
2.      with open(ndjson_file, "r") as f:
3.          for line in f:
4.              doc = json.loads(line)
5.              yield {"_index": index_name, "_source": doc}

8.  try:
9.      success, errors = helpers.bulk(es_client, build_data("vet-visits.ndjson", ES_INDEX))
10.      print(f"{success} documents indexed successfully")

12.      if errors:
13.          print("Errors during indexing:", errors)
14.  except Exception as e:
15.      print(f"Error: {str(e)}")

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

如果一切顺利,你应该会看到以下信息:

`10 documents indexed successfully`AI写代码

现在,数据已导入 Elasticsearch 并准备好使用。接下来,我们将构建 API 来展示 FastAPI 的功能。

Hello, world!

开始之前,我们只需要安装 FastAPI 和用于服务器创建的 UvicornPydantic 用于模式处理,以及 Elasticsearch 用于存储和搜索数据。

`pip install fastapi uvicorn elasticsearch pydantic -q`AI写代码

我们先导入库并创建 FastAPI 服务器实例。

`

1.  import asyncio
2.  import json
3.  import os
4.  from typing import List

6.  import uvicorn
7.  from fastapi import BackgroundTasks, Body, FastAPI, HTTPException, Response
8.  from pydantic import BaseModel

11.  app = FastAPI()

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

然后,我们可以创建一个 ping 端点来检查服务器状态。

`

1.  @app.get("/ping")
2.  async def ping():
3.      try:
4.          health = await es_client.cluster.health()

6.          return {
7.              "status": "success",
8.              "message": "Connected to Elasticsearch",
9.              "cluster_status": health["status"],
10.              "number_of_nodes": health["number_of_nodes"],
11.              "active_shards": health["active_shards"],
12.          }
13.      except Exception as e:
14.          status_code = getattr(e, "status_code", 500)

16.          raise HTTPException(
17.              status_code=status_code,
18.              detail=f"Error connecting to Elasticsearch: {str(e)}",
19.          )

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

请求:

`curl -XGET "http://localhost:8000/ping"`AI写代码

响应:

`{"status":"success","message":"Connected to Elasticsearch","cluster_status":"green","number_of_nodes":4,"active_shards":172}`AI写代码
`

1.  @app.post("/search")
2.  async def search(query: dict = Body(...)):
3.      try:
4.          result = await es_client.search(index=ES_INDEX, body=query)

6.          return result
7.      except Exception as e:
8.          status_code = getattr(e, "status_code", 500)

10.          raise HTTPException(status_code=status_code, detail=str(e))

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

我们将尝试使用 match_phrase 搜索牙齿清洁的就诊记录:

请求:

`

1.  curl -X POST "http://localhost:8000/search" \
2.    -H "Content-Type: application/json" \
3.    -d '{
4.      "query": {
5.        "match_phrase": {
6.          "visit_details": "dental cleaning"
7.        }
8.      },
9.      "size": 10
10.    }'

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

响应:

`

1.  {
2.      "took": 1,
3.      "timed_out": false,
4.      "_shards": {
5.          "total": 1,
6.          "successful": 1,
7.          "skipped": 0,
8.          "failed": 0
9.      },
10.      "hits": {
11.          "total": {
12.              "value": 1,
13.              "relation": "eq"
14.          },
15.          "max_score": 3.5869093,
16.          "hits": [
17.              {
18.                  "_index": "vet-visits",
19.                  "_id": "VUjqWZYB8Z9CzAyMLmyB",
20.                  "_score": 3.5869093,
21.                  "_source": {
22.                      "owner_name": "Leo Martínez",
23.                      "pet_name": "Simba",
24.                      "species": "Cat",
25.                      "breed": "Maine Coon",
26.                      "vaccination_history": [
27.                          "Rabies",
28.                          "Feline Panleukopenia"
29.                      ],
30.                      "visit_details": "Dental cleaning. Minor tartar buildup removed."
31.                  }
32.              }
33.          ]
34.      }
35.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

输入和响应的类型定义

FastAPI 的一个关键特性是与 Pydantic 的集成,用于数据模式处理,允许你使用类型注解定义类,并自动进行数据验证。在大多数情况下,给数据加类型对构建健壮稳定的应用至关重要。它还能实现模型复用,使代码更易读、维护和文档化。

不过,严格的类型和模式限制在需要处理高度动态或不可预测的数据结构时可能不太适用。

接下来,我们把搜索端点的请求和响应改成类型定义。

我们可以创建类来验证字段类型(field types)、设置默认值(default values)、定义枚举(enums)和列表(lists)等。这里我们将为用户请求和 Elasticsearch 响应创建类,并应用部分这些概念。

现在,用户只需发送搜索词,和可选的结果数量限制,就会返回包含 owner_name 和 visit_details 的命中列表。

`

1.  # Pydantic classes

3.  # Pydantic class for the request
4.  class SearchRequest(BaseModel):
5.      term: str
6.      size: int = 10

8.  # Pydantic class for the response
9.  class SearchResponse(BaseModel):
10.      hits: List[SearchHit]
11.      total: int

13.  # Class to format for hits
14.  class SearchHit(BaseModel):
15.      owner_name: str = ""
16.      visit_details: str = ""

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

现在这个端点对用户来说更简单,对我们来说更安全,因为用户只能控制查询的一部分,而不是全部。

`

1.  @app.post("/search", response_model=SearchResponse)
2.  async def search_v3(request: SearchRequest):
3.      try:
4.          query = {
5.              "query": {"match_phrase": {"visit_details": request.term}},
6.              "size": request.size,
7.          }

9.          result = await es_client.search(index=ES_INDEX, body=query)
10.          hits = result["hits"]["hits"]
11.          results = []

13.          for hit in hits:
14.              source = hit.get("_source", {})
15.              results.append(
16.                  SearchHit(
17.                      owner_name=source["owner_name"],
18.                      visit_details=source["visit_details"],
19.                  )
20.              )

22.          return SearchResponse(hits=results, total=len(results))
23.      except Exception as e:
24.          status_code = getattr(e, "status_code", 500)

26.          raise HTTPException(status_code=status_code, detail=str(e))

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

让用户完全访问 _search 查询体被认为是安全风险,因为恶意用户可能发送超载集群的查询。采用这种方式,用户只能设置 match_phrase 子句内的内容,使端点更安全。

响应也是如此。我们可以返回更简洁的结果,隐藏 _id、_score、_index 等字段。

请求:

`

1.  curl -X POST "http://localhost:8000/search" \
2.    -H "Content-Type: application/json" \
3.    -d '{"term": "dental cleaning"}'

`AI写代码

响应:

`{"hits":[{"owner_name":"Leo Martínez","visit_details":"Dental cleaning. Minor tartar buildup removed."}],"total":1}`AI写代码

后台任务

FastAPI 的另一个功能是支持后台任务。利用后台任务,你可以立即返回结果给用户,同时在后台继续执行任务。这个功能对长时间运行的任务特别有用。

在 Elasticsearch 中,我们使用 wait_for_completion=false 参数获取任务 ID 并关闭连接,而不是等待任务完成。然后可以用 tasks API 查询任务状态。一些示例有 _reindex_update_by_query_delete_by_query

假设你想让用户触发基于字段值删除几百万条文档的操作,并在完成后通知他们。你可以结合 FastAPI 的后台任务和 Elasticsearch 的 wait_for_completion 实现。

我们先创建一个函数,每隔 2 秒查询 tasks API 检查任务状态。

`

1.  async def check_task(es_client, task_id):
2.      try:
3.          while True:
4.              status = await es_client.tasks.get(task_id=task_id)
5.              if status.get("completed", False):
6.                  print(f"Task {task_id} completed.")
7.                  # Here you can add the logic to send the notification
8.                  break
9.              await asyncio.sleep(2)
10.      except Exception as e:
11.          print(f"Error checking task {task_id}: {e}")

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

现在,我们可以创建一个端点,接收用作过滤条件的值来删除文档。调用 _delete_by_query API,设置 wait_for_completion=false,并使用返回的任务 ID 创建一个后台任务,调用我们之前写的 check_task 方法。

background_tasks.add_task 的第一个参数是要执行的函数,后面的参数是该函数所需的参数。

`

1.  # Background task endpoint
2.  @app.post("/delete-by-query")
3.  async def delete_by_query(
4.      request: SearchRequest = Body(...), background_tasks: BackgroundTasks = None
5.  ):
6.      try:
7.          body = {"query": {"term": {"pet_name.keyword": request.term}}}

9.          response = await es_client.delete_by_query(
10.              index=ES_INDEX, body=body, wait_for_completion=False
11.          )

13.          task_id = response.get("task")
14.          if task_id:
15.              background_tasks.add_task(check_task, es_async_client, task_id)

17.          return Response(
18.              status_code=200,
19.              content=json.dumps(
20.                  {
21.                      "message": "Delete by query. The response will be send by email when the task is completed.",
22.                      "task_id": task_id,
23.                  }
24.              ),
25.              media_type="application/json",
26.          )
27.      except Exception as e:
28.          status_code = getattr(e, "status_code", 500)

30.          raise HTTPException(status_code=status_code, detail=str(e))

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

下面是删除所有 pet_name.keyword 字段中包含 “Buddy” 文档的示例:

`

1.  curl -X POST "http://localhost:8000/delete-by-query" \
2.    -H "Content-Type: application/json" \
3.    -d '{"term": "Buddy"}'

`AI写代码

响应:

`{"message": "Delete by query. The response will be send by email when the task is completed.", "task_id": "191ALShERbucSkcFTGpOCg:34822095"}`AI写代码

后台日志:

`

1.  INFO:     127.0.0.1:58804 - "POST /delete-by-query HTTP/1.1" 200 OK
2.  Task 191ALShERbucSkcFTGpOCg:34822095 completed.

`AI写代码

运行 API

添加以下代码块,将服务器暴露在 8000 端口:

`

1.  if __name__ == "__main__":
2.      uvicorn.run(app, host="0.0.0.0", port=8000)

`AI写代码

使用以下命令运行 FastAPI 应用:

`uvicorn app:app --reload`AI写代码

总结

FastAPI 让构建安全且简洁的 Elasticsearch API 层变得简单。它内置类型检查、异步支持和后台任务,能轻松处理常见用例,且开销不大。

这些示例只是起点——你可以根据需要添加认证、分页,甚至 websockets。关键是保持集群安全,API 清晰且易于维护。

原文:Building Elasticsearch APIs with FastAPI - Elasticsearch Labs