Vuepress 2从0-1保姆级进阶教程——全文搜索篇(含Algolia、meilisearch)

本文详细介绍Vuepress2中全文搜索的实现方法,包括search-pro本地搜索、Algolia远程搜索及MeiliSearch的接入步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

Vuepress 2 专栏目录【已完结】

1. 入门阶段

  1. Vuepress 2从0-1保姆级入门教程——环境配置篇
  2. Vuepress 2从0-1保姆级入门教程——安装流程篇
  3. Vuepress 2从0-1保姆级入门教程——文档配置篇
  4. Vuepress 2从0-1保姆级入门教程——主题与部署

2.进阶阶段

  1. Vuepress 2从0-1保姆级进阶教程——全文搜索篇
  2. Vuepress 2从0-1保姆级进阶教程——美化与模版
  3. Vuepress 2从0-1保姆级进阶教程——标准化流程

更新日志

  • 添加Algolia版块
  • 更新meilisearch2.x搜索(链接)版块
  • 所有提示块信息放大显示
  • 添加algolia新的使用方式
  • 替换旧版algolia教程网页
  • 添加reco主题搜索模版
  • 修复plume主题搜索模版错误
  • 添加仓库链接
  • 添加自动爬虫方法
  • 移除search-pro插件
  • 添加docker国内镜像加速方法
  • 更新hope主题自建爬虫模版
  • 添加自动爬虫模版
  • 添加meilisearch爬虫
  • 引用块样式矢量放大更清晰
  • 添加Meiliserch ssl访问方法

在这里插入图片描述

logo等静态文件:https://wwk.lanzouo.com/b04x8f3hg
密码:666
仓库:https://gitee.com/passwordgloo/vuepress2-tutorial

git clone git@gitee.com:passwordgloo/vuepress2-tutorial.git
cd vuepress2-tutorial
pnpm install

# 谨慎更新包
# pnpm docs:update-package

在这里插入图片描述

一、slimsearch

1️⃣ 安装

pnpm install -D @vuepress/plugin-slimsearch@next

2️⃣ 多个插件配置之间用,隔开

//写在config.ts开头引入插件
import { slimsearchPlugin } from '@vuepress/plugin-slimsearch'

export default {
  plugins: [
    slimsearchPlugin({
       // 已启用全文搜索
      indexContent: true,
    }),
  ],
}

在这里插入图片描述

3️⃣ 预览

pnpm docs:dev

在这里插入图片描述
在这里插入图片描述

二、Algolia

在这里插入图片描述

(一)爬虫

方式一:官方Algolia DocSearch申请

在这里插入图片描述

在这里插入图片描述

访问DocSearch 官方平台申请

在这里插入图片描述
如果符合要求,官方会通过邮件联系你,你需要用英语回复官方自己是网站的管理者,拥有修改文件的权限,不会英语可以用在线翻译

官方确认后会回复带有indexName、appId、apikey的邮件

使用申请时的邮箱注册algolia账号

方式二:自建爬虫

在这里插入图片描述

如果你嫌等待时间太久,或者不符合上述任意一条申请条件,Algolia提供自建爬虫方式手动爬取网站信息

1.注册Algolia账号

使用Github账号登录Algolia 官网,如果你会魔法,请大胆用google账号

在这里插入图片描述

在这里插入图片描述

2.创建应用

新版网页界面简化创建流程,点击+ Create Application 新建

在这里插入图片描述

在这里插入图片描述

稍等片刻,自动创建完毕

【可选】修改名称,不影响后续操作

在这里插入图片描述

☑️ 要修改的项目,点击重命名

3.查看必要信息
方法1 点击首页Overview

在这里插入图片描述

方法2 API Keys页面

♦️点击设置进入API Keys

在这里插入图片描述

♦️复制紫色框内信息

在这里插入图片描述

4.安装Docker

进入Docker官网,根据系统平台选择对应版本。

Windows 10及Windows 11可参照追逐时光者的【全面详细】Windows10 Docker安装详细教程

如果你使用以下平台,官方提供安装教程或一键安装

在这里插入图片描述

5.新建.env文件

✅ 项目文件夹内新建.env环境文件
⚠️ 替换成刚刚复制的信息,不需要Admin_API_Key
🔐 .env文件请添加到.gitignore文件中,已公开仓库请删除该文件,避免敏感信息泄露

APPLICATION_ID=Your_Application_ID
API_KEY=Your_Write_API_Key
6.安装jq

在这里插入图片描述

访问jq 官网下载软件,这里提供一下Linux安装命令

CentOS/ Fedora

sudo dnf install jq

Ubuntu/Debian

sudo apt-get install jq
7.新建config.json

⚠️ 注意替换内容

  • 第2行,第32行INDEX_NAME请替换成自己的index名字
  • 第4行、第7行、第10行Your Website Address替换成自己的域名

1️⃣ Vuepress 2默认主题

{
    "index_name": "INDEX_NAME",
    "start_urls": [
        "Your Website Address"
    ],
    "pathsToMatch": [
        "Your Website Address/**"
    ],
    "discoveryPatterns": [
        "Your Website Address/**"
    ],
    "ignoreCanonicalTo": false,
    "selectors": {
        "lvl0": {
            "selector": ".sidebar-heading.active",
            "defaultValue": "Documentation"
        },
        "lvl1": ".theme-default-content h1",
        "lvl2": ".theme-default-content h2",
        "lvl3": ".theme-default-content h3",
        "lvl4": ".theme-default-content h4",
        "lvl5": ".theme-default-content h5",
        "lvl6": ".theme-default-content h6",
        "content": ".theme-default-content p, .theme-default-content li",
        "lang": {
            "selector": "/html/@lang",
            "type": "xpath",
            "global": true
        }
    },
    "initialIndexSettings": {
        "INDEX_NAME": {
            "attributesForFaceting": [
                "type",
                "lang"
            ],
            "attributesToRetrieve": [
                "hierarchy",
                "content",
                "anchor",
                "url"
            ],
            "attributesToHighlight": [
                "hierarchy",
                "hierarchy_camel",
                "content"
            ],
            "attributesToSnippet": [
                "content:10"
            ],
            "camelCaseAttributes": [
                "hierarchy",
                "hierarchy_radio",
                "content"
            ],
            "searchableAttributes": [
                "unordered(hierarchy_radio_camel.lvl0)",
                "unordered(hierarchy_radio.lvl0)",
                "unordered(hierarchy_radio_camel.lvl1)",
                "unordered(hierarchy_radio.lvl1)",
                "unordered(hierarchy_radio_camel.lvl2)",
                "unordered(hierarchy_radio.lvl2)",
                "unordered(hierarchy_radio_camel.lvl3)",
                "unordered(hierarchy_radio.lvl3)",
                "unordered(hierarchy_radio_camel.lvl4)",
                "unordered(hierarchy_radio.lvl4)",
                "unordered(hierarchy_radio_camel.lvl5)",
                "unordered(hierarchy_radio.lvl5)",
                "unordered(hierarchy_radio_camel.lvl6)",
                "unordered(hierarchy_radio.lvl6)",
                "unordered(hierarchy_camel.lvl0)",
                "unordered(hierarchy.lvl0)",
                "unordered(hierarchy_camel.lvl1)",
                "unordered(hierarchy.lvl1)",
                "unordered(hierarchy_camel.lvl2)",
                "unordered(hierarchy.lvl2)",
                "unordered(hierarchy_camel.lvl3)",
                "unordered(hierarchy.lvl3)",
                "unordered(hierarchy_camel.lvl4)",
                "unordered(hierarchy.lvl4)",
                "unordered(hierarchy_camel.lvl5)",
                "unordered(hierarchy.lvl5)",
                "unordered(hierarchy_camel.lvl6)",
                "unordered(hierarchy.lvl6)",
                "content"
            ]
        }
    },
    "distinct": true,
    "attributeForDistinct": "url",
    "customRanking": [
        "desc(weight.pageRank)",
        "desc(weight.level)",
        "asc(weight.position)"
    ],
    "ranking": [
        "words",
        "filters",
        "typo",
        "attribute",
        "proximity",
        "exact",
        "custom"
    ],
    "custom_settings": {
        "attributesForFaceting": [
            "lang"
        ]
    },
    "highlightPreTag": "<span class='algolia-docsearch-suggestion--highlight'>",
    "highlightPostTag": "</span>",
    "minWordSizefor1Typo": 3,
    "minWordSizefor2Typos": 7,
    "allowTyposOnNumericTokens": false,
    "minProximity": 1,
    "ignorePlurals": true,
    "advancedSyntax": true,
    "attributeCriteriaComputedByMinProximity": true,
    "removeWordsIfNoResults": "allOptional"
}

2️⃣ Vuepress 2 Hope主题

{
    "index_name": "INDEX_NAME",
    "start_urls": [
        "Your Website Address"
    ],
    "pathsToMatch": [
        "Your Website Address/**"
    ],
    "discoveryPatterns": [
        "Your Website Address/**"
    ],
    "ignoreCanonicalTo": false,
    "selectors": {
          lvl0: {
              selectors: [".vp-sidebar-link.active", "[vp-content] h1"],
              defaultValue: "Documentation",
            },
            lvl1: "[vp-content] h1",
            lvl2: "[vp-content] h2",
            lvl3: "[vp-content] h3",
            lvl4: "[vp-content] h4",
            lvl5: "[vp-content] h5",
            lvl6: "[vp-content] h6",
            content: "[vp-content] p, [vp-content] li",
        "lang": {
            "selector": "/html/@lang",
            "type": "xpath",
            "global": true
        }
    },
    "initialIndexSettings": {
        "INDEX_NAME": {
            attributesForFaceting: ["type", "lang"],
            "attributesToRetrieve": [
                "hierarchy",
                "content",
                "anchor",
                "url"
            ],
            "attributesToHighlight": [
                "hierarchy",
                "hierarchy_camel",
                "content"
            ],
            "attributesToSnippet": [
                "content:10"
            ],
            "camelCaseAttributes": [
                "hierarchy",
                "hierarchy_radio",
                "content"
            ],
            "searchableAttributes": [
                "unordered(hierarchy_radio_camel.lvl0)",
                "unordered(hierarchy_radio.lvl0)",
                "unordered(hierarchy_radio_camel.lvl1)",
                "unordered(hierarchy_radio.lvl1)",
                "unordered(hierarchy_radio_camel.lvl2)",
                "unordered(hierarchy_radio.lvl2)",
                "unordered(hierarchy_radio_camel.lvl3)",
                "unordered(hierarchy_radio.lvl3)",
                "unordered(hierarchy_radio_camel.lvl4)",
                "unordered(hierarchy_radio.lvl4)",
                "unordered(hierarchy_radio_camel.lvl5)",
                "unordered(hierarchy_radio.lvl5)",
                "unordered(hierarchy_radio_camel.lvl6)",
                "unordered(hierarchy_radio.lvl6)",
                "unordered(hierarchy_camel.lvl0)",
                "unordered(hierarchy.lvl0)",
                "unordered(hierarchy_camel.lvl1)",
                "unordered(hierarchy.lvl1)",
                "unordered(hierarchy_camel.lvl2)",
                "unordered(hierarchy.lvl2)",
                "unordered(hierarchy_camel.lvl3)",
                "unordered(hierarchy.lvl3)",
                "unordered(hierarchy_camel.lvl4)",
                "unordered(hierarchy.lvl4)",
                "unordered(hierarchy_camel.lvl5)",
                "unordered(hierarchy.lvl5)",
                "unordered(hierarchy_camel.lvl6)",
                "unordered(hierarchy.lvl6)",
                "content"
            ]
        }
    },
    "distinct": true,
    "attributeForDistinct": "url",
    "customRanking": [
        "desc(weight.pageRank)",
        "desc(weight.level)",
        "asc(weight.position)"
    ],
    "ranking": [
        "words",
        "filters",
        "typo",
        "attribute",
        "proximity",
        "exact",
        "custom"
    ],
    "custom_settings": {
        "attributesForFaceting": [
            "lang"
        ]
    },
    "highlightPreTag": "<span class='algolia-docsearch-suggestion--highlight'>",
    "highlightPostTag": "</span>",
    "minWordSizefor1Typo": 3,
    "minWordSizefor2Typos": 7,
    "allowTyposOnNumericTokens": false,
    "minProximity": 1,
    "ignorePlurals": true,
    "advancedSyntax": true,
    "attributeCriteriaComputedByMinProximity": true,
    "removeWordsIfNoResults": "allOptional"
}

3️⃣ Vuepress 2 Reco 主题

