在 Ubuntu 24.04 上使用 FastAPI 部署和查询 Meilisearch

什么是 Meilisearch?

Meilisearch 是一个开源的、快速且相关的搜索引擎。它使用 Rust 编写,提供了以下特点:

  • 闪电般的速度: Meilisearch 可以在毫秒级内返回搜索结果,无论数据集大小如何。
  • 相关性优先: 通过智能排序算法,确保最相关的结果总是排在前面。
  • 易于使用: 提供直观的 API 和丰富的文档,降低了集成和使用的门槛。
  • 高度可定制: 允许根据具体需求调整搜索行为。

在 Ubuntu 24.04 上部署 Meilisearch

步骤 1:安装 Docker

确保您的系统上已经安装了 Docker。您可以通过以下命令安装 Docker:

sql 复制代码
bash
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

步骤 2:拉取并运行 Meilisearch Docker 镜像

拉取并运行 Meilisearch Docker 镜像:

bash 复制代码
bash
docker pull getmeili/meilisearch:latest
docker run -p 7700:7700 getmeili/meilisearch:latest
  • -p 7700:7700:将容器的 7700 端口映射到主机的 7700 端口。Meilisearch 默认使用 7700 端口。

步骤 3:访问 Meilisearch Web 界面

在浏览器中访问 http://localhost:7700,您将看到 Meilisearch 的 Web 界面。

步骤 4:启用 API 密钥(可选)

如果您需要启用 API 密钥,可以在运行 Docker 容器时添加环境变量 MEILI_MASTER_KEY

ini 复制代码
bash
docker run -p 7700:7700 -e MEILI_MASTER_KEY='your_master_key' getmeili/meilisearch:latest

your_master_key 替换为您自己的 API 密钥。

使用 FastAPI 与 Meilisearch 交互

安装 FastAPI 和 Meilisearch FastAPI

创建一个新的虚拟环境并激活它:

bash 复制代码
python3 -m venv myenv
source myenv/bin/activate

然后安装必要的包:

bash 复制代码
pip install fastapi uvicorn meilisearch-fastapi

创建 FastAPI 应用

创建一个名为 main.py 的文件,并添加以下代码:

python 复制代码
from fastapi import FastAPI
from meilisearch_fastapi.routes import (
    document_routes,
    index_routes,
    meilisearch_routes,
    search_routes,
    settings_routes,
)

app = FastAPI()
api_router = FastAPI()

api_router.include_router(
    document_routes.router,
    prefix="/documents",
)

api_router.include_router(
    index_routes.router,
    prefix="/indexes",
)

api_router.include_router(
    meilisearch_routes.router,
    prefix="/meilisearch",
)

api_router.include_router(
    search_routes.router,
    prefix="/search",
)

api_router.include_router(
    settings_routes.router,
    prefix="/settings",
)

app.include_router(api_router)

# 配置环境变量
import os
os.environ["MEILI_HTTP_ADDR"] = "http://localhost:7700"
os.environ["MEILI_MASTER_KEY"] = "your_master_key"  # 如果启用了 API 密钥

运行 FastAPI 应用

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

bash 复制代码
uvicorn main:app --host 0.0.0.0 --port 8000 --reload

测试 FastAPI 接口

现在您可以通过访问 http://localhost:8000/docs 来查看 FastAPI 的 API 文档,并使用这些接口与 Meilisearch 进行交互。

使用 FastAPI 进行搜索

以下是一个使用 FastAPI 进行搜索的例子:

python 复制代码
from fastapi import FastAPI
from pydantic import BaseModel
import requests

app = FastAPI()

class SearchQuery(BaseModel):
    q: str

@app.post("/search")
def search(query: SearchQuery):
    url = "http://localhost:7700/indexes/movies/search"
    params = {"q": query.q}
    response = requests.post(url, json=params)
    return response.json()

# 测试搜索接口
# curl -X POST -H "Content-Type: application/json" -d '{"q": "spiderman"}' http://localhost:8000/search

这个例子演示了如何使用 FastAPI 创建一个简单的搜索接口,并将搜索请求转发给 Meilisearch。

相关推荐
tod1131 天前
TCP全连接队列与tcpdump抓包
网络·网络协议·tcp/ip·github·tcpdump
Luck_ff08101 天前
百度指数数据采集与可视化平台 BaiduIndexHunter
github·开源软件
qq_297574671 天前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
Bella的成长园地1 天前
为什么c++中的条件变量的 wait() 函数需要配合while 循环或谓词?
c++·面试
郝学胜-神的一滴1 天前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
阿里嘎多学长1 天前
2026-02-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Tony Bai1 天前
“Go 2,请不要发生!”:如果 Go 变成了“缝合怪”,你还会爱它吗?
开发语言·后端·golang
Victor3561 天前
Hibernate(91)如何在数据库回归测试中使用Hibernate?
后端
Victor3561 天前
MongoDB(1)什么是MongoDB?
后端
子兮曰1 天前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github