开启 Algolia
参考 Docusaurus Search 和 Algolia Documentation,本站使用 Algolia 做全站搜索,若需要使用其它搜索方式,可参考官网资源。
Algolia 的配置过程如下:
- 点击 Algolia 官网 注册,点击 Dashboard 进入控制台首页。
- 点击左下角的 Settings/General/Applications:
- 点击 New application 创建新应用
- 根据需求自定义应用名,如笔者设置为网站名 qileq,然后选择付费方式,对于个人小站点可先选择 Free,点击下一步
- 选择数据保存地址,笔者数据保存在内地,故选择 Hong-Kong,点击下一步查看需求汇总
- 勾选协议后,点击创建应用按钮
- 点击 Settings/API Keys,选择应用 qileq,可以看到
Application ID
、Search-Only API Key
和Admin API Key
等值。 - 配置
docusaurus.config.js
,参考 Using Algolia DocSearch,添加如下配置:
docusaurus.config.js
module.exports = {
// ...
themeConfig: {
// ...
algolia: {
appId: 'YOUR_APP_ID', // 第 3 步中的 Application ID
apiKey: 'YOUR_SEARCH_API_KEY', // 第 3 步中的 Search-Only API Key
indexName: 'site', // 自定义
},
},
};
- 配置 Algolia DocSearch 。若项目满足同时满足如下 checklist 则可点击 apply to the DocSearch program 填写应用信息来使用官方的爬虫来爬取网站内容:
- 拥有网站更新权限
- 网站能公开访问
- 开源的技术网站或博客
- 网站已发布到生产环境 否则需要自己部署爬虫。
部署爬虫
笔者从灵活性角度还是选择自己部署 Docker 来运行爬虫,过程如下。
配置爬虫环境
登录腾讯云服务器,找个目录创建 crawler
目录,在该目录下再创建 .env
文件。内容如下:
.env
APPLICATION_ID=YOUR_APP_ID
API_KEY=YOUR_API_KEY
其中 API_KEY
为每三步中的 Admin API Key
。
安装 jq
过程如下:
$ sudo apt install 0install-core
$ 0install add jq https://apps.0install.net/utils/jq.xml
$ sudo mv ~/bin/jq /usr/local/bin/
$ jq --version
jq-1.6
安装 Docker 并启动
参考 Install Docker Engine on Ubuntu,安装过程如下:
- 安装 HTTPS 相关的软件包:
$ sudo apt update
$ sudo apt install \
ca-certificates \
curl \
gnupg \
lsb-release
- 添加软件源的
GPG
密钥,鉴于国内网络速度,笔者使用阿里云源来代替官方源:
# 阿里云源
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- 添加 Docker 源:
# 阿里云源
$ echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 官方源
# $ echo \
# "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
# $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- 更新 apt 软件包缓存并安装 Docker
$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io
- 修改 Docker 镜像源地址为腾讯云 Docker 镜像源。编辑
/etc/docker/daemon.json
,添加如下内容:
/etc/docker/daemon.json
{
"registry-mirrors": [
"https://mirror.ccs.tencentyun.com"
]
}
- 启动 Docker
$ sudo systemctl enable docker
$ sudo systemctl start docker
$ sudo systemctl status docker
配置 DocSearch
在 crawler
目录下创建 DocSearch 配置文件 docsearch.json
,内容参考 DocSearch Config Files,如下:
docsearch.json
{
"index_name": "site",
"start_urls": [
"https://qileq.com"
],
"sitemap_urls": [
"https://qileq.com/sitemap.xml"
],
"stop_urls": [
"/search",
"/playground"
],
"sitemap_alternate_links": true,
"selectors": {
"lvl0": {
"selector": "(//ul[contains(@class,'menu__list')]//a[contains(@class, 'menu__link menu__link--sublist menu__link--active')]/text() | //nav[contains(@class, 'navbar')]//a[contains(@class, 'navbar__link--active')]/text())[last()]",
"type": "xpath",
"global": true,
"default_value": "Documentation"
},
"lvl1": "header h1",
"lvl2": "article h2",
"lvl3": "article h3",
"lvl4": "article h4",
"lvl5": "article h5, article td:first-child",
"lvl6": "article h6",
"text": "article p, article li, article td:last-child"
},
"strip_chars": " .,;:#",
"custom_settings": {
"separatorsToIndex": "_",
"attributesForFaceting": [
"language",
"version",
"type",
"docusaurus_tag"
],
"attributesToRetrieve": [
"hierarchy",
"content",
"anchor",
"url",
"url_without_anchor",
"type"
]
},
"js_render": true,
"use_anchors": true
}
在爬虫运行过程中,会生成 index_name
+ _tmp 的临时索引,当爬虫运行结束后,该临时索引会重命名为 index_name
以代替之前的索引。
启动爬虫服务
执行如下命令启动爬虫:
$ sudo docker run -it --env-file=.env -e "CONFIG=$(cat docsearch.json | jq -r tostring)" algolia/docsearch-scraper
查看索引状态
打开 algolia 控制台可查看索引是否创建成功,如下图则表示创建成功:
每次重新部署应用后,爬虫服务都需要运行一次来保证搜索的准确性。每次手动操作太麻烦了,可以将运行爬虫的命令和 reload nginx 服务一起写个脚本,每次运行该脚本即可。但更好的方式是每次重新发布后自动运行爬虫来更新索引,见自动发布。