{
    "index_name": "INDEX_NAME",
    "start_urls": [
        "Your Website Address"
    ],
    "pathsToMatch": [
        "Your Website Address/**"
    ],
    "discoveryPatterns": [
        "Your Website Address/**"
    ],
    "ignoreCanonicalTo": false,
    "selectors": {
        "lvl0": {
            "selector": ".sidebar-heading.active",
            "defaultValue": "Documentation"
        },
        "lvl1": "h1",
        "lvl2": "h2",
        "lvl3": "h3",
        "lvl4": "h4",
        "lvl5": "h5",
        "lvl6": "h6",
        "content": "p, li",
        "lang": {
            "selector": "/html/@lang",
            "type": "xpath",
            "global": true
        }
    },
    "initialIndexSettings": {
        "INDEX_NAME": {
            "attributesForFaceting": [
                "type",
                "lang"
            ],
            "attributesToRetrieve": [
                "hierarchy",
                "content",
                "anchor",
                "url"
            ],
            "attributesToHighlight": [
                "hierarchy",
                "hierarchy_camel",
                "content"
            ],
            "attributesToSnippet": [
                "content:10"
            ],
            "camelCaseAttributes": [
                "hierarchy",
                "hierarchy_radio",
                "content"
            ],
            "searchableAttributes": [
                "unordered(hierarchy_radio_camel.lvl0)",
                "unordered(hierarchy_radio.lvl0)",
                "unordered(hierarchy_radio_camel.lvl1)",
                "unordered(hierarchy_radio.lvl1)",
                "unordered(hierarchy_radio_camel.lvl2)",
                "unordered(hierarchy_radio.lvl2)",
                "unordered(hierarchy_radio_camel.lvl3)",
                "unordered(hierarchy_radio.lvl3)",
                "unordered(hierarchy_radio_camel.lvl4)",
                "unordered(hierarchy_radio.lvl4)",
                "unordered(hierarchy_radio_camel.lvl5)",
                "unordered(hierarchy_radio.lvl5)",
                "unordered(hierarchy_radio_camel.lvl6)",
                "unordered(hierarchy_radio.lvl6)",
                "unordered(hierarchy_camel.lvl0)",
                "unordered(hierarchy.lvl0)",
                "unordered(hierarchy_camel.lvl1)",
                "unordered(hierarchy.lvl1)",
                "unordered(hierarchy_camel.lvl2)",
                "unordered(hierarchy.lvl2)",
                "unordered(hierarchy_camel.lvl3)",
                "unordered(hierarchy.lvl3)",
                "unordered(hierarchy_camel.lvl4)",
                "unordered(hierarchy.lvl4)",
                "unordered(hierarchy_camel.lvl5)",
                "unordered(hierarchy.lvl5)",
                "unordered(hierarchy_camel.lvl6)",
                "unordered(hierarchy.lvl6)",
                "content"
            ]
        }
    },
    "distinct": true,
    "attributeForDistinct": "url",
    "customRanking": [
        "desc(weight.pageRank)",
        "desc(weight.level)",
        "asc(weight.position)"
    ],
    "ranking": [
        "words",
        "filters",
        "typo",
        "attribute",
        "proximity",
        "exact",
        "custom"
    ],
    "custom_settings": {
        "attributesForFaceting": [
            "lang"
        ]
    },
    "highlightPreTag": "<span class='algolia-docsearch-suggestion--highlight'>",
    "highlightPostTag": "</span>",
    "minWordSizefor1Typo": 3,
    "minWordSizefor2Typos": 7,
    "allowTyposOnNumericTokens": false,
    "minProximity": 1,
    "ignorePlurals": true,
    "advancedSyntax": true,
    "attributeCriteriaComputedByMinProximity": true,
    "removeWordsIfNoResults": "allOptional"
}

4️⃣ Vuepress 2 Plume(羽毛)主题

