介绍
[Meilisearch] (https://www.meilisearch.com/)是一款以Rust编程语言书写的开源独立搜索引擎. 相较于其他流行的搜索引擎,Meilisearch部署需要极少数步骤,您可以运行一个Meilisearch服务器并使用单一的命令行二进制来查询. Meilisearch具有模糊匹配和无计划索引等特征,并包括用于演示目的的网络前端. 更复杂的部署支持与InstantSearch javascript库的整合.
在本教程中,您将首先在 Ubuntu 22.04 服务器上运行 Meilisearch 使用 Docker 来实验它. 您将用样本数据填充它,并从命令行和内置的 web frontend 进行查询。
前提条件
要遵循本教程,您将需要:
- 通过跟随Ubuntu 22.04 初始服务器设置指南建立的Ubuntu 22.04服务器,包括一个sudo非root用户和一个防火墙.
- Docker和Docker编组是继[如何在Ubuntu22.04上安装和使用Docker(https://andsky.com/tech/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04)后安装的,也是[在Ubuntu22.04上安装和使用Docker-Sockose(https://andsky.com/tech/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04)的第一步.
步骤 1 — 安装 Meilisearch 和加载样本数据
Meilisearch 為許多環境提供安裝套件,因為它正在進行積極的開發,而且其內部數據結構仍在更新,所以透過 Docker 安裝 Meilisearch 是個好主意。
首先,从 Docker Hub 拉下最新的 Meilisearch 图像 (0.26.1 ):
1docker pull getmeili/meilisearch:v0.26.1
您现在可以通过提供一些参数来启动docker run
的 Meilisearch docker 图像:
1docker run --rm -p 127.0.0.1:7700:7700 getmeili/meilisearch:v0.26.1
这个命令可以分解如下:
- 「docker run -rm」使用「--rm」旗帜来确保容器在退出时会自行清理。 * 「-p 127.0.0.1:7700:7700」在您的服务器的 localhost 接口上将流量转移到 7700 端口上的 Meilisearch 默认端口「7700」在 Docker 容器内,因此您可以正常访问它。
当您运行 Meilisearch 时,它会自动生成一些配置细节,并将其包括在输出中。 请注意,Meilisearch 索引目录的 ./data.ms
已自动生成在容器中。
1…
2[secondary_label Output]
3Database path: "./data.ms"
4Server listening on: "http://0.0.0.0:7700"
5Environment: "development"
6Commit SHA: "unknown"
7Commit date: "unknown"
8Package version: "0.26.1"
9…
您现在有一个运行的 Meilisearch 实例,里面没有数据. 您需要加载一些样本数据才能开始使用 Meilisearch. meilisearch
过程将阻止它所启动的壳,只要它在运行,所以您还需要打开与您的服务器的单独连接以继续运行其他命令。
Meilisearch 项目提供了从 TMDB,电影数据库中摘取的样本 JSON 格式化数据集. 使用wget
命令从docs.meilisearch.com
下载数据:
1wget https://docs.meilisearch.com/movies.json
您可以运行尾巴
,查看该文件的内容的片段:
1[secondary_label Output]
2…
3{"id":"289239","title":"Mostly Ghostly: Have You Met My Ghoulfriend?","poster":"https://image.tmdb.org/t/p/w500/eiVY4kKpbo1f7wyNubgJX5ILpxg.jpg","overview":"Bella Thorne, Madison Pettis and Ryan Ochoa lead an ensemble cast in this spook-tacular adventure with new ghosts, new thrills, and the return of some old friends. Max (Ochoa) only has eyes for Cammy (Thorne), the smart, popular redhead at school. When Max finally scores a date with Cammy on Halloween, Phears, an evil ghost with plans on taking over the world, unleashes his ghouls and things go haywire. With the help of his ghostly pals, Tara and Nicky, can Max thwart Phears' evil plot, help reunite his ghost friends with their long-lost parents and still make his date with Cammy on Halloween? R.L. Stine's Mostly Ghostly: Have You Met My Ghoulfriend? is a frightful family delight!","release_date":1409619600,"genres":["Family","Fantasy","Horror"]},
4{"id":"423189","title":"Right Here Right Now","poster":"https://image.tmdb.org/t/p/w500/sCo1excKlzhKas681CTSe1ujcOa.jpg","overview":"Hamburg, St. Pauli, New Year's Eve. Oskar Wrobel runs a music club in an old hospital at the edge of the Reeperbahn. While fireworks go off in the streets of St. Pauli, he prepares the big final party - the club has to close. Thankfully there is no time to think about it because the chaos is breaking into his living room, all while hell break loose at the club. The film, based on the novel by Tino Hanekamp, was filmed with hundreds of extras attending a real-life three-night-long party.","release_date":1530579600,"genres":["Documentary","Music"]},
5{"id":"550315","title":"Fireman Sam - Set for Action!","poster":"https://image.tmdb.org/t/p/w500/2atmRsuSA4tX6sbOEgFzquFWcCV.jpg","overview":"","release_date":1538010000,"genres":["Family","Animation"]}
6]
每个条目都包含一个 id ,一个** 标题** ,一个链接到一个** 海报** 图像,可选的是电影的** 概览** ,一个** 时间标签格式的 ** 发布日期** 和一个** 类型列表。
您可以通过使用‘curl’将数据加载到 Meilisearch 来构建 HTTP POST 请求。 curl是创建命令行 Web 请求的强大而普遍的工具,并且 HTTP POST 是少数常见的 HTTP 词汇之一(与 Web 浏览器使用的 PUT 和 GET 一起),用于将格式化数据发送到 API 端点。
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/documents' \
3 -H 'Content-Type: application/json' \
4 --data-binary @movies.json
curl
命令的各种论点是:
- **
-X POST http://url
表示您将执行 POST 请求并发送数据。 *-H 'Content-Type: application/json' 提供一个 ** h** 标题,指定文件类型。 *
--data-binary @movies.json' 包括文件本身。
默认情况下,Meilisearch在端口7700
上运行,而127.0.0.1
反映了 localhost IP. 在这种情况下,您正在创建一个新的Meilisearch索引在/indexes/movies/documents
,并在请求中提供必要的格式化以上传JSON文件。
该命令应该返回输出,表示您的请求已成功查询
。Meilisearch 以无同步的方式处理所有请求,而不是等待它们完成。
1[secondary_label Output]
2{"uid":0,"indexUid":"movies","status":"enqueued","type":"documentAddition","enqueuedAt":"2022-03-09T17:23:18.233702815Z"}
您可以通过向同一个索引创建的新 /tasks/
终端点执行 curl -X GET
请求来检查此请求的状态。
1curl -X GET 'http://localhost:7700/indexes/movies/tasks/0'
1[secondary_label Output]
2{"uid":0,"indexUid":"movies","status":"succeeded","type":"documentAddition","details":{"receivedDocuments":19547,"indexedDocuments":19546},"duration":"PT29.866920116S","enqueuedAt":"2022-03-09T17:23:18.233702815Z","startedAt":"2022-03-09T17:23:18.241475424Z","finishedAt":"2022-03-09T17:23:48.108395540Z"}
您现在有一个 Meilisearch 索引,包含样本数据,在下一步,您将尝试一些示例查询来探索数据。
步骤 2 – 使用 Meilisearch 搜索
要搜索 Meilisearch 索引,您可以通过 API 发送单个查询,或使用 Web 界面进行搜索。
通过 API 搜索与通过 HTTP POST 上传数据相似。 要进行搜索,您可以向 `/search’ 终端提出请求,并在命令行中包含整个请求 JSON。
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/search' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{ "q": "saint" }'
它会返回一个 JSON 对象,其中包含一个 hits 列表:
1[secondary_label Output]
2{
3 "hits": [
4 {
5 "id": "45756",
6 "title": "Saint",
7 "poster": "https://image.tmdb.org/t/p/w500/pEPd4mgMwvz6aRhuWkmPUv98P1O.jpg",
8 "overview": "A horror film that depicts St. Nicholas as a murderous bishop who kidnaps and murders children when there is a full moon on December 5.",
9 "release_date": 1288486800,
10 "genres": []
11 },
12 {
13 "id": "121576",
14 "title": "Saint Philip Neri I Prefer Heaven",
15 "poster": "https://image.tmdb.org/t/p/w500/z9OsQoM343WsIrP0zMEE06pO1vH.jpg",
16 "overview": "An epic feature film on the famous 'Apostle of Rome' and great friend of youth in the 16th century. One of the most popular saints of all time, St. Philip Neri was widely known for his great charity, deep prayer life, and tremendous humor. Hoping to join St. Ignatius of Loyola's new order of Jesuits and be a missionary to India, Philip was instead guided by Providence to seek out the poor and abandoned youth of Rome to catechize them in the faith and help them find a better life. He became the founder of the religious congregation, the Oratory, that worked with the youth and also labored to re-evangelize a decadent Rome.",
17 "release_date": 1284944400,
18 "genres": [
19 "Drama"
20 ]
21 },
22 {
23 "id": "221667",
24 "title": "Saint Laurent",
25 "poster": "https://image.tmdb.org/t/p/w500/ekpT7mTk4t5PjRYZfiyh0sTKpY5.jpg",
26 "overview": "1967-1976. As one of history's greatest fashion designers entered a decade of freedom, neither came out of it in one piece.",
27 "release_date": 1411434000,
28 "genres": [
29 "Drama"
30 ]
31 },
32...
注意:为了实现更可读的命令行 JSON 格式化,您可以使用sudo apt install jq
安装另一个名为jq
的工具,然后使用壳管通过jq
传输 Meilisearch JSON 输出,并附加到命令的 jq
。
例如,若要列出示例圣
查询的标题,请安装jq
,然后运行以下命令:
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/search' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{ "q": "saint" }' | jq -r '.hits [] .title'
您将收到这样的标题列表:
1[secondary_label Output
2Saint
3Saint Philip Neri I Prefer Heaven
4Saint Laurent
5. . .
美元
为了展示 Meilisearch 的模糊的匹配功能,您还可以尝试搜索seint
,假设用户可能已经打字:
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/search' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{ "q": "seint" }'
由于查询排名的细节,结果会略有不同,但您仍然会看到包含圣
的许多条目。
1[secondary_label Output]
2{
3 "hits": [
4 {
5 "id": "10105",
6 "title": "Saints and Soldiers",
7 "poster": "https://image.tmdb.org/t/p/w500/efhqxap8fLi4v1GEXVvakey0z3S.jpg",
8 "overview": "Five American soldiers fighting in Europe during World War II struggle to return to Allied territory after being separated from U.S. forces during the historic Malmedy Massacre.",
9 "release_date": 1063242000,
10 "genres": [
11 "War",
12 "Action",
13 "Drama"
14 ]
15 },
16 {
17 "id": "121576",
18 "title": "Saint Philip Neri I Prefer Heaven",
19 "poster": "https://image.tmdb.org/t/p/w500/z9OsQoM343WsIrP0zMEE06pO1vH.jpg",
20 "overview": "An epic feature film on the famous 'Apostle of Rome' and great friend of youth in the 16th century. One of the most popular saints of all time, St. Philip Neri was widely known for his great charity, deep prayer life, and tremendous humor. Hoping to join St. Ignatius of Loyola's new order of Jesuits and be a missionary to India, Philip was instead guided by Providence to seek out the poor and abandoned youth of Rome to catechize them in the faith and help them find a better life. He became the founder of the religious congregation, the Oratory, that worked with the youth and also labored to re-evangelize a decadent Rome.",
21 "release_date": 1284944400,
22 "genres": [
23 "Drama"
24 ]
25 },
26 {
27 "id": "133558",
28 "title": "Saints and Soldiers: Airborne Creed",
29 "poster": "https://image.tmdb.org/t/p/w500/gwqR9UY0xqBZwP2qb8ZPmf9b2lq.jpg",
30 "overview": "A group of American GIs work their way through war-torn France during the final days of the Second World War.",
31 "release_date": 1345165200,
32 "genres": [
33 "Drama"
34 ]
35 },
36…
现在您已经尝试了命令行搜索, 如果您正在本地机器上运行Meilisearch, 您可以在浏览器中浏览http://localhost: 770
, 以便查看网络用户界面 。 如果您运行在远程服务器上并遵循此教程的前提条件, 您的防火墙配置将会阻止此 URL 访问 。 您需要创建 SSH 隧道才能访问搜索界面 。 为了从本地机器到服务器建立一个隧道,用 " L " 旗运行 " ssh " 。 提供端口号`7700'以及您的远程服务器的IP地址:
1[environment local]
2ssh -L 7700:127.0.0.1:7700 sammy@your_server_ip
然后,您应该能够访问浏览器中的仪表板在http://localhost:7700
。
在 Meilisearch 演示界面中搜索非常快,应该提供令人兴奋的生产使用预览,在下一步中,您将找到调节搜索排名和过滤对某些参数偏见的示例。
步骤3 – 调节搜索排名和过滤
搜索引擎的一个重要特征是,它们都执行了一些方法来衡量不同字段的重要性. 另一个术语是 bias. 例如,假设您的搜索索引包含多个字段,并且您使用单个单词查询进行搜索。
一些开源搜索引擎不允许您配置偏见,查询结果可能会无助的广泛或被数据的趋势扭曲,而不是搜索术语的相关性。Meilisearch有一个默认的偏见规则,您可以进一步配置。
要检查您的排名规则,您可以使用弯曲
到/settings/ranking-rules
终端点进行 HTTP GET 请求:
1curl -X GET 'http://localhost:7700/indexes/movies/settings/ranking-rules'
1[secondary_label Output]
2["words","typo","proximity","attribute","sort","exactness"]
这些规则在 [Meilisearch 文档] 中更详细地解释(https://docs.meilisearch.com/learn/core_concepts/relevancy.html# ranking-rules)。 基本上,它们允许您优化 Meilisearch 优先考虑解决常见字体的问题的方式,而不是优先考虑短语中的单词的接近性。
您可以将一个 HTTP POST 请求带有弯曲
返回到该端点,并将相同的规则重新安排以更改其优先顺序. 如果您的数据集包含定量字段,您还可以添加排序规则,这些规则将偏向这些字段以上升或下降顺序。
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/settings/ranking-rules' \
3 -H 'Content-Type: application/json' \
4 --data-binary '[
5 "words",
6 "typo",
7 "proximity",
8 "release_date:asc",
9 "attribute",
10 "sort",
11 "exactness",
12 "rank:desc"
13 ]'
此请求将返回一个查询
的响应,类似于您上传movies.json
文件的第一个 HTTP POST:
1[secondary_label Output]
2{"uid":1,"indexUid":"meteorites","status":"enqueued","type":"settingsUpdate","enqueuedAt":"2022-03-10T21:36:47.592902987Z"}
Meilisearch 允许您查询和更新规则,描述哪些属性可搜索,哪些属性在结果中显示,以及哪些属性可以过滤或排序,使用相同的 HTTP GET 和 HTTP POST 方法。
例如,如果您希望只有某些属性可搜索,而其他属性(如)只能从结果中排除,则可以将searchableAttributes
的JSON列表发送到Settings
终端:
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/settings' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{
5 "searchableAttributes": [
6 "title",
7 "overview",
8 "genres"
9 ]
10 }'
现在只能搜索标题
、概览
和类别
字段,其余部分则被排除在索引过程中。
您也可以通过发布显示属性
列表来更改哪些属性显示或隐藏在搜索结果中:
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/settings' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{
5 "displayedAttributes": [
6 "title",
7 "overview",
8 "genres",
9 "release_date"
10 ]
11 }'
现在,电影的所有字段都隐藏了,除了您在显示属性
列表中包含的字段之外。
此列表包括通过使用比较运算符的量化过滤(如)和通过包含在指定集中的过滤,也称为面部搜索。
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/settings' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{
5 "filterableAttributes": [
6 "genres",
7 "release_date"
8 ],
9 "sortableAttributes": [
10 "release_date"
11 ]
12 }'
这些规则可以让你创建如下类型的查询:
1curl \
2 -X POST 'http://localhost:7700/indexes/movies/search' \
3 -H 'Content-Type: application/json' \
4 --data-binary '{ "q": "house", "sort": ["release_date:desc"], "filter": "genres = Horror" }'
此查询相当于书面句子:从最新的到最古老的恐怖电影,其中也包含标题中的
房子一词的所有电影找到
。
1[secondary_label Output]
2{
3 "hits": [
4 {
5 "id": "82505",
6 "title": "House at the End of the Street",
7 "poster": "https://image.tmdb.org/t/p/w500/t9E3Inaar1CAn5Cwj0M8dTwtD8H.jpg",
8 "overview": "A mother and daughter move to a new town and find themselves living next door to a house where a young girl murdered her parents. When the daughter befriends the surviving son, she learns the story is far from over.",
9 "release_date": 1348189200,
10 "genres": [
11 "Horror",
12 "Thriller"
13 ]
14 },
15 {
16 "id": "29293",
17 "title": "House of the Dead 2",
18 "poster": "https://image.tmdb.org/t/p/w500/r95UYIFeCjIVKZ1MPxZwEHITAhg.jpg",
19 "overview": "In Guesta Verde University, the deranged Professor Curien is trying to bring back the dead, killing students for the experiment. There is an outbreak of zombies in the campus, and the government sends a NSA medical research team, formed by Dr. Alexandra Morgan a.k.a. Nightingale and lieutenant Ellis, with a special force leaded by lieutenant Dalton, trying to get the zero sample from the first generation zombie. The team has a very short time to accomplish their mission and leave the place before missiles are sent to destroy the area. However, the place is crowded of hyper sapiens and the group has to fight to survive.",
20 "release_date": 1139616000,
21 "genres": [
22 "TV Movie",
23 "Horror"
24 ]
25 },
26 {
27 "id": "10066",
28 "title": "House of Wax",
29 "poster": "https://image.tmdb.org/t/p/w500/r0v8qg78Ol9NIsRGe3DME27ORpd.jpg",
30 "overview": "A group of unwitting teens are stranded near a strange wax museum and soon must fight to survive and keep from becoming the next exhibit.",
31 "release_date": 1114822800,
32 "genres": [
33 "Horror",
34 "Drama"
35 ]
36 },
37…
包括和排除文档索引、搜索和显示中的字段是基于文档的搜索引擎的核心功能,允许它们条件允许您保持搜索性能,并快速和全面地定制您的搜索引擎以适应最终用户。
现在你已经探索了如何运行、查询和配置 Meilisearch,在本教程的下一步中,你将创建一个docker-compose
配置 Meilisearch,这将允许它在背景中运行,使用安全身份验证密钥。
步骤 4 – 创建 Docker-Compose 配置并使用基于密钥的身份验证
与任何其他服务器侧应用程序一样,Meilisearch索引的最终用户应该假定它将随时运行并可用。为了确保Meilisearch可以定期与您的服务器重新启动,并跟踪其日志,您应该创建一个docker-compose
配置,允许它自动管理。
首先,在「/var/local」中创建一个目录,可用于 Meilisearch 永久存储其搜索索引和其他配置细节:
1sudo mkdir /var/local/meilisearch
之后,在现有工作环境中创建一个名为meilisearch-docker
的目录,用于存储 Meilisearch 的 Docker 配置,然后将其cd
存入该目录:
1mkdir ~/meilisearch-docker
2cd ~/meilisearch-docker
接下来,使用nano
或您最喜欢的文本编辑器,打开名为docker-compose.yml
的文件:
1nano docker-compose.yml
将下列内容复制到文件中,这将成为您的 Meilisearch Docker 配置。
1[label docker-compose.yml]
2version: "3.9"
3services:
4 meilisearch:
5 image: "getmeili/meilisearch:v0.26.1"
6 restart: unless-stopped
7 ports:
8 - "127.0.0.1:7700:7700"
9 volumes:
10 - /var/local/meilisearch:/data.ms
11 env_file:
12 - ./meilisearch.env
保存并关闭文件. 如果您正在使用nano
,请按Ctrl+X
,然后在提示时按Y
,然后按ENTER
。
此文件中使用的设置类似于本教程中早些时候运行的原始docker
语法,加上一些补充:
- 「重新啟動:除非停止」意味著此服務將繼續在背景中運行,並持續進行重新啟動,除非您手動停止。 * 您的 Meilisearch 索引現在持久,並存儲在您本地機器上的
/var/local/meilisearch. *
env_file聲明環境檔案,
./meilisearch.env,以設定 Meilisearch 選項。
使用环境变量是配置应用程序参数的一致、全系统的方式,而无需编辑特定应用程序的配置文件。‘docker-compose’允许您在单独的文件中宣布环境变量,以避免在主要的‘docker-compse.yml’配置中包含任何秘密信息。
要在生产模式下运行 Meilisearch,您需要为安全使用配置一个API密钥,您可以创建自己的密钥,或使用以下openssl
命令生成一个密钥:
1openssl rand -hex 30
1[secondary_label Output]
2173e95f077590ed33dad89247247be8d8ce8b6722ccc87829aaefe3207be
最后,使用nano
或您最喜欢的文本编辑器再次打开名为meilisearch.env
的新文件:
1nano meilisearch.env
将您的密钥粘贴到文件中作为MEILI_MASTER_KEY
环境变量:
1[label meilisearch.env]
2MEILI_MASTER_KEY="173e95f077590ed33dad89247247be8d8ce8b6722ccc87829aaefe3207be"
如果您仍然在另一个壳中运行 Meilisearch 实例,您现在应该通过返回该终端窗口并按Ctrl+C
来关闭它,现在您可以使用docker-compose up
和--detach
来在背景中运行新的 Meilisearch Docker 实例:
1docker compose up --detach
您可以通过使用docker ps
来验证它是否成功启动:
1docker compose ps
1[secondary_label Output]
2 Name Command State Ports
3------------------------------------------------------------------------------------------------------------
4meilisearch-docker_meilisearch_1 tini -- /bin/sh -c ./meili ... Up 127.0.0.1:7700->7700/tcp
从现在开始,Meilisearch 将自动重新启动服务器重新启动,如果需要,您可以通过返回~/docker-meilisearch
目录并运行docker compose stop
来阻止 Docker 容器。
此「docker-compose」配置还会创建一个新的、空的搜索索引,您将需要重新填充以继续使用样本数据。如果您需要迁移使用现有索引创建的任何规则,您可以遵循 Meilisearch 升级文档.
您的 Meilisearch 实例现在也使用基于密钥的身份验证,这需要额外的 `-H '授权: Bearer 173e95f077590ed33dad89247247be8d8ce8b6722ccc87829aaefe3207be''的标题,以包含每个 API 请求。您可以查看 Meilisearch auth 文档以创建多个身份验证密钥,如果需要的话,您可以查看更多细微级的权限。
结论
在本教程中,您使用 Docker 部署并配置了 Meilisearch 索引. 您看到其查询解析和 API 响应结构的示例,以及其网页前端搜索性能。 您可以使用从任何应用程序发送的 JSON 请求来查询 Meilisearch,就像您使用弯曲
一样。
您还创建了一个 API 密钥,并将其配置为环境变量 Meilisearch 准备安全生产部署 。 然而,您在此教程中使用的基于网络的搜索界面并没有被设计用于制作,因为它对面部和其他高级搜索功能的支持有限. 在此系列的下个教程中, 您将 [在 Node. js 应用程序中使用 InstantSearch 库配置一个 Meilisearch 前端 (https://andsky.com/tech/tutorials/how-to-run-a-meilisearch-frontend-using-instantsearch-on-ubuntu-22-04) .