4

Hugo 博客如何更新 Algolia 索引

 2 years ago
source link: https://ttys3.dev/post/how-to-update-hugo-blog-algolia-index/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Hugo 博客如何更新 Algolia 索引

2021-06-03

:: 荒野無燈

:: Mod 2021-06-04(41c14c9)

#hugo  #search  #index  #updater 

June 4, 2021

集成 Algolia 后,最简单无脑的更新索引方式就是每次构建后由 Netlify 触发 webhook 通知 Algolia 自动抓取。 这也就是 Algolia Crawler for Netlify 干的活。

但是老灯使用一段时间后发现存在一些问题:

  1. 每发一篇博客,甚至可能是只改动了一个字就要触发全量索引 (git push 触发了 Netlify CI CD -> webhook -> Algolia Crawler 干活 -> 抓取全站 URL 并索引)。一是浪费资源,二是低低效(速度慢)。
  2. 很多不需要索引的页面也被索引进去了,比如 tags 和 categories 的分页列表页面。我博客总共才70篇文章不到,索引后的页面居然有300多!

要个性化的索引,还得自己动手。毕竟你自己的内容,自己最清楚。

浏览了下 Github, star 数量多的相关项目几乎没有。

https://github.com/spotlightpa/algolia-indexer 0 star

https://github.com/duckpuppy/algolia-hugo 8 star 但是我稍看了下代码,觉得他把简单的事情搞复杂了,还用到了 viper 这个 go mod, 天啊,放弃吧。

继续找,找到一个 https://github.com/naah69/Hugo-Algolia-Chinese-Builder 但是,这个 repo 的命名首先就很不 golang style.

于是我 fork 出来新的 https://github.com/ttys3/hugo-algolia-updater 并整理了一下代码。

加上 golangci-lint 和 zap logger. 最终效果如图:

代码还有待优化,时间有限,先这样了。

Refs

https://www.algolia.com/doc/tools/crawler/netlify-plugin/quick-start/

https://www.netlify.com/blog/2017/10/10/replacing-our-search-with-algolia/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK