-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.go
470 lines (425 loc) · 15.2 KB
/
indexer.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package es
import (
"bufio"
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/olivere/elastic.v6"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/utils"
)
type Indexer struct {
indices []Index
}
func MakeProdIndexer(date string, mdb *sql.DB, esc *elastic.Client) (*Indexer, error) {
return MakeIndexer("prod", date, consts.ES_ALL_RESULT_TYPES, mdb, esc)
}
func MakeFakeIndexer(mdb *sql.DB, esc *elastic.Client) (*Indexer, error) {
return MakeIndexer("fake", "fake-date", []string{}, mdb, esc)
}
// Receives namespace and list of indexes names.
func MakeIndexer(namespace string, date string, names []string, mdb *sql.DB, esc *elastic.Client) (*Indexer, error) {
log.Infof("Indexer - Make indexer - %s - %s - %s", namespace, date, strings.Join(names, ", "))
indexer := new(Indexer)
indexer.indices = make([]Index, len(names))
for i, name := range names {
if name == consts.ES_RESULT_TYPE_UNITS {
indexer.indices[i] = MakeContentUnitsIndex(namespace, date, mdb, esc)
} else if name == consts.ES_RESULT_TYPE_SOURCES {
indexer.indices[i] = MakeSourcesIndex(namespace, date, mdb, esc)
} else if name == consts.ES_RESULT_TYPE_TAGS {
indexer.indices[i] = MakeTagsIndex(namespace, date, mdb, esc)
} else if name == consts.ES_RESULT_TYPE_COLLECTIONS {
indexer.indices[i] = MakeCollectionsIndex(namespace, date, mdb, esc)
} else if name == consts.ES_RESULT_TYPE_BLOG_POSTS {
indexer.indices[i] = MakeBlogIndex(namespace, date, mdb, esc)
} else if name == consts.ES_RESULT_TYPE_TWEETS {
indexer.indices[i] = MakeTweeterIndex(namespace, date, mdb, esc)
} else {
return nil, errors.New(fmt.Sprintf("MakeIndexer - Invalid index name: %+v", name))
}
}
return indexer, nil
}
func ProdIndexDate(esc *elastic.Client) (error, string) {
indexDate := viper.GetString("elasticsearch.index-date")
if indexDate == "" {
return aliasedIndexDate(esc, "prod", consts.ES_RESULTS_INDEX)
}
return nil, indexDate
}
// Deprecated, use ProdIndexDateForEvents, we should alway
//func ProdAliasedIndexDate(esc *elastic.Client) (error, string) {
// return aliasedIndexDate(esc, "prod", consts.ES_RESULTS_INDEX)
//}
func aliasedIndexDate(esc *elastic.Client, namespace string, name string) (error, string) {
aliasRegexp := IndexName(namespace, name, ".*", ".*")
alias := indexAliasName(namespace, name, "%s")
return AliasedIndex(esc, alias, aliasRegexp)
}
func AliasedIndex(esc *elastic.Client, alias string, aliasRegexp string) (error, string) {
aliasesService := elastic.NewAliasesService(esc)
prevIndicesByAlias := make(map[string]string)
aliasesRes, err := aliasesService.Do(context.TODO())
if err != nil {
return errors.Wrapf(err, "Error fetching asiases, alias: %s regexp: %s", alias, aliasRegexp), ""
}
for indexName, indexResult := range aliasesRes.Indices {
matched, err := regexp.MatchString(aliasRegexp, indexName)
if err != nil {
return errors.Wrapf(err, "Error matching regex, alias: %s, regexp: %s, indexName: %s", alias, aliasRegexp, indexName), ""
}
if matched {
if len(indexResult.Aliases) > 1 {
return errors.New(fmt.Sprintf("Expected no more then one alias for %s, got %d", indexName, len(indexResult.Aliases))), ""
}
if len(indexResult.Aliases) == 1 {
prevIndicesByAlias[indexResult.Aliases[0].AliasName] = indexName
}
}
}
date := ""
indicesExist := false
for _, lang := range consts.ALL_KNOWN_LANGS {
prevIndex, ok := prevIndicesByAlias[fmt.Sprintf(alias, lang)]
if ok {
indicesExist = true
parts := strings.Split(prevIndex, "_")
if len(parts) != 4 {
return errors.New(fmt.Sprintf("Expected 4 parts in index name %s, got %d.", prevIndex, len(parts))), ""
}
if date == "" {
date = parts[len(parts)-1]
}
if date != parts[len(parts)-1] {
return errors.New(fmt.Sprintf("Expected index date to be %s got %s at index %s", date, parts[len(parts)], prevIndex)), ""
}
} else {
if indicesExist {
log.Warnf("Indexer - Did not find index name for %s", alias)
}
}
}
if date == "" && indicesExist {
return errors.New("At least one aliased index should have date specified."), ""
}
return nil, date
}
func SwitchProdAliasToCurrentIndex(date string, esc *elastic.Client) error {
return SwitchAliasToCurrentIndex("prod", consts.ES_RESULTS_INDEX, date, esc)
}
func SwitchAliasToCurrentIndex(namespace string, name string, date string, esc *elastic.Client) error {
alias := indexAliasName(namespace, name, "%s")
iName := IndexName(namespace, name, "%s", date)
err, prevDate := aliasedIndexDate(esc, namespace, name)
if err != nil {
return errors.Wrapf(err, "Error getting prev date, namespace: %s, name: %s, date: %s.", namespace, name, date)
}
prevIndex := ""
if prevDate != "" {
prevIndex = IndexName(namespace, name, "%s", prevDate)
}
return SwitchAlias(alias, prevIndex, iName, esc)
}
func SwitchAlias(alias string, prev string, next string, esc *elastic.Client) error {
finalErr := error(nil)
for _, lang := range consts.ALL_KNOWN_LANGS {
aliasService := elastic.NewAliasService(esc)
if prev != "" {
aliasService = aliasService.Remove(fmt.Sprintf(prev, lang), fmt.Sprintf(alias, lang))
}
aliasService.Add(fmt.Sprintf(next, lang), fmt.Sprintf(alias, lang))
res, err := aliasService.Do(context.TODO())
if err != nil || !res.Acknowledged {
finalErr = utils.JoinErrors(err, errors.Wrapf(err, "Failed due to error or Acknowledged is false. prev: %s, next: %s, alias: %s.",
fmt.Sprintf(prev, lang), fmt.Sprintf(next, lang), fmt.Sprintf(alias, lang)))
continue
}
}
return finalErr
}
func openIndex(indexName string, esc *elastic.Client) {
openRes, err := esc.OpenIndex(indexName).Do(context.TODO())
if err != nil {
log.Error(errors.Wrapf(err, "OpenIndex: %s", indexName))
return
}
if !openRes.Acknowledged {
log.Errorf("OpenIndex not Acknowledged: %s", indexName)
return
}
err = esc.WaitForYellowStatus("10s")
if err != nil {
log.Errorf("OpenIndex, failed waiting for yellow status.")
}
}
type IndexNameByLang func(lang string) string
func IndexNameFuncByNamespaceAndDate(namespace string, indexDate string) IndexNameByLang {
return func(lang string) string {
if indexDate != "" {
// Use specific date index.
return IndexName(namespace, consts.ES_RESULTS_INDEX, lang, indexDate)
} else {
// Use prooduction (alias) index.
return IndexNameForServing(namespace, consts.ES_RESULTS_INDEX, lang)
}
}
}
func UpdateSynonyms(esc *elastic.Client, indexNameByLang IndexNameByLang) error {
folder, err := SynonymsFolder()
if err != nil {
return errors.Wrap(err, "SynonymsFolder not available.")
}
files, err := ioutil.ReadDir(folder)
if err != nil {
return errors.Wrap(err, "Cannot read synonym files list.")
}
type SynonymGraphSU struct {
Type string `json:"type"`
Tokenizer string `json:"tokenizer"`
Synonyms []string `json:"synonyms"`
}
type FilterSU struct {
SynonymGraph SynonymGraphSU `json:"synonym_graph"`
}
type AnalysisSU struct {
Filter FilterSU `json:"filter"`
}
type IndexSU struct {
Analysis AnalysisSU `json:"analysis"`
}
type SU struct {
Index IndexSU `json:"index"`
}
body := SU{
IndexSU{
AnalysisSU{
FilterSU{
SynonymGraphSU{
Type: "synonym_graph",
Tokenizer: "keyword",
},
},
},
},
}
for _, fileInfo := range files {
keywords := make([]string, 0)
// Convention: file name without extension is the language code.
var ext = filepath.Ext(fileInfo.Name())
var lang = fileInfo.Name()[0 : len(fileInfo.Name())-len(ext)]
if !utils.Contains(utils.Is(consts.ALL_KNOWN_LANGS), lang) {
log.Warningf("Strange synonyms file: %s, skipping.", fileInfo.Name())
continue
}
filePath := filepath.Join(folder, fileInfo.Name())
file, err := os.Open(filePath)
if err != nil {
return errors.Wrapf(err, "Unable to open synonyms file: %s.", filePath)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Blank lines and lines starting with pound are comments (like Solr format).
trimmed := strings.TrimSpace(line)
if trimmed != "" {
if !strings.HasPrefix(trimmed, "#") {
commaSeperated := strings.Replace(trimmed, "\t", ",", -1)
keywords = append(keywords, commaSeperated)
}
}
}
if err := scanner.Err(); err != nil {
return errors.Wrapf(err, "Error at scanning synonym config file: %s.", filePath)
}
// Set keywords to update synonyms
body.Index.Analysis.Filter.SynonymGraph.Synonyms = keywords
// Close the index in order to update the synonyms
log.Infof("Synonyms language: %s.", lang)
indexName := indexNameByLang(lang)
closeRes, err := esc.CloseIndex(indexName).Do(context.TODO())
if err != nil {
return errors.Wrapf(err, "CloseIndex: %s", indexName)
}
if !closeRes.Acknowledged {
return errors.New(fmt.Sprintf("CloseIndex not Acknowledged: %s", indexName))
}
defer openIndex(indexName, esc)
bodyStr, err := json.Marshal(body)
if err != nil {
return errors.New(fmt.Sprintf("Failed marshding %+v.", body))
}
// Using standard HTTP put call instead of esc.IndexPutSettings(indexName).BodyJson(body).Do(context.TODO())
// due to the fact that this version of elastic hides error when synonyms are not updated properly.
if runtime.GOOS == "windows" {
indexName = url.QueryEscape(indexName)
}
url := fmt.Sprintf("%s/%s/_settings", viper.GetString("elasticsearch.url"), indexName)
log.Infof("Sending to %s: %s", url, string(bodyStr))
contents, err := putRequest(url, bytes.NewBuffer(bodyStr))
if err != nil {
return errors.Wrapf(err, "IndexPutSettings: %s with keywords: \n%s\n", indexName, strings.Join(keywords, "\n"))
}
settingsRes := new(elastic.IndicesPutSettingsResponse)
if err := json.Unmarshal(contents, settingsRes); err != nil {
return errors.New(fmt.Sprintf("Error decoding ret"))
}
if !settingsRes.Acknowledged {
return errors.New(fmt.Sprintf("IndexPutSettings not Acknowledged: %s", indexName))
}
}
return nil
}
func putRequest(url string, data io.Reader) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodPut, url, data)
req.Header.Set("Content-Type", "application/json")
if err != nil {
log.Fatal(err)
}
response, err := client.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "While calling PUT request %s", url)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, errors.Wrapf(err, "While reading PUT response for %s", url)
}
if response.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Error, received: %s", string(contents)))
}
return contents, nil
}
func (indexer *Indexer) ReindexAll(esc *elastic.Client) error {
log.Info("Indexer - Re-Indexing everything")
if err := indexer.CreateIndexes(); err != nil {
return errors.Wrapf(err, "Error creating indexes.")
}
log.Info("Indexer - Updating Synonyms.")
if len(indexer.indices) == 0 {
return errors.New("Expected indices to be more than 0.")
}
if err := UpdateSynonyms(esc, IndexNameFuncByNamespaceAndDate(indexer.indices[0].Namespace(), indexer.indices[0].IndexDate())); err != nil {
return errors.Wrapf(err, "Error updating synonyms.")
}
log.Info("Indexer - Synonymns updated.")
done := make(chan string)
errs := make([]error, len(indexer.indices))
for i := range indexer.indices {
go func(i int) {
errs[i] = indexer.indices[i].ReindexAll()
done <- indexer.indices[i].ResultType()
}(i)
}
for _ = range indexer.indices {
name := <-done
log.Infof("Finished: %s", name)
}
err := (error)(nil)
for i, e := range errs {
err = utils.JoinErrorsWrap(err, e, fmt.Sprintf("Reindex of: %s", indexer.indices[i].IndexName("xx")))
}
return err
}
func (indexer *Indexer) RefreshAll() error {
log.Info("Indexer - Refresh (sync new indexed documents) all indices.")
err := (error)(nil)
for _, index := range indexer.indices {
err = utils.JoinErrorsWrap(err, index.RefreshIndex(), fmt.Sprintf("Error creating index: %s.", index.IndexName("xx")))
}
return err
}
func (indexer *Indexer) CreateIndexes() error {
log.Infof("Indexer - Create new indices in elastic: %+v", indexer.indices)
err := (error)(nil)
for _, index := range indexer.indices {
err = utils.JoinErrorsWrap(err, index.CreateIndex(), fmt.Sprintf("Error creating index: %s.", index.IndexName("xx")))
}
return err
}
func (indexer *Indexer) DeleteIndexes() error {
log.Info("Indexer - Delete indices from elastic.")
err := (error)(nil)
for _, index := range indexer.indices {
err = utils.JoinErrorsWrap(err, index.DeleteIndex(), fmt.Sprintf("Error creating index: %s.", index.IndexName("xx")))
}
return err
}
func (indexer *Indexer) Update(scope Scope) error {
// Maps const.ES_UID_TYPE_* to list of uids.
// This is required to correctly add the removed uids per type.
removedByResultType := make(map[string][]string)
err := (error)(nil)
for _, index := range indexer.indices {
removed, e := index.RemoveFromIndex(scope)
for resultType, removedUIDs := range removed {
if _, ok := removedByResultType[resultType]; !ok {
removedByResultType[resultType] = []string{}
}
removedByResultType[resultType] = append(removedByResultType[resultType], removedUIDs...)
}
err = utils.JoinErrorsWrap(err, e, fmt.Sprintf("Error updating: %+v", scope))
}
err = utils.JoinErrorsWrap(err, indexer.RefreshAll(), fmt.Sprintf("Error Refreshing: %+v", scope))
for _, index := range indexer.indices {
removed, ok := removedByResultType[index.ResultType()]
if !ok {
removed = []string{}
}
err = utils.JoinErrorsWrap(err, index.AddToIndex(scope, removed), fmt.Sprintf("Error updating: %+v", scope))
}
return err
}
// Set of MDB event handlers to incrementally change all indexes.
func (indexer *Indexer) CollectionUpdate(uid string) error {
log.Infof("Indexer - Index collection upadate event: %s", uid)
return indexer.Update(Scope{CollectionUID: uid})
}
func (indexer *Indexer) ContentUnitUpdate(uid string) error {
log.Infof("Indexer - Index content unit update event: %s", uid)
return indexer.Update(Scope{ContentUnitUID: uid})
}
func (indexer *Indexer) FileUpdate(uid string) error {
log.Infof("Indexer - Index file update event: %s", uid)
return indexer.Update(Scope{FileUID: uid})
}
func (indexer *Indexer) SourceUpdate(uid string) error {
log.Infof("Indexer - Index source update event: %s", uid)
return indexer.Update(Scope{SourceUID: uid})
}
func (indexer *Indexer) TagUpdate(uid string) error {
log.Infof("Indexer - Index tag update event: %s", uid)
return indexer.Update(Scope{TagUID: uid})
}
func (indexer *Indexer) PersonUpdate(uid string) error {
log.Infof("Indexer - Index person update event: %s", uid)
return indexer.Update(Scope{PersonUID: uid})
}
func (indexer *Indexer) PublisherUpdate(uid string) error {
log.Infof("Indexer - Index publisher update event: %s", uid)
return indexer.Update(Scope{PublisherUID: uid})
}
func (indexer *Indexer) BlogPostUpdate(id string) error {
log.Infof("Indexer - Index blog post update event: %v", id)
return indexer.Update(Scope{BlogPostWPID: id})
}
func (indexer *Indexer) TweetUpdate(tid string) error {
log.Infof("Indexer - Index tweet update event: %v", tid)
return indexer.Update(Scope{TweetTID: tid})
}