找回密码
 立即注册
首页 业界区 业界 algolia使用配置教程-为SSG静态站增加algolia搜索功能 ...

algolia使用配置教程-为SSG静态站增加algolia搜索功能

鞠彗云 2025-6-3 00:32:32
要构建SSG静态站点时,一般为了方便增加algolia搜索框,但这里algolia配置使用时用很多的坑,折腾了我好几天,网上没有一个可用的教程。
自己弄了几天,终于搞明白里面的道道了,现在分享出来给大家,避免踩坑。
前面注册的就不用了。
比如:用的docusaurus、vitepress构建SSG静态站点,安装algolia依赖的,都会有algolia的设置。此处不讲这些简单的。
先看效果图:
1.png

algolia设置步骤


  • docusaurus、vitepress设置appid、key、抓取配置等
  • algolia新建app
  • 创建爬虫
  • 设置索引
实操说明

下面以我自构建的一个gulp文档静态站点:https://gulpjs.uihtm.com 为演示,一步步怎么设置完成的。
静态站配置algolia密钥、索引名、appid

本站点是使用docusaurus来构建静态站点的,在docusaurus.config.js配置文件里有一个设置algolia的appId、apiKey、indexName
2.png
  1. algolia: {
  2.       appId: 'XQC8CUNYC9',
  3.       apiKey: '1fe23b551c6d578e296aeb91ef858c2e',
  4.       indexName: 'deploy-gulpjs',
  5.       contextualSearch: true,
  6.       searchParameters: {
  7.         facetFilters: ['language:zh-CN']
  8.       }
  9.     }
复制代码
这里的appId、apiKey、indexName对应的是algolia后台:https://dashboard.algolia.com/ 面板里的。
先在后台创建应用才能得到appid、apikey,
创建应用

3.png

选择地域区,随便选一个
4.png

5.png

创建完点击应用就可以对相应应用设置
6.png

apikey在这里:Search API Key

7.png

8.png

algolia 索引index创建

点击这里的serach就会到index页面,
9.png

默认索引里的,records,record size是空的0,点击event data进去,再点Crawler爬取设置
10.png

11.png

设置爬虫

创建爬虫,爬虫可以创建多个的,其实只需要一个就够了。到时验证域名,点击爬虫名称,进入爬取设置
12.png

13.png

点击Resume crawling 会自动爬取域名里的网址,爬取前可以设置,爬取的开始域名,最有url数和爬取周期
14.png

这里就是爬取完的显示,这是有爬取完成的url数量,url里的记录数,
注意这里的Indices,

  • 这里有个名称为:deploy-gulpjs,的索引名。是填到静态站点配置里的,
  • 索引是可以有多个
  • 如果没设置edtor,爬取出来的索引是随机名,后面可以重命名
  • 注意索引有没有records记录
15.png

在爬取过程,有一个deploy-gulpjs.tmp的名称,在爬完后自动消失。
16.png

自定义爬虫设置

在这里点击editor,可以设置测试爬取内容等配置。
17.png

18.png

可以编辑里面的json参数设置爬取。代码里的indexName: "deploy-gulpjs",就是我设置爬取后生成的索引名称,还有一点是站点语言,一般
  1. new Crawler({
  2.   appId: "xxxxxx",
  3.   indexPrefix: "",
  4.   rateLimit: 8,
  5.   maxUrls: null,
  6.   schedule: "on the 12 day of the month",
  7.   startUrls: ["https://gulpjs.uihtm.com"],
  8.   renderJavaScript: false,
  9.   sitemaps: ["https://gulpjs.uihtm.com/sitemap.xml"],
  10.   ignoreCanonicalTo: true,
  11.   discoveryPatterns: ["https://gulpjs.uihtm.com/**"],
  12.   actions: [
  13.     {
  14.       indexName: "deploy-gulpjs",
  15.       pathsToMatch: ["https://gulpjs.uihtm.com/**"],
  16.       recordExtractor: ({ $, helpers }) => {
  17.         const lvl0 =
  18.           $(
  19.             ".menu__link.menu__link--sublist.menu__link--active, .navbar__item.navbar__link--active",
  20.           )
  21.             .last()
  22.             .text() || "Documentation";
  23.         const records = helpers.docsearch({
  24.           recordProps: {
  25.             lvl0: {
  26.               selectors: "",
  27.               defaultValue: "gulpjs",
  28.             },
  29.             lvl1: ["header h1", "article h1"],
  30.             lvl2: "article h2",
  31.             lvl3: "article h3",
  32.             lvl4: "article h4",
  33.             lvl5: "article h5, article td:first-child",
  34.             lvl6: "article h6",
  35.             content: "article p, article li, article td:last-child",
  36.           },
  37.           aggregateContent: true,
  38.           recordVersion: "v3",
  39.         });
  40.         // 设置中文
  41.         records.forEach((record) => {
  42.           record.lang = "zh-CN";
  43.         });
  44.         return records;
  45.       },
  46.     },
  47.   ],
  48.   safetyChecks: { beforeIndexPublishing: { maxLostRecordsPercentage: 30 } },
  49.   initialIndexSettings: {
  50.     deployGulpjs: {
  51.       attributesForFaceting: [
  52.         "type",
  53.         "lang",
  54.         "language",
  55.         "version",
  56.         "docusaurus_tag",
  57.       ],
  58.       attributesToRetrieve: [
  59.         "hierarchy",
  60.         "content",
  61.         "anchor",
  62.         "url",
  63.         "url_without_anchor",
  64.         "type",
  65.       ],
  66.       attributesToHighlight: ["hierarchy", "content"],
  67.       attributesToSnippet: ["content:10"],
  68.       camelCaseAttributes: ["hierarchy", "content"],
  69.       searchableAttributes: [
  70.         "unordered(hierarchy.lvl0)",
  71.         "unordered(hierarchy.lvl1)",
  72.         "unordered(hierarchy.lvl2)",
  73.         "unordered(hierarchy.lvl3)",
  74.         "unordered(hierarchy.lvl4)",
  75.         "unordered(hierarchy.lvl5)",
  76.         "unordered(hierarchy.lvl6)",
  77.         "content",
  78.       ],
  79.       distinct: true,
  80.       attributeForDistinct: "url",
  81.       customRanking: [
  82.         "desc(weight.pageRank)",
  83.         "desc(weight.level)",
  84.         "asc(weight.position)",
  85.       ],
  86.       ranking: [
  87.         "words",
  88.         "filters",
  89.         "typo",
  90.         "attribute",
  91.         "proximity",
  92.         "exact",
  93.         "custom",
  94.       ],
  95.       highlightPreTag: '',
  96.       highlightPostTag: "",
  97.       minWordSizefor1Typo: 3,
  98.       minWordSizefor2Typos: 7,
  99.       allowTyposOnNumericTokens: false,
  100.       minProximity: 1,
  101.       ignorePlurals: true,
  102.       advancedSyntax: true,
  103.       attributeCriteriaComputedByMinProximity: true,
  104.       removeWordsIfNoResults: "allOptional",
  105.     },
  106.   },
  107.   apiKey: "xxxxxxx",
  108. });
复制代码
看你的静态站点html是设置什么语言,lang="zh-CN",那对应要json参数设置爬取
19.png
  1. // 设置中文
  2. records.forEach((record) => {
  3.   record.lang = "zh-CN";
  4. });
复制代码
20.png

21.png

重点,索引设置

索引设置里的Searchable attributes 、Facets、Language
22.png

23.png

24.png

25.png

最后有这样的数据出来就大功告成了。


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册