目录

Algolia搜索引擎配置

为什么是 Alogolia

  1. LoveIt这个主题只支持两个搜索引擎lunr和Alogolia
  2. lunr的中文分词的很不理想,需要使用lunr-zh 参考hugo说明
  3. Algolia 性能很好,唯一缺点就是需要上传 index.json ,但这个缺点基本都能通过自动化脚本弥补

Alogolia 使用方式

基本流程

  1. 官网注册账号,直接授权连接github账号即可
  2. 根据提示创建app和index
  3. 本地通过 hugo 指令生成静态页面的同时,public文件夹下会同时生成一个 index.json 文件,把他上传到对应刚才创建的algolia的index即可
1
2
3
4
5
6
├─public
│  ├─...
│  ├─css
│  ├─images
│  ├─index.json // 需要上传的文件
│  ├─posts

上传方式

1 手动上传

就是手动上传,没什么好说的

2 Algolia提供的API接口

这里采用Golang接口

流程很简单,核心内容只有三行代码
这里需要用到Alogolia个人中心的几个参数

1
2
3
4
5
6
// yourAppID = algolia的Application ID
// yourAdminApiKey = algolia的Admin API Key,非Search-Only API Key
client := search.NewClient(yourAppID, yourAdminApiKey)  
// yourAlgoliaIndex = 在algolia创建的index名
index := client.InitIndex(yourAlgoliaIndex) 
_, err = index.SaveObjects(needUploadIndexObject, opt.AutoGenerateObjectIDIfNotExist(true))

具体可进一步参考 文档

这里提供一个具体脚本和对应配置文件样例

脚本
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

// Alogolia更新脚本

import (
	"encoding/json"
	"io/ioutil"
	"log"

	"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
	"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
	"github.com/spf13/viper"
)

type IndexObj struct {
	Categories []string `json:"categories"`
	Content    string   `json:"content"`
	Date       string   `json:"date"`
	ObjectID   string   `json:"objectID"`
	Tags       []string `json:"tags"`
	Title      string   `json:"title"`
	URI        string   `json:"uri"`
}

// Config 配置结构体
type Config struct {
	IndexPath string      `mapstructure:"indexPath"`
	Algolia   AlgoliaConf `mapstructure:"algolia"`
}

type AlgoliaConf struct {
	AppID       string `mapstructure:"appID"`
	AdminApiKey string `mapstructure:"adminApiKey"`
	Index       string `mapstructure:"index"`
}

var conf = new(Config)

func main() {
	log.Println("Algolia begin update...")
	// 读取配置
	viper.SetConfigFile("./algolia-config.yaml")
	if err := viper.ReadInConfig(); err != nil {
		log.Fatalf("viper.ReadInConfig err:%v", err)
	}
	if err := viper.Unmarshal(conf); err != nil {
		log.Fatalf("viper.Unmarshal err:%v", err)
	}
	// fmt.Printf("%#v\n", conf)
	// 读取hugo index.json
	content, err := ioutil.ReadFile(conf.IndexPath)
	if err != nil {
		log.Fatalf("ioutil.ReadFile err:%v", err)
	}
	var indexObjs []IndexObj
	if err := json.Unmarshal(content, &indexObjs); err != nil {
		log.Fatalf("json.Unmarshal err:%v", err)
	}
	log.Println("Read index success!")
	// 更新
	client := search.NewClient(conf.Algolia.AppID, conf.Algolia.AdminApiKey)
	index := client.InitIndex(conf.Algolia.Index)
	_, err = index.SaveObjects(indexObjs, opt.AutoGenerateObjectIDIfNotExist(true))
	if err != nil {
		log.Fatalf("index.SaveObjects err:%v", err)
	}
	log.Println("Update success!")
}
配置文件
1
2
3
4
5
6
7
# Alogolia上传脚本的使用配置样例algolia-config.yaml
# 按照参数填入以下位置即可
indexPath: path/to/your/index.json
algolia:
  appID: xxxxxxxxxx
  adminApiKey: xxxxxxxxxxxxxxxxxxxxxxxxx
  index: xxxxx
Makefile

algolia-config.yamlmain.go或者编译后的程序放同一目录,目录结构如下:

1
2
3
4
5
6
7
8
9
├─algolia
│  ├─main.go				// 更新脚本
│  └─algolia-config.yaml	// 更新程序的配置文件
├─archetypes
├─assets
├─...
├─public					// 静态页面的生成目录
├─...
├─Makefile					// Makefile

在项目主目录,创建Makefile集成hugo指令

1
2
build:
	hugo; cd ./algolia; go run main.go

每次更新的时候只需要执行make build,就可以生成页面的同时更新algolia

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
hugo; cd ./algolia; go run main.go
Start building sites … 
hugo v0.100.2-d25cb2943fd94ecf781412aeff9682d5dc62e284+extended linux/amd64 BuildDate=2022-06-08T10:25:57Z VendorInfo=gohugoio

                   | ZH-CN  
-------------------+--------
  Pages            |    57  
  Paginator pages  |     1  
  Non-page files   |    50  
  Static files     |    83  
  Processed images |     0  
  Aliases          |    16  
  Sitemaps         |     1  
  Cleaned          |     0  

Total in 244 ms
2022/08/05 15:55:38 Algolia begin update...
2022/08/05 15:55:38 Read index success!
2022/08/05 15:55:41 Update success!

3 Github Action等工作流模式