-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.go
168 lines (150 loc) · 4.52 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
package es
import (
"database/sql"
"strings"
log "github.com/Sirupsen/logrus"
"gopkg.in/olivere/elastic.v5"
"github.com/Bnei-Baruch/archive-backend/consts"
)
type Indexer struct {
indices []Index
}
func MakeProdIndexer(mdb *sql.DB, esc *elastic.Client) *Indexer {
return MakeIndexer("prod", []string{
consts.ES_CLASSIFICATIONS_INDEX,
consts.ES_UNITS_INDEX,
consts.ES_COLLECTIONS_INDEX}, mdb, esc)
}
func MakeFakeIndexer(mdb *sql.DB, esc *elastic.Client) *Indexer {
return MakeIndexer("fake", []string{}, mdb, esc)
}
// Receives namespace and list of indexes names.
func MakeIndexer(namespace string, names []string, mdb *sql.DB, esc *elastic.Client) *Indexer {
log.Infof("Indexer - Make indexer - %s - %s", namespace, strings.Join(names, ", "))
indexer := new(Indexer)
indexer.indices = make([]Index, len(names))
for i, name := range names {
if name == consts.ES_CLASSIFICATIONS_INDEX {
indexer.indices[i] = MakeClassificationsIndex(namespace, mdb, esc)
} else if name == consts.ES_UNITS_INDEX {
indexer.indices[i] = MakeContentUnitsIndex(namespace, mdb, esc)
} else if name == consts.ES_COLLECTIONS_INDEX {
indexer.indices[i] = MakeCollectionsIndex(namespace, mdb, esc)
}
}
return indexer
}
func (indexer *Indexer) ReindexAll() error {
log.Info("Indexer - Re-Indexing everything")
for _, index := range indexer.indices {
// TODO: Check if indexing things in parallel will make things faster?
log.Info("Indexer - Deleting index.")
if err := index.DeleteIndex(); err != nil {
return err
}
log.Info("Indexer - Creating index.")
if err := index.CreateIndex(); err != nil {
return err
}
log.Info("Indexer - Reindexing")
if err := index.ReindexAll(); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) RefreshAll() error {
log.Info("Indexer - Refresh (sync new indexed documents) all indices.")
for _, index := range indexer.indices {
index.RefreshIndex()
}
return nil
}
func (indexer *Indexer) CreateIndexes() error {
log.Info("Indexer - Create new indices in elastic.")
for _, index := range indexer.indices {
if err := index.CreateIndex(); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) DeleteIndexes() error {
log.Info("Indexer - Delete indices from elastic.")
for _, index := range indexer.indices {
if err := index.DeleteIndex(); err != nil {
return err
}
}
return nil
}
// 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)
for _, index := range indexer.indices {
if err := index.Update(Scope{CollectionUID: uid}); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) ContentUnitUpdate(uid string) error {
log.Infof("Indexer - Index content unit update event: %s", uid)
for _, index := range indexer.indices {
// TODO: Optimize update to update elastic and not delete and then
// add. It might be a problem on bulk updates, i.e., of someone added
// some kind of tag for 1000 documents.
// In that case removeing and adding will be much slower then updating
// existing documents in elastic.
// Decicded to not optimize prematurly.
if err := index.Update(Scope{ContentUnitUID: uid}); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) FileUpdate(uid string) error {
log.Infof("Indexer - Index file update event: %s", uid)
for _, index := range indexer.indices {
if err := index.Update(Scope{FileUID: uid}); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) SourceUpdate(uid string) error {
log.Infof("Indexer - Index source update event: %s", uid)
for _, index := range indexer.indices {
if err := index.Update(Scope{SourceUID: uid}); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) TagUpdate(uid string) error {
log.Infof("Indexer - Index tag update event: %s", uid)
for _, index := range indexer.indices {
if err := index.Update(Scope{TagUID: uid}); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) PersonUpdate(uid string) error {
log.Infof("Indexer - Index person update event: %s", uid)
for _, index := range indexer.indices {
if err := index.Update(Scope{PersonUID: uid}); err != nil {
return err
}
}
return nil
}
func (indexer *Indexer) PublisherUpdate(uid string) error {
log.Infof("Indexer - Index publisher update event: %s", uid)
for _, index := range indexer.indices {
if err := index.Update(Scope{PublisherUID: uid}); err != nil {
return err
}
}
return nil
}