{
    "index_name": "INDEX_NAME",
    "start_urls": [
        "Your Website Address"
    ],
    "pathsToMatch": [
        "Your Website Address/**"
    ],
    "discoveryPatterns": [
        "Your Website Address/**"
    ],
    "rateLimit": 8,
    "ignoreCanonicalTo": false,
    "selectors": {
        "lvl0": {
            "selector": ".sidebar-item.is-active p,.content-container .page-title",
            "defaultValue": "Documentation"
        },
        "lvl1": ".plume-content h1",
        "lvl2": ".plume-content h2",
        "lvl3": ".plume-content h3",
        "lvl4": ".plume-content h4",
        "lvl5": ".plume-content h5",
        "lvl6": ".plume-content h6",
        "content": ".plume-content p, .plume-content li",
        "lang": {
            "selector": "/html/@lang",
            "type": "xpath",
            "global": true
        }
    },
    "initialIndexSettings": {
        "INDEX_NAME": {
            "attributesForFaceting": [
                "type",
                "lang"
            ],
            "attributesToRetrieve": [
                "hierarchy",
                "content",
                "anchor",
                "url"
            ],
            "attributesToHighlight": [
                "hierarchy",
                "hierarchy_camel",
                "content"
            ],
            "attributesToSnippet": [
                "content:10"
            ],
            "camelCaseAttributes": [
                "hierarchy",
                "hierarchy_radio",
                "content"
            ],
            "searchableAttributes": [
                "unordered(hierarchy_radio_camel.lvl0)",
                "unordered(hierarchy_radio.lvl0)",
                "unordered(hierarchy_radio_camel.lvl1)",
                "unordered(hierarchy_radio.lvl1)",
                "unordered(hierarchy_radio_camel.lvl2)",
                "unordered(hierarchy_radio.lvl2)",
                "unordered(hierarchy_radio_camel.lvl3)",
                "unordered(hierarchy_radio.lvl3)",
                "unordered(hierarchy_radio_camel.lvl4)",
                "unordered(hierarchy_radio.lvl4)",
                "unordered(hierarchy_radio_camel.lvl5)",
                "unordered(hierarchy_radio.lvl5)",
                "unordered(hierarchy_radio_camel.lvl6)",
                "unordered(hierarchy_radio.lvl6)",
                "unordered(hierarchy_camel.lvl0)",
                "unordered(hierarchy.lvl0)",
                "unordered(hierarchy_camel.lvl1)",
                "unordered(hierarchy.lvl1)",
                "unordered(hierarchy_camel.lvl2)",
                "unordered(hierarchy.lvl2)",
                "unordered(hierarchy_camel.lvl3)",
                "unordered(hierarchy.lvl3)",
                "unordered(hierarchy_camel.lvl4)",
                "unordered(hierarchy.lvl4)",
                "unordered(hierarchy_camel.lvl5)",
                "unordered(hierarchy.lvl5)",
                "unordered(hierarchy_camel.lvl6)",
                "unordered(hierarchy.lvl6)",
                "content"
            ]
        }
    },
    "distinct": true,
    "attributeForDistinct": "url",
    "customRanking": [
        "desc(weight.pageRank)",
        "desc(weight.level)",
        "asc(weight.position)"
    ],
    "ranking": [
        "words",
        "filters",
        "typo",
        "attribute",
        "proximity",
        "exact",
        "custom"
    ],
    "custom_settings": {
        "attributesForFaceting": [
            "lang"
        ]
    },
    "highlightPreTag": "<span class='algolia-docsearch-suggestion--highlight'>",
    "highlightPostTag": "</span>",
    "minWordSizefor1Typo": 3,
    "minWordSizefor2Typos": 7,
    "allowTyposOnNumericTokens": false,
    "minProximity": 1,
    "ignorePlurals": true,
    "advancedSyntax": true,
    "attributeCriteriaComputedByMinProximity": true,
    "removeWordsIfNoResults": "allOptional"
}
8.运行自建爬虫

把刚刚新建的config.json文件具体路径替换掉下面代码里的/path/to/your/config.json,然后运行

docker run -it --env-file=.env -e "CONFIG=$(cat /path/to/your/config.json | jq -r tostring)" algolia/docsearch-scraper

在这里插入图片描述

在这里插入图片描述
通常,执行命令后,docker会从官方站点拉取指定镜像,稍等片刻会出现下面结果

在这里插入图片描述
但是如果网络不好,你很可能卡在下载这一步
在这里插入图片描述

好消息是国内有镜像加速下载站,比如

在这里插入图片描述

docker pull hub.fast360.xyz/algolia/docsearch-scraper:latest
docker tag hub.fast360.xyz/algolia/docsearch-scraper:latest algolia/docsearch-scraper:latest
docker rmi hub.fast360.xyz/algolia/docsearch-scraper:latest
9.查看爬虫效果

在这里插入图片描述

不会设置文档语言可以回看文档配置篇多语言配置

在这里插入图片描述

10.在线测试效果

点击UI Demos,新建demo
在这里插入图片描述
在这里插入图片描述
测试结果成功,可以安装并配置插件了😁

方式三:Netlify

在这里插入图片描述
在这里插入图片描述

Netlify与Algolia有合作关系,所以如果你不想填申请、建爬虫,却还想使用Algolia服务可以尝一下鲜

0.文档推送到GitHub远程仓库

一个空仓库是搜不到任何信息的

1.部署文档到Netlify

使用GitHub账号登录Netlify网站
在这里插入图片描述
Add new site选择要部署的的GitHub仓库

在这里插入图片描述
在这里插入图片描述
没事别乱改自己的branch分支名,用默认的
在这里插入图片描述

2.管理你的algolia爬虫

使用你的algolia账号访问Algolia爬虫面板

在这里插入图片描述

点击右上角+ Link a new site

在这里插入图片描述

上面填刚刚部署的项目
下面选择部署节点
在这里插入图片描述

Netlify会自动安装algolia插件,点击左侧Crawlers查看爬虫运行情况,通常索引数量少于实际(仅做了Vuepress2适配,没有主题适配)
在这里插入图片描述

在这里插入图片描述

方式四:自动爬虫

  • 适用于方式三修改Netlify发布站点爬虫逻辑。
  • 适用于Vuepress 2自行部署到服务器,不想手动更新索引

方式2自建爬虫第2步创建完应用后,Algolia提供一定限度的免费自动爬虫服务,超量需付费,如果站点部署在Netlify,还有单独的Netlify 爬虫管理平台,为方便大家俩个都介绍一下,大家看个人情况任选

方式一:站点Crawler

  1. 在当前应用点击左侧图标,进入统计分析
  1. 先在第二栏Domins添加并验证自己的域名,平台提供DNS、robot、meta等验证方式

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
3. 在第一栏crawler 里新建
在这里插入图片描述
新建完成后,点击Configuration图形化界面配置爬虫

在这里插入图片描述

点击Editor可编辑爬虫代码
❌ 不要修改生成的appID和apikey
✅ 可参考下面主题给的模版

默认主题提供的爬虫模版
Hope主题提供的爬虫模版
Plume主题提供的爬虫模版

方式二:Netlify 爬虫面板

使用Algolia账号登录Netlify 爬虫管理

点击+ New Crawler

在这里插入图片描述
在这里插入图片描述

🔔 界面提示开始配置爬虫前需要验证域名

在这里插入图片描述

点击左侧侧边栏Domins

在这里插入图片描述

在这里插入图片描述

站点根目录robots.txt添加复制过来的验证代码👇

在这里插入图片描述
格式类似下面

 # Algolia-Crawler-Verif: XXXX

 User-Agent: *
 Allow: /

在这里插入图片描述

不要在Vuepress2 根目录放置自定义robots.txt,请放在.vuepress/public,官方说明如下
在这里插入图片描述

在这里插入图片描述

点击左侧侧边栏Crawler,进入自己的项目,点击Check Website通过第一步验证域名

在这里插入图片描述

点击Start the crawler自动生成索引

在这里插入图片描述

在这里插入图片描述

模版基于默认主题
在这里插入图片描述

但其他主题需要自己修改,请点击左侧侧边栏的Editor,在原有格式上修改内容,修改完成后点击右上角保存

❌ 不要修改生成的appID和apikey
✅ 可参考下面主题给的模版

默认主题提供的爬虫模版
Hope主题提供的爬虫模版
Plume主题提供的爬虫模版

在这里插入图片描述

介绍几个重要配置

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

方式五:升级Algolia账号

想要享受VIP待遇可在Algolia首页点一下Upgrade,填写信息绑定一张卡,选择相应的计划就行。

只有超过免费额度的部分需要付费
在这里插入图片描述

在这里插入图片描述

(二)安装插件

在这里插入图片描述

pnpm add -D @vuepress/plugin-docsearch@next

(三)配置

1. Vuepress 2 默认主题

import { docsearchPlugin } from "@vuepress/plugin-docsearch";

export default defineUserConfig({
  plugins: [
    docsearchPlugin({
      appId: '<APP_ID>',
      apiKey: '<Search_API_Key>',
      indexName: '<INDEX_NAME>',
      locales: {
        '/en/': {
          placeholder: 'Search Documentation',
          translations: {
            button: {
              buttonText: 'Search Documentation',
            },
          },
        },
        '/': {
          placeholder: '搜索文档',
          translations: {
            button: {
              buttonText: '搜索文档',
            },
          },
        },
      },
    }),
  ],
})

2. Vuepress 2 Hope 主题

最新版Hope主题自带algolia插件,无需安装,请在theme.ts里定位到plugin设置

plugin: {
    docsearch:({
      appId: "<APP_ID>",
      apiKey: "<API_KEY>",
      indexName: "<INDEX_NAME>",

      locales: {
        "/": {
          placeholder: "搜索文档",
          translations: {
            button: {
              buttonText: "搜索文档",
              buttonAriaLabel: "搜索文档",
            },
            modal: {
              searchBox: {
                resetButtonTitle: "清除查询条件",
                resetButtonAriaLabel: "清除查询条件",
                cancelButtonText: "取消",
                cancelButtonAriaLabel: "取消",
              },
              startScreen: {
                recentSearchesTitle: "搜索历史",
                noRecentSearchesText: "没有搜索历史",
                saveRecentSearchButtonTitle: "保存至搜索历史",
                removeRecentSearchButtonTitle: "从搜索历史中移除",
                favoriteSearchesTitle: "收藏",
                removeFavoriteSearchButtonTitle: "从收藏中移除",
              },
              errorScreen: {
                titleText: "无法获取结果",
                helpText: "你可能需要检查你的网络连接",
              },
              footer: {
                selectText: "选择",
                navigateText: "切换",
                closeText: "关闭",
                searchByText: "搜索提供者",
              },
              noResultsScreen: {
                noResultsText: "无法找到相关结果",
                suggestedQueryText: "你可以尝试查询",
                reportMissingResultsText: "你认为该查询应该有结果?",
                reportMissingResultsLinkText: "点击反馈",
              },
            },
          },
        },
        "/en/": {
          placeholder: "Search Documentation",
          translations: {
            button: {
              buttonText: "Search Documentation",
            },
          },
        },
      },
    }),
})

3. Vuepress 2 Reco 主题

主题自带algolia插件,编辑config.ts,定位到以下代码

  algolia: {
        appId: 'XXXXXXXXXX',
        apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        indexName: 'XXXXXXXXXX',
        //inputSelector: '### REPLACE ME ####',
        // algoliaOptions: { 'facetFilters': ["lang:$LANG"] },
        debug: false // Set debug to true if you want to inspect the dropdown
    },

4. Vuepress 2 Plume 主题

主题自带algolia插件,启用algolia需取消本地搜索

plugins: {
    // search: {
    //    请注释掉本地搜索
    // },
    docsearch: {
        appId: 'Application 🆔',
        apiKey: 'Search_API_Key',
        indexName: 'XXX',
    }
}

(四)测试

在这里插入图片描述

三、meilisearch搜索

algolia免费额度毕竟有限,开源meilisearch提供每月10k次搜索、10k次记录,支持自建搜索引擎

Vuepress 2现已接入meilisearch,使用自己服务器或者本地端口免费使用meilisearch
当然付费用户,可使用meilisearch cloud

在这里插入图片描述

在这里插入图片描述

(一)安装curl

yum -y install curl

(二)启动meilisearch

docker run -it --rm \
  -p 7700:7700 \
  -v $(pwd)/meili_data:/meili_data \
 getmeili/meilisearch:latest

在这里插入图片描述

默认会提供一个master_key,请一定要保存好,然后Ctrl c关闭进程

(三)两种方式访问

方式一:HTTP访问

在这里插入图片描述

在这里插入图片描述

替换下面的 <YOUR_MASTER_KEY> 为你自己的主密钥,启动meilisearch

docker run -d -it --rm \
  --name MeiliSearch \
  -e MEILI_MASTER_KEY='<YOUR_MASTER_KEY>' \
  -e MEILI_ENV=production \
  -p 7700:7700 \
  -v $(pwd)/meili_data:/meili_data \
  getmeili/meilisearch:latest

启动后,刷新网页,显示下面效果表明meiliseach正常工作
在这里插入图片描述
在这里插入图片描述

方式二:SSL访问

参考官网使用HTTP/2和ssl与Mellisearch教程,如需在SSL情况下使用,需提前把SSL证书密钥文件放上去,请按照实际修改下面内容然后运行

  • /path/to/cert/是存放SSL证书和密钥文件位置
  • your_website.pem和your_website.key是nginx环境下ssl证书文件
docker run -d -it --rm \
  --name MeiliSearch \
  -e MEILI_MASTER_KEY='<YOUR_MASTER_KEY>' \
  -e MEILI_ENV=production \
  -v /path/to/cert/:/certs \
  -p 7700:7700 \
  -v $(pwd)/meili_data:/meili_data \
  getmeili/meilisearch:latest \
  meilisearch --ssl-cert-path /certs/your_website.pem --ssl-key-path /certs/your_website.key

在这里插入图片描述

(四)爬取数据

根目录新建config.json,模版请参考上面的algolia

说明
<YOUR_INDEX_NAME>索引名字
<YOUR_WEBSITE_URL>服务器IP/域名
{
  "index_uid": "<YOUR_INDEX_NAME>",
  "start_urls": ["https://<YOUR_WEBSITE_URL>/"],
  "sitemap_urls": ["https://<YOUR_WEBSITE_URL>/sitemap.xml"],
  "selectors": {
    "lvl0": {
      "selector": ".vp-sidebar-heading.active",
      "global": true,
      "default_value": "Documentation"
    },
    "lvl1": "[vp-content] h1",
    "lvl2": "[vp-content] h2",
    "lvl3": "[vp-content] h3",
    "lvl4": "[vp-content] h4",
    "lvl5": "[vp-content] h5",
    "lvl6": "[vp-content] h6",
    "content": "[vp-content] p, [vp-content] li",
    "lang": {
      "selector": "/html/@lang",
      "global": true,
      "type": "xpath"
    }
  },
  "custom_settings": {
    "searchableAttributes": [
      "hierarchy_radio_lvl0",
      "hierarchy_radio_lvl1",
      "hierarchy_radio_lvl2",
      "hierarchy_radio_lvl3",
      "hierarchy_radio_lvl4",
      "hierarchy_radio_lvl5",
      "hierarchy_lvl0",
      "hierarchy_lvl1",
      "hierarchy_lvl2",
      "hierarchy_lvl3",
      "hierarchy_lvl4",
      "hierarchy_lvl5",
      "hierarchy_lvl6",
      "content",
      "lang",
      "objectID",
      "page_rank",
      "level",
      "position"
    ],
    "displayedAttributes": [
      "hierarchy_radio_lvl0",
      "hierarchy_radio_lvl1",
      "hierarchy_radio_lvl2",
      "hierarchy_radio_lvl3",
      "hierarchy_radio_lvl4",
      "hierarchy_radio_lvl5",
      "hierarchy_lvl0",
      "hierarchy_lvl1",
      "hierarchy_lvl2",
      "hierarchy_lvl3",
      "hierarchy_lvl4",
      "hierarchy_lvl5",
      "hierarchy_lvl6",
      "anchor",
      "url",
      "lang",
      "content",
      "objectID"
    ],
    "filterableAttributes": ["lang"]
  }
}

这里我以vuepress2官网为例

在这里插入图片描述

确保meilisearch正在运行,然后

docker run -t --rm \
  --network=host \
  -e MEILISEARCH_HOST_URL='<YOUR_WEBSITE_URL>' \
  -e MEILISEARCH_API_KEY='<YOUR_MASTER_KEY>' \
  -v <path/to/your/config.json>:/docs-scraper/config.json \
  getmeili/docs-scraper:latest pipenv run ./docs_scraper config.json
说明
<YOUR_INDEX_NAME>索引名字
<YOUR_WEBSITE_URL>服务器IP/域名
<path/to/your/config.json>config.json文件绝对路径

爬取成功!
在这里插入图片描述

(五)search_key

出于安全考虑,容器不会暴露在外,我们新建search_key用于公开访问

curl \
  -X POST '<YOUR_WEBSITE_URL>/keys' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <MASTER_KEY>' \
  --data-binary '{
    "indexes": ["<YOUR_INDEX_NAME>"],
    "actions": ["search"],
    "expiresAt": null,
    "description": "Search key for <YOUR_INDEX_NAME>"
  }'
说明
<YOUR_WEBSITE_URL>服务器IP/域名
<MASTER_KEY>master_key
<YOUR_INDEX_NAME>索引名字

然后会返回下面的值,复制下面的<YOUR_SEARCH_KEY>

{
  "name": null,
  "description": "Search key for <YOUR_INDEX_NAME>",
  "key": "<YOUR_SEARCH_KEY>",
  "uid": "b84d1be5-caa5-4752-b078-8f40be39051d",
  "actions": ["search"],
  "indexes": ["<YOUR_INDEX_NAME>"],
  "expiresAt": null,
  "createdAt": "2025-04-21T11:11:51.61628879Z",
  "updatedAt": "2025-04-21T11:11:51.61628879Z"
}

在这里插入图片描述

(六)插件安装

两个插件任选其一即可

1.官方插件

官方链接直达

在这里插入图片描述

pnpm install -D @vuepress/plugin-meilisearch@next

编辑配置文件config.js

说明
<YOUR_WEBSITE_URL>服务器IP/域名
<YOUR_SEARCH_KEY>搜索key
<YOUR_INDEX_NAME>索引名字
import { meilisearchPlugin } from '@vuepress/plugin-meilisearch'

export default {
  plugins: [
    meilisearchPlugin({
      host: '<YOUR_WEBSITE_URL>',
      apiKey: '<YOUR_SEARCH_KEY>',
      indexUid: '<YOUR_INDEX_NAME>',
    }),
  ],
}

2.第三方开发

GitHub用户JQiue和sixmillions开发了vuepress-plugin-meilisearch2插件
点击访问插件使用教程

在这里插入图片描述

开源仓库地址

在这里插入图片描述

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值