Meilisearch 是由 Meili (一家总部位于法国的软件开发公司)创建的搜索引擎,目前在 Github 上有 47.9k stars。
Meillisearch 具备以下特色功能(ChatGPT-4o 翻译):
- 混合搜索:结合语义搜索和全文搜索的优点,获取最相关的结果
- 即时搜索:在不到50毫秒的时间内找到并显示结果,以提供直观的体验
- 容错:即使查询中包含错别字和拼写错误,也能获得相关匹配结果
- 过滤和分面搜索:通过自定义过滤器增强用户的搜索体验,并用几行代码构建分面搜索界面
- 排序:根据价格、日期或其他用户需求排序结果
- 同义词支持:配置同义词以在搜索结果中包含更多相关内容
- 地理搜索:基于地理数据过滤和排序文档
- 广泛的语言支持:支持任何语言的数据集搜索,特别优化了中文、日文、希伯来文和使用拉丁字母的语
- 安全管理:通过允许细粒度权限处理的 API 密钥控制用户的数据访问
- 多租户:为任意数量的应用程序租户个性化搜索结果
- 高度可定制:可以根据特定需求定制 Meilisearch,或使用Meillisearch开箱即用的无忧预设
- RESTful API:可以基于提供的插件和 SDK 将 Meilisearch 集成到技术栈中
- 易于安装、部署和维护
如何部署?
两种方式,本地部署或者使用 Meilisearch Cloud,本篇博客的后续内容将简单介绍一下如何本地部署Meillisearch。
操作系统:Ubuntu 22.04.4 LTS
安装命令:
bash
# Install Meilisearch
curl -L https://install.meilisearch.com | sh
执行完命令后的输出如下
bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 167 100 167 0 0 227 0 --:--:-- --:--:-- --:--:-- 226
100 4352 100 4352 0 0 2745 0 0:00:01 0:00:01 --:--:-- 4250k
Downloading Meilisearch binary v1.11.3 for linux, architecture amd64...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
100 115M 100 115M 0 0 1941k 0 0:01:01 0:01:01 --:--:-- 2174k
Meilisearch v1.11.3 binary successfully downloaded as 'meilisearch' file.
Run it:
$ ./meilisearch
Usage:
$ ./meilisearch --help
如何运行?
启动 Meilisearch应用,可以配置 master-key
让Meilisearch的运行更安全。
设置主密钥在开发环境中是可选的,但强烈建议进行配置。在生产环境中,主密钥是必需的。
主密钥可以改为任何长度大于等于 16 的字母数字组合形成的字符串。
bash
# Launch Meilisearch
./meilisearch --master-key="aSampleMasterKey"
正常启动后终端输出(默认运行在7700端口):
bash
888b d888 d8b 888 d8b 888
8888b d8888 Y8P 888 Y8P 888
88888b.d88888 888 888
888Y88888P888 .d88b. 888 888 888 .d8888b .d88b. 8888b. 888d888 .d8888b 88888b.
888 Y888P 888 d8P Y8b 888 888 888 88K d8P Y8b "88b 888P" d88P" 888 "88b
888 Y8P 888 88888888 888 888 888 "Y8888b. 88888888 .d888888 888 888 888 888
888 " 888 Y8b. 888 888 888 X88 Y8b. 888 888 888 Y88b. 888 888
888 888 "Y8888 888 888 888 88888P' "Y8888 "Y888888 888 "Y8888P 888 888
Config file path: "none"
Database path: "./data.ms"
Server listening on: "http://localhost:7700"
Environment: "development"
Commit SHA: "unknown"
Commit date: "unknown"
Package version: "1.11.3"
Thank you for using Meilisearch!
We collect anonymized analytics to improve our product and your experience. To learn more, including how to turn off analytics, visit our dedicated documentation page: https://www.meilisearch.com/docs/learn/what_is_meilisearch/telemetry
Anonymous telemetry: "Enabled"
Instance UID: "2e3c3aae-3fc3-458c-a610-3aaf910f86a4"
A master key has been set. Requests to Meilisearch won't be authorized unless you provide an authentication key.
Check out Meilisearch Cloud! https://www.meilisearch.com/cloud?utm_campaign=oss&utm_source=engine&utm_medium=cli
Documentation: https://www.meilisearch.com/docs
Source code: https://github.com/meilisearch/meilisearch
Discord: https://discord.meilisearch.com
2024-12-13T09:18:37.941118Z INFO actix_server::builder: starting 32 workers
2024-12-13T09:18:37.941200Z INFO actix_server::server: Actix runtime found; starting in Actix runtime
如何使用?
- 将官方提供的
movies.json
文件上传到安装部署 Meilisearch 的同一目录下,然后执行以下命令:
bash
curl \
-X POST 'http://localhost:7700/indexes/movies/documents?primaryKey=id' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer aSampleMasterKey' \
--data-binary @movies.json
movies.json 中某一条数据示例:
json
{
"id": 2,
"title": "Ariel",
"overview": "Taisto Kasurinen is a Finnish coal miner whose father has just committed suicide and who is framed for a crime he did not commit. In jail, he starts to dream about leaving the country and starting a new life. He escapes from prison but things don't go as planned...",
"genres": [
"Drama",
"Crime",
"Comedy"
],
"poster": "https://image.tmdb.org/t/p/w500/ojDg0PGvs6R9xYFodRct2kdI6wC.jpg",
"release_date": 593395200
}
Meilisearch 以离散记录的形式存储数据,这些记录称为文档。每个文档是由多个字段组成的对象,每个字段由一个属性和值对构构成。文档被分组到集合中,这些集合称为索引。先前执行的 curl
命令将 movies.json 中的文档添加到了一个名为 movies 的新索引中,并将 id 设置为主键。
终端输出结果:
bash
{
"taskUid": 1,
"indexUid": "movies",
"status": "enqueued",
"type": "documentAdditionOrUpdate",
"enqueuedAt": "2024-12-13T09:39:50.659976613Z"
}
- 对建立的名为 movies 的索引进行检索
执行如下命令:
bash
curl \
-X POST 'http://localhost:7700/indexes/movies/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer aSampleMasterKey' \
--data-binary '{ "q": "botman" }'
参数 q 代表query,默认情况下,Meilisearch 仅返回搜索查询的前 20 个结果(可以使用 limit 参数更改此设置)。
检索 botman 相关电影信息并返回一条检索结果:
bash
curl \
-X POST 'http://localhost:7700/indexes/movies/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer aSampleMasterKey' \
--data-binary '{ "q": "botman", "limit": 1}'
执行命令后终端输出的检索结果如下:
json
{
"hits": [
{
"id": 155,
"title": "The Dark Knight",
"overview": "Batman raises the stakes in his war on crime. With the help of Lt. Jim Gordon and District Attorney Harvey Dent, Batman sets out to dismantle the remaining criminal organizations that plague the streets. The partnership proves to be effective, but they soon find themselves prey to a reign of chaos unleashed by a rising criminal mastermind known to the terrified citizens of Gotham as the Joker.",
"genres": [
"Drama",
"Action",
"Crime",
"Thriller"
],
"poster": "https://image.tmdb.org/t/p/w500/qJ2tW6WMUDux911r6m7haRef0WH.jpg",
"release_date": 1216166400
}
],
"query": "botman",
"processingTimeMs": 0,
"limit": 1,
"offset": 0,
"estimatedTotalHits": 70
}
更多Meillisearch相关使用请前往 官方文档 或关注后续博客内容。