-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_units_index.go
359 lines (326 loc) · 13 KB
/
content_units_index.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
package es
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/Bnei-Baruch/sqlboiler/queries/qm"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v6"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/mdb"
mdbmodels "github.com/Bnei-Baruch/archive-backend/mdb/models"
"github.com/Bnei-Baruch/archive-backend/utils"
)
func MakeContentUnitsIndex(namespace string, indexDate string, db *sql.DB, esc *elastic.Client) *ContentUnitsIndex {
cui := new(ContentUnitsIndex)
cui.resultType = consts.ES_RESULT_TYPE_UNITS
cui.baseName = consts.ES_RESULTS_INDEX
cui.namespace = namespace
cui.indexDate = indexDate
cui.db = db
cui.esc = esc
return cui
}
type ContentUnitsIndex struct {
BaseIndex
Progress uint64
}
func defaultContentUnit(cu *mdbmodels.ContentUnit) bool {
return cu.Secure == 0 && cu.Published && !utils.Int64InSlice(cu.TypeID, []int64{
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_LELO_MIKUD].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_PUBLICATION].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_SONG].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_BOOK].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_BLOG_POST].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_KITEI_MAKOR].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_RESEARCH_MATERIAL].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_KTAIM_NIVCHARIM].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_UNKNOWN].ID,
})
}
func defaultContentUnitSql() string {
return fmt.Sprintf("cu.secure = 0 AND cu.published IS TRUE AND cu.type_id NOT IN (%d, %d, %d, %d, %d, %d, %d, %d, %d)",
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_LELO_MIKUD].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_PUBLICATION].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_SONG].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_BOOK].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_BLOG_POST].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_KITEI_MAKOR].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_RESEARCH_MATERIAL].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_KTAIM_NIVCHARIM].ID,
mdb.CONTENT_TYPE_REGISTRY.ByName[consts.CT_UNKNOWN].ID,
)
}
func (index *ContentUnitsIndex) ReindexAll() error {
log.Info("Content Units Index - Reindex all.")
_, indexErrors := index.RemoveFromIndexQuery(index.FilterByResultTypeQuery(index.resultType))
if err := indexErrors.CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "ContentUnitsIndex"); err != nil {
return err
}
return indexErrors.Join(index.addToIndexSql(defaultContentUnitSql()), "").CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "ContentUnitsIndex")
}
func (index *ContentUnitsIndex) RemoveFromIndex(scope Scope) (map[string][]string, error) {
log.Debugf("Content Units Index - RemoveFromIndex. Scope: %+v.", scope)
removed, indexErrors := index.removeFromIndex(scope)
return removed, indexErrors.CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "ContentUnitsIndex")
}
func (index *ContentUnitsIndex) AddToIndex(scope Scope, removedUIDs []string) error {
log.Debugf("ContentUnitsIndex - AddToIndex. Scope: %+v, removedUIDs: %+v", scope, removedUIDs)
return index.addToIndex(scope, removedUIDs).CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "ContentUnitsIndex")
}
func (index *ContentUnitsIndex) addToIndex(scope Scope, removedUIDs []string) *IndexErrors {
// TODO: Missing tag scope handling.
sqlScope := defaultContentUnitSql()
uids := removedUIDs
if scope.ContentUnitUID != "" {
uids = append(uids, scope.ContentUnitUID)
}
indexErrors := MakeIndexErrors()
if scope.FileUID != "" {
moreUIDs, err := contentUnitsScopeByFile(index.db, scope.FileUID)
indexErrors.SetError(err)
uids = append(uids, moreUIDs...)
}
if scope.CollectionUID != "" {
moreUIDs, err := contentUnitsScopeByCollection(index.db, scope.CollectionUID)
indexErrors.SetError(err)
uids = append(uids, moreUIDs...)
}
if scope.SourceUID != "" {
moreUIDs, err := contentUnitsScopeBySource(index.db, scope.SourceUID)
indexErrors.SetError(err)
uids = append(uids, moreUIDs...)
}
if len(uids) == 0 {
return indexErrors
}
quoted := make([]string, len(uids))
for i, uid := range uids {
quoted[i] = fmt.Sprintf("'%s'", uid)
}
sqlScope = fmt.Sprintf("%s AND cu.uid IN (%s)", sqlScope, strings.Join(quoted, ","))
return indexErrors.Join(index.addToIndexSql(sqlScope), fmt.Sprintf("Failed adding to index: %+v", sqlScope))
}
func (index *ContentUnitsIndex) removeFromIndex(scope Scope) (map[string][]string, *IndexErrors) {
typedUids := make([]string, 0)
if scope.ContentUnitUID != "" {
typedUids = append(typedUids, KeyValue(consts.ES_UID_TYPE_CONTENT_UNIT, scope.ContentUnitUID))
}
indexErrors := MakeIndexErrors()
if scope.FileUID != "" {
typedUids = append(typedUids, KeyValue(consts.ES_UID_TYPE_FILE, scope.FileUID))
moreUIDs, err := contentUnitsScopeByFile(index.db, scope.FileUID)
indexErrors.SetError(err)
typedUids = append(typedUids, KeyValues(consts.ES_UID_TYPE_CONTENT_UNIT, moreUIDs)...)
}
if scope.CollectionUID != "" {
typedUids = append(typedUids, KeyValue(consts.ES_UID_TYPE_COLLECTION, scope.CollectionUID))
moreUIDs, err := contentUnitsScopeByCollection(index.db, scope.CollectionUID)
indexErrors.SetError(err)
typedUids = append(typedUids, KeyValues(consts.ES_UID_TYPE_CONTENT_UNIT, moreUIDs)...)
}
if scope.TagUID != "" {
typedUids = append(typedUids, KeyValue(consts.ES_UID_TYPE_TAG, scope.TagUID))
}
if scope.SourceUID != "" {
typedUids = append(typedUids, KeyValue(consts.ES_UID_TYPE_SOURCE, scope.SourceUID))
moreUIDs, err := contentUnitsScopeBySource(index.db, scope.SourceUID)
indexErrors.SetError(err)
typedUids = append(typedUids, KeyValues(consts.ES_UID_TYPE_CONTENT_UNIT, moreUIDs)...)
}
if len(typedUids) > 0 {
typedUidsI := make([]interface{}, len(typedUids))
for i, typedUID := range typedUids {
typedUidsI[i] = typedUID
}
elasticScope := index.FilterByResultTypeQuery(index.resultType).
Filter(elastic.NewTermsQuery("typed_uids", typedUidsI...))
uids, removeIndexErrors := index.RemoveFromIndexQuery(elasticScope)
return uids, indexErrors.Join(removeIndexErrors, fmt.Sprintf("Failed removing from index: %+v", elasticScope))
} else {
// Nothing to remove, or error.
return make(map[string][]string), indexErrors
}
}
func (index *ContentUnitsIndex) bulkIndexUnits(bulk OffsetLimitJob, sqlScope string) *IndexErrors {
indexErrors := MakeIndexErrors()
var units []*mdbmodels.ContentUnit
if err := mdbmodels.NewQuery(index.db,
qm.From("content_units as cu"),
qm.Load("ContentUnitI18ns"),
qm.Load("CollectionsContentUnits"),
qm.Load("CollectionsContentUnits.Collection"),
qm.Where(sqlScope),
qm.Offset(bulk.Offset),
qm.Limit(bulk.Limit)).Bind(&units); err != nil {
return indexErrors.SetError(errors.Wrap(err, "Fetch units from mdb"))
}
log.Infof("Content Units Index - Adding %d units (offset: %d total: %d).", len(units), bulk.Offset, bulk.Total)
indexData, err := MakeIndexData(index.db, sqlScope)
if err != nil {
return indexErrors.SetError(errors.Wrap(err, "Failed making index data."))
}
i18nMap := make(map[string][]Result)
for _, unit := range units {
i18nUnit, unitIndexErrors := index.prepareIndexUnit(unit, indexData)
for lang, result := range i18nUnit {
i18nMap[lang] = append(i18nMap[lang], result)
}
indexErrors.Join(unitIndexErrors, "")
}
// Index each document in its language index
for lang, results := range i18nMap {
indexName := index.IndexName(lang)
bulkService := elastic.NewBulkService(index.esc).Index(indexName)
for _, result := range results {
indexErrors.ShouldIndex(lang)
bulkService.Add(elastic.NewBulkIndexRequest().Index(indexName).Type("result").Doc(result))
}
bulkRes, e := bulkService.Do(context.TODO())
if e != nil {
indexErrors.LanguageError(lang, e, fmt.Sprintf("Results Index - bulkIndexUnits %s %+v.", indexName, sqlScope))
continue
}
for _, itemMap := range bulkRes.Items {
for _, res := range itemMap {
if res.Result == "created" {
indexErrors.Indexed(lang)
}
}
}
}
indexErrors.PrintIndexCounts(fmt.Sprintf("ContentUnitIndex %d - %d / %d", bulk.Offset, bulk.Offset+bulk.Limit, bulk.Total))
return indexErrors
}
type OffsetLimitJob struct {
Offset int
Limit int
Total int
}
func (index *ContentUnitsIndex) addToIndexSql(sqlScope string) *IndexErrors {
indexErrors := MakeIndexErrors()
var count int
err := mdbmodels.NewQuery(index.db,
qm.Select("COUNT(1)"),
qm.From("content_units as cu"),
qm.Where(sqlScope)).QueryRow().Scan(&count)
if err != nil {
return indexErrors.SetError(errors.Wrapf(err, "Failed fetching content_units with sql scope: %s", sqlScope))
}
log.Debugf("Content Units Index - Adding %d units. Scope: %s", count, sqlScope)
tasks := make(chan OffsetLimitJob, 300)
errors := make(chan *IndexErrors, 300)
doneAdding := make(chan bool)
tasksCount := 0
go func() {
offset := 0
limit := utils.MaxInt(10, utils.MinInt(250, (int)(count/10)))
for offset < count {
tasks <- OffsetLimitJob{offset, limit, count}
tasksCount += 1
offset += limit
}
close(tasks)
doneAdding <- true
}()
for w := 1; w <= 10; w++ {
go func(tasks <-chan OffsetLimitJob, errors chan<- *IndexErrors) {
for task := range tasks {
errors <- index.bulkIndexUnits(task, sqlScope)
}
}(tasks, errors)
}
<-doneAdding
for a := 1; a <= tasksCount; a++ {
indexErrors.Join(<-errors, "")
}
return indexErrors
}
func collectionsContentTypes(collectionsContentUnits mdbmodels.CollectionsContentUnitSlice) []string {
ret := make([]string, len(collectionsContentUnits))
for i, ccu := range collectionsContentUnits {
ret[i] = mdb.CONTENT_TYPE_REGISTRY.ByID[ccu.R.Collection.TypeID].Name
}
return ret
}
func collectionsTypedUids(collectionsContentUnits mdbmodels.CollectionsContentUnitSlice) []string {
ret := make([]string, len(collectionsContentUnits))
for i, ccu := range collectionsContentUnits {
ret[i] = KeyValue(consts.ES_UID_TYPE_COLLECTION, ccu.R.Collection.UID)
}
return ret
}
func (index *ContentUnitsIndex) prepareIndexUnit(cu *mdbmodels.ContentUnit, indexData *IndexData) (map[string]Result, *IndexErrors) {
indexErrors := MakeIndexErrors()
// Create documents in each language with available translation
i18nMap := make(map[string]Result)
for _, i18n := range cu.R.ContentUnitI18ns {
if i18n.Name.Valid && strings.TrimSpace(i18n.Name.String) != "" {
typedUids := append([]string{KeyValue(consts.ES_UID_TYPE_CONTENT_UNIT, cu.UID)},
collectionsTypedUids(cu.R.CollectionsContentUnits)...)
filterValues := append([]string{KeyValue("content_type", mdb.CONTENT_TYPE_REGISTRY.ByID[cu.TypeID].Name)},
KeyValues("collections_content_type", collectionsContentTypes(cu.R.CollectionsContentUnits))...)
unit := Result{
ResultType: index.resultType,
IndexDate: &utils.Date{Time: time.Now()},
MDB_UID: cu.UID,
TypedUids: typedUids,
FilterValues: filterValues,
Title: i18n.Name.String,
}
if i18n.Description.Valid && i18n.Description.String != "" {
unit.Description = i18n.Description.String
}
if cu.Properties.Valid {
var props map[string]interface{}
err := json.Unmarshal(cu.Properties.JSON, &props)
indexErrors.DocumentError(i18n.Language, err, fmt.Sprintf("json.Unmarshal properties %s", cu.UID))
if err != nil {
continue
}
if filmDate, ok := props["film_date"]; ok {
val, err := time.Parse("2006-01-02", filmDate.(string))
indexErrors.DocumentError(i18n.Language, err, fmt.Sprintf("time.Parse film_date %s", cu.UID))
if err != nil {
continue
}
unit.EffectiveDate = &utils.Date{Time: val}
}
}
if val, ok := indexData.Sources[cu.UID]; ok {
unit.FilterValues = append(unit.FilterValues, KeyValues(consts.ES_UID_TYPE_SOURCE, val)...)
unit.TypedUids = append(unit.TypedUids, KeyValues(consts.ES_UID_TYPE_SOURCE, val)...)
// We dont add TitleSuggest to CU with source
unit.TitleSuggest = SuggestField{[]string{}, float64(0)}
} else {
unit.TitleSuggest = SuggestField{Suffixes(i18n.Name.String), float64(1)}
}
if val, ok := indexData.Tags[cu.UID]; ok {
unit.FilterValues = append(unit.FilterValues, KeyValues(consts.ES_UID_TYPE_TAG, val)...)
unit.TypedUids = append(unit.TypedUids, KeyValues(consts.ES_UID_TYPE_TAG, val)...)
}
if val, ok := indexData.MediaLanguages[cu.UID]; ok {
unit.FilterValues = append(unit.FilterValues, KeyValues(consts.FILTER_LANGUAGE, val)...)
}
if byLang, ok := indexData.Transcripts[cu.UID]; ok {
if val, ok := byLang[i18n.Language]; ok {
var err error
unit.Content, err = DocText(val[0])
if unit.Content == "" {
log.Warnf("Content Units Index - Transcript empty: %s", val[0])
}
indexErrors.DocumentError(i18n.Language, err, fmt.Sprintf("Content Units Index - Error parsing docx: %s", val[0]))
if err == nil {
unit.TypedUids = append(unit.TypedUids, KeyValue(consts.ES_UID_TYPE_FILE, val[0]))
}
}
}
i18nMap[i18n.Language] = unit
}
}
return i18nMap, indexErrors
}