-
Notifications
You must be signed in to change notification settings - Fork 99
/
client.go
315 lines (266 loc) · 9.24 KB
/
client.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package algolia
import (
"fmt"
"time"
"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
// Client provides access to Hermes indexes in Algolia.
type Client struct {
*search.Client
// Docs is an Algolia index for searching documents.
Docs *search.Index
// DocsCreatedTimeAsc is an Algolia replica of the docs index that is sorted
// by ascending created time.
DocsCreatedTimeAsc *search.Index
// DocsCreatedTimeDesc is an Algolia replica of the docs index that is sorted
// by descending created time.
DocsCreatedTimeDesc *search.Index
// DocsModifiedTimeDesc is an Algolia replica of the docs index that is sorted
// by descending modified time.
DocsModifiedTimeDesc *search.Index
// Drafts is an Algolia index for storing metadata for draft documents.
Drafts *search.Index
// DraftsCreatedTimeAsc is an Algolia replica of the drafts index that is sorted
// by ascending created time.
DraftsCreatedTimeAsc *search.Index
// DraftsCreatedTimeDesc is an Algolia replica of the drafts index that is sorted
// by descending created time.
DraftsCreatedTimeDesc *search.Index
// DraftsModifiedTimeDesc is an Algolia replica of the drafts index that is sorted
// by descending modified time.
DraftsModifiedTimeDesc *search.Index
// Internal is an Algolia index for storing internal Hermes metadata.
Internal *search.Index
// Links is an Algolia index for storing links of documents
Links *search.Index
// MissingFields is an Algolia index for storing missing fields from indexed
// documents.
MissingFields *search.Index
}
// Config is the configuration for interacting with the Algolia API.
type Config struct {
// ApplicationID is the Algolia Application ID.
ApplicationID string `hcl:"application_id,optional"`
// DocsIndexName is the name of the Algolia index for storing document
// metadata.
DocsIndexName string `hcl:"docs_index_name,optional"`
// DraftsIndexName is the name of the Algolia index for storing draft
// documents' metadata.
DraftsIndexName string `hcl:"drafts_index_name,optional"`
// InternalIndexName is the name of the Algolia index for storing internal
// Hermes metadata.
InternalIndexName string `hcl:"internal_index_name,optional"`
// LinksIndexName is the name of the Algolia index for storing links
LinksIndexName string `hcl:"links_index_name,optional"`
// MissingFieldsIndexName is the name of the Algolia index for storing missing
// fields from indexed documents.
MissingFieldsIndexName string `hcl:"missing_fields_index_name,optional"`
// SearchAPIKey is the Algolia API Key for searching Hermes indices.
SearchAPIKey string `hcl:"search_api_key,optional"`
// WriteAPIKey is the Algolia API Key for writing to Hermes indices.
WriteAPIKey string `hcl:"write_api_key,optional"`
}
// New initializes Hermes indices and returns a new Algolia client for
// indexing data.
func New(cfg *Config) (*Client, error) {
if err := validate(cfg); err != nil {
return nil, fmt.Errorf("error initializing Algolia client: %q", err)
}
c := &Client{}
// TODO: make timeouts configurable.
a := search.NewClientWithConfig(search.Configuration{
AppID: cfg.ApplicationID,
APIKey: cfg.WriteAPIKey,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
})
c.Docs = a.InitIndex(cfg.DocsIndexName)
c.Drafts = a.InitIndex(cfg.DraftsIndexName)
c.Internal = a.InitIndex(cfg.InternalIndexName)
c.Links = a.InitIndex(cfg.LinksIndexName)
c.MissingFields = a.InitIndex(cfg.MissingFieldsIndexName)
// Configure the docs index.
err := configureMainIndex(cfg.DocsIndexName, c.Docs, search.Settings{
// Attributes
AttributesForFaceting: opt.AttributesForFaceting(
"appCreated",
"approvers",
"approvedBy",
"docType",
"owners",
"product",
"status",
"searchable(tags)",
),
// Highlighting/snippeting
AttributesToSnippet: opt.AttributesToSnippet(
"content:7",
),
HighlightPostTag: opt.HighlightPostTag("</mark>"),
HighlightPreTag: opt.HighlightPreTag(`<mark class="hds-surface-warning hds-foreground-warning-on-surface">`),
SnippetEllipsisText: opt.SnippetEllipsisText("..."),
// Ranking
Replicas: opt.Replicas(
cfg.DocsIndexName+"_createdTime_asc",
cfg.DocsIndexName+"_createdTime_desc",
cfg.DocsIndexName+"_modifiedTime_desc",
),
})
if err != nil {
return nil, err
}
// Configure the docs createdTime_asc, createdTime_desc, modifiedTime_desc replica.
c.DocsCreatedTimeAsc = a.InitIndex(cfg.DocsIndexName + "_createdTime_asc")
c.DocsCreatedTimeDesc = a.InitIndex(cfg.DocsIndexName + "_createdTime_desc")
c.DocsModifiedTimeDesc = a.InitIndex(cfg.DocsIndexName + "_modifiedTime_desc")
err = configureReplicaIndexes(
cfg.DocsIndexName,
c.DocsCreatedTimeAsc,
c.DocsCreatedTimeDesc,
c.DocsModifiedTimeDesc,
)
if err != nil {
return nil, err
}
// Configure the drafts index.
err = configureMainIndex(cfg.DraftsIndexName, c.Drafts, search.Settings{
// Attributes
AttributesForFaceting: opt.AttributesForFaceting(
"contributors",
"docType",
"owners",
"product",
"status",
"tags",
),
// Ranking
Replicas: opt.Replicas(
cfg.DraftsIndexName+"_createdTime_asc",
cfg.DraftsIndexName+"_createdTime_desc",
cfg.DraftsIndexName+"_modifiedTime_desc",
),
})
if err != nil {
return nil, err
}
// Configure the drafts createdTime_asc, createdTime_desc, modifiedTime_desc replica.
c.DraftsCreatedTimeAsc = a.InitIndex(cfg.DraftsIndexName + "_createdTime_asc")
c.DraftsCreatedTimeDesc = a.InitIndex(cfg.DraftsIndexName + "_createdTime_desc")
c.DraftsModifiedTimeDesc = a.InitIndex(cfg.DraftsIndexName + "_modifiedTime_desc")
err = configureReplicaIndexes(
cfg.DraftsIndexName,
c.DraftsCreatedTimeAsc,
c.DraftsCreatedTimeDesc,
c.DraftsModifiedTimeDesc,
)
if err != nil {
return nil, err
}
return c, nil
}
// configureMainIndex configures the main index with settings
func configureMainIndex(indexName string, mainIndex *search.Index, settings search.Settings) error {
res, err := mainIndex.SetSettings(settings)
if err != nil {
return fmt.Errorf("error setting settings for %s index: %w",
indexName, err)
}
err = res.Wait()
if err != nil {
return fmt.Errorf("error setting settings for %s index: %w",
indexName, err)
}
return nil
}
func configureReplicaIndexes(
indexName string,
createdTimeAscIndex *search.Index,
createdTimeDescIndex *search.Index,
modifiedTimeDescIndex *search.Index,
) error {
// Configure the createdTime_asc replica for index.
_, err := createdTimeAscIndex.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting(
"contributors",
"docType",
"owners",
"product",
"status",
),
Ranking: opt.Ranking(
"asc(createdTime)",
),
})
if err != nil {
return fmt.Errorf(
"error setting settings for the %s createdTime_asc standard replica: %w",
indexName, err)
}
// Configure the createdTime_desc replica for index.
_, err = createdTimeDescIndex.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting(
"contributors",
"docType",
"owners",
"product",
"status",
),
Ranking: opt.Ranking(
"desc(createdTime)",
),
})
if err != nil {
return fmt.Errorf(
"error setting settings for the %s createdTime_desc standard replica: %w",
indexName, err)
}
// Configure the modifiedTime_desc replica for index.
_, err = modifiedTimeDescIndex.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting(
"status",
),
Ranking: opt.Ranking(
"desc(modifiedTime)",
),
})
if err != nil {
return fmt.Errorf(
"error setting settings for the %s modifiedTime_desc standard replica: %w",
indexName, err)
}
return nil
}
// NewSearchClient returns a new Algolia client for searching indices.
func NewSearchClient(cfg *Config) (*Client, error) {
if err := validate(cfg); err != nil {
return nil, fmt.Errorf("error initializing Algolia client: %q", err)
}
c := &Client{}
// TODO: make ReadTimeout configurable.
a := search.NewClient(cfg.ApplicationID, cfg.SearchAPIKey)
c.Docs = a.InitIndex(cfg.DocsIndexName)
c.DocsCreatedTimeAsc = a.InitIndex(cfg.DocsIndexName + "_createdTime_asc")
c.DocsCreatedTimeDesc = a.InitIndex(cfg.DocsIndexName + "_createdTime_desc")
c.DocsModifiedTimeDesc = a.InitIndex(cfg.DocsIndexName + "_modifiedTime_desc")
c.Drafts = a.InitIndex(cfg.DraftsIndexName)
c.DraftsCreatedTimeAsc = a.InitIndex(cfg.DraftsIndexName + "_createdTime_asc")
c.DraftsCreatedTimeDesc = a.InitIndex(cfg.DraftsIndexName + "_createdTime_desc")
c.DraftsModifiedTimeDesc = a.InitIndex(cfg.DraftsIndexName + "_modifiedTime_desc")
c.Internal = a.InitIndex(cfg.InternalIndexName)
c.Links = a.InitIndex(cfg.LinksIndexName)
return c, nil
}
// validate validates the Algolia configuration.
func validate(c *Config) error {
return validation.ValidateStruct(c,
validation.Field(&c.ApplicationID, validation.Required),
validation.Field(&c.DocsIndexName, validation.Required),
validation.Field(&c.DraftsIndexName, validation.Required),
validation.Field(&c.InternalIndexName, validation.Required),
validation.Field(&c.LinksIndexName, validation.Required),
validation.Field(&c.MissingFieldsIndexName, validation.Required),
validation.Field(&c.SearchAPIKey, validation.Required),
)
}