-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
583 lines (533 loc) · 19.5 KB
/
engine.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package search
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"math"
"net/url"
"sort"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v6"
"github.com/Bnei-Baruch/archive-backend/cache"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/es"
"github.com/Bnei-Baruch/archive-backend/utils"
)
type ESEngine struct {
esc *elastic.Client
mdb *sql.DB
cache cache.CacheManager
ExecutionTimeLog map[string]time.Duration
}
type byRelevance []*elastic.SearchHit
type byNewerToOlder []*elastic.SearchHit
type byOlderToNewer []*elastic.SearchHit
type bySourceFirst []*elastic.SearchHit
func (s byRelevance) Len() int {
return len(s)
}
func (s byRelevance) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byRelevance) Less(i, j int) bool {
res, err := compareHits(s[i], s[j], consts.SORT_BY_RELEVANCE)
if err != nil {
panic(fmt.Sprintf("compareHits error: %s", err))
}
return res
}
func (s byNewerToOlder) Len() int {
return len(s)
}
func (s byNewerToOlder) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byNewerToOlder) Less(i, j int) bool {
res, err := compareHits(s[i], s[j], consts.SORT_BY_NEWER_TO_OLDER)
if err != nil {
panic(fmt.Sprintf("compareHits error: %s", err))
}
return res
}
func (s byOlderToNewer) Len() int {
return len(s)
}
func (s byOlderToNewer) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byOlderToNewer) Less(i, j int) bool {
res, err := compareHits(s[i], s[j], consts.SORT_BY_OLDER_TO_NEWER)
if err != nil {
panic(fmt.Sprintf("compareHits error: %s", err))
}
return res
}
func (s bySourceFirst) Len() int {
return len(s)
}
func (s bySourceFirst) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s bySourceFirst) Less(i, j int) bool {
res, err := compareHits(s[i], s[j], consts.SORT_BY_SOURCE_FIRST)
if err != nil {
panic(fmt.Sprintf("compareHits error: %s", err))
}
return res
}
// TODO: All interactions with ES should be throttled to prevent downstream pressure
func NewESEngine(esc *elastic.Client, db *sql.DB, cache cache.CacheManager) *ESEngine {
return &ESEngine{esc: esc, mdb: db, cache: cache, ExecutionTimeLog: make(map[string]time.Duration)}
}
func SuggestionHasOptions(ss elastic.SearchSuggest) bool {
for _, v := range ss {
for _, s := range v {
if len(s.Options) > 0 {
return true
}
}
}
return false
}
func (e *ESEngine) GetSuggestions(ctx context.Context, query Query, preference string) (interface{}, error) {
e.timeTrack(time.Now(), "GetSuggestions")
multiSearchService := e.esc.MultiSearch()
requests := NewResultsSuggestRequests([]string{consts.ES_RESULT_TYPE_TAGS, consts.ES_RESULT_TYPE_SOURCES}, query, preference)
multiSearchService.Add(requests...)
// Actual call to elastic
beforeMssDo := time.Now()
mr, err := multiSearchService.Do(ctx)
e.timeTrack(beforeMssDo, "GetSuggestions.MultisearchDo")
if err != nil {
// don't kill entire request if ctx was cancelled
if ue, ok := err.(*url.Error); ok {
if ue.Err == context.DeadlineExceeded || ue.Err == context.Canceled {
if ue.Err == context.DeadlineExceeded {
log.Warn("ESEngine.GetSuggestions - ctx cancelled - deadline.")
}
if ue.Err == context.Canceled {
log.Warn("ESEngine.GetSuggestions - ctx cancelled - canceled.")
}
return nil, nil
}
}
return nil, errors.Wrap(err, "ESEngine.GetSuggestions")
}
// Process response
sRes := (*elastic.SearchResult)(nil)
for _, r := range mr.Responses {
if r != nil && SuggestionHasOptions(r.Suggest) {
sRes = r
break
}
}
if sRes == nil && len(mr.Responses) > 0 {
sRes = mr.Responses[0]
}
return sRes, nil
}
func (e *ESEngine) AddIntentSecondRound(h *elastic.SearchHit, intent Intent, query Query) (error, *Intent, *Query) {
var classificationIntent es.ClassificationIntent
if err := json.Unmarshal(*h.Source, &classificationIntent); err != nil {
return err, nil, nil
}
if query.Deb {
classificationIntent.Explanation = *h.Explanation
}
// log.Infof("Hit: %+v %+v", *h.Score, classificationIntent)
if h.Score != nil && *h.Score > 0 {
classificationIntent.Score = h.Score
// Search for specific classification by full name to evaluate max score.
query.Term = ""
query.ExactTerms = []string{classificationIntent.Title}
intent.Value = classificationIntent
// log.Infof("Potential intent: %s", classificationIntent.Title)
return nil, &intent, &query
}
return nil, nil, nil
}
func (e *ESEngine) AddIntents(query *Query, preference string) error {
if len(query.Term) == 0 && len(query.ExactTerms) == 0 {
return nil
}
// Don't do intents, if sources are selected in section filter or filtering the results by media language.
for filterKey := range query.Filters {
if filterKey == consts.FILTERS[consts.FILTER_SECTION_SOURCES] || filterKey == consts.FILTERS[consts.FILTER_LANGUAGE] {
return nil
}
}
defer e.timeTrack(time.Now(), "DoSearch.AddIntents")
checkContentUnitsTypes := []string{}
if values, ok := query.Filters[consts.FILTERS[consts.FILTER_UNITS_CONTENT_TYPES]]; ok {
for _, value := range values {
if value == consts.CT_LESSON_PART {
checkContentUnitsTypes = append(checkContentUnitsTypes, consts.CT_LESSON_PART)
}
if value == consts.CT_VIDEO_PROGRAM_CHAPTER {
checkContentUnitsTypes = append(checkContentUnitsTypes, consts.CT_VIDEO_PROGRAM_CHAPTER)
}
}
} else {
checkContentUnitsTypes = append(checkContentUnitsTypes, consts.CT_LESSON_PART, consts.CT_VIDEO_PROGRAM_CHAPTER)
}
// Clear filters. We don't want filters on Intents. Filters should be applied to final hits.
queryWithoutFilters := *query
queryWithoutFilters.Filters = map[string][]string{}
mssFirstRound := e.esc.MultiSearch()
potentialIntents := make([]Intent, 0)
for _, language := range query.LanguageOrder {
// Order here provides the priority in results, i.e., tags are more importnt then sources.
index := es.IndexAliasName("prod", consts.ES_RESULTS_INDEX, language)
mssFirstRound.Add(NewResultsSearchRequest(
SearchRequestOptions{
resultTypes: []string{consts.ES_RESULT_TYPE_TAGS},
index: index,
query: queryWithoutFilters,
sortBy: consts.SORT_BY_RELEVANCE,
from: 0,
size: consts.API_DEFAULT_PAGE_SIZE,
preference: preference,
useHighlight: false,
partialHighlight: true}))
potentialIntents = append(potentialIntents, Intent{consts.INTENT_TYPE_TAG, language, nil})
mssFirstRound.Add(NewResultsSearchRequest(
SearchRequestOptions{
resultTypes: []string{consts.ES_RESULT_TYPE_SOURCES},
index: index,
query: queryWithoutFilters,
sortBy: consts.SORT_BY_RELEVANCE,
from: 0,
size: consts.API_DEFAULT_PAGE_SIZE,
preference: preference,
useHighlight: false,
partialHighlight: true}))
potentialIntents = append(potentialIntents, Intent{consts.INTENT_TYPE_SOURCE, language, nil})
}
beforeFirstRoundDo := time.Now()
mr, err := mssFirstRound.Do(context.TODO())
e.timeTrack(beforeFirstRoundDo, "DoSearch.AddIntents.FirstRoundDo")
if err != nil {
return errors.Wrap(err, "ESEngine.AddIntents - Error multisearch Do.")
}
// Build second request to evaluate how close the search is toward the full name.
mssSecondRound := e.esc.MultiSearch()
finalIntents := make([]Intent, 0)
for i := 0; i < len(potentialIntents); i++ {
res := mr.Responses[i]
if res.Error != nil {
log.Warnf("ESEngine.AddIntents - First Run %+v", res.Error)
return errors.New("ESEngine.AddIntents - First Run Failed multi get (S).")
}
if haveHits(res) {
for _, h := range res.Hits.Hits {
err, intent, secondRoundQuery := e.AddIntentSecondRound(h, potentialIntents[i], queryWithoutFilters)
// log.Infof("Adding second round for %+v %+v %+v", intent, secondRoundQuery, potentialIntents[i])
if err != nil {
return errors.Wrapf(err, "ESEngine.AddIntents - Error second run for intent %+v", potentialIntents[i])
}
if intent != nil {
mssSecondRound.Add(NewResultsSearchRequest(
SearchRequestOptions{
resultTypes: []string{consts.RESULT_TYPE_BY_INDEX_TYPE[potentialIntents[i].Type]},
index: es.IndexAliasName("prod", consts.ES_RESULTS_INDEX, intent.Language),
query: *secondRoundQuery,
sortBy: consts.SORT_BY_RELEVANCE,
from: 0,
size: consts.API_DEFAULT_PAGE_SIZE,
preference: preference,
useHighlight: false,
partialHighlight: true}))
finalIntents = append(finalIntents, *intent)
}
}
}
}
beforeSecondRoundDo := time.Now()
mr, err = mssSecondRound.Do(context.TODO())
e.timeTrack(beforeSecondRoundDo, "DoSearch.AddIntents.SecondRoundDo")
for i := 0; i < len(finalIntents); i++ {
res := mr.Responses[i]
if res.Error != nil {
log.Warnf("ESEngine.AddIntents - Second Run %+v", res.Error)
log.Warnf("ESEngine.AddIntents - Second Run %+v", res.Error.RootCause[0])
return errors.New("ESEngine.AddIntents - Second Run Failed multi get (S).")
}
intentValue, intentOk := finalIntents[i].Value.(es.ClassificationIntent)
if !intentOk {
return errors.New(fmt.Sprintf("ESEngine.AddIntents - Unexpected intent value: %+v", finalIntents[i].Value))
}
if haveHits(res) {
// log.Infof("Found Hits for %+v", intentValue)
found := false
for _, h := range res.Hits.Hits {
var classificationIntent es.ClassificationIntent
if err := json.Unmarshal(*h.Source, &classificationIntent); err != nil {
return errors.Wrap(err, "ESEngine.AddIntents - Unmarshal classification intent filed.")
}
if query.Deb {
intentValue.MaxExplanation = *h.Explanation
}
log.Infof("%s: %+v", classificationIntent.Title, *h.Score)
if intentValue.MDB_UID == classificationIntent.MDB_UID {
found = true
// log.Infof("Max Score: %+v", *h.Score)
if h.Score != nil && *h.Score > 0 {
intentValue.MaxScore = h.Score
if *intentValue.MaxScore < *intentValue.Score {
log.Warnf("ESEngine.AddIntents - Not expected score %f to be larger then max score %f for %s - %s.",
*intentValue.Score, *intentValue.MaxScore, intentValue.MDB_UID, intentValue.Title)
}
query.Intents = append(query.Intents, Intent{finalIntents[i].Type, finalIntents[i].Language, intentValue})
}
}
}
if !found {
log.Warnf("ESEngine.AddIntents - Did not find matching second run: %s - %s.",
intentValue.MDB_UID, intentValue.Title)
}
}
}
// Set content unit type and exists for intents that are in the query, i.e., those who passed the second round.
// If more then one content unit type exist for this intent, we will have to duplicate that intent.
moreIntents := make([]Intent, 0)
for intentIdx := range query.Intents {
for _, contentUnitType := range checkContentUnitsTypes {
if intentValue, ok := query.Intents[intentIdx].Value.(es.ClassificationIntent); ok {
intentP := &query.Intents[intentIdx]
intentValueP := &intentValue
if intentValue.ContentType != "" {
// We need to copy the intent as we have more than one existing content types for that intent.
moreIntents = append(moreIntents, query.Intents[intentIdx])
intentP = &moreIntents[len(moreIntents)-1]
copyIntentValue := intentP.Value.(es.ClassificationIntent)
intentValueP = ©IntentValue
}
intentValueP.ContentType = contentUnitType
if intentP.Type == consts.INTENT_TYPE_TAG {
intentValueP.Exist = e.cache.SearchStats().IsTagWithUnits(intentValueP.MDB_UID, contentUnitType)
} else if intentP.Type == consts.INTENT_TYPE_SOURCE {
intentValueP.Exist = e.cache.SearchStats().IsSourceWithUnits(intentValueP.MDB_UID, contentUnitType)
}
// Assign the changed intent value, as everything is by value in golang.
intentP.Value = *intentValueP
}
}
}
query.Intents = append(query.Intents, moreIntents...)
return nil
}
func (e *ESEngine) IntentsToResults(query *Query) (error, map[string]*elastic.SearchResult) {
srMap := make(map[string]*elastic.SearchResult)
for _, lang := range query.LanguageOrder {
sh := &elastic.SearchHits{TotalHits: 0}
sr := &elastic.SearchResult{Hits: sh}
srMap[lang] = sr
}
// log.Infof("IntentsToResults - %d intents.", len(query.Intents))
for _, intent := range query.Intents {
// Convert intent to result with score.
intentValue := intent.Value.(es.ClassificationIntent)
boostedScore := float64(0.0)
if intentValue.Exist {
sh := srMap[intent.Language].Hits
sh.TotalHits++
// Boost up to 33% for exact match, i.e., for score / max score of 1.0.
boostedScore = *intentValue.Score * (3.0 + *intentValue.Score / *intentValue.MaxScore) / 3.0
if sh.MaxScore != nil {
maxScore := math.Max(*sh.MaxScore, boostedScore)
sh.MaxScore = &maxScore
} else {
sh.MaxScore = &boostedScore
}
intentHit := &elastic.SearchHit{}
intentHit.Explanation = &intentValue.Explanation
intentHit.Score = &boostedScore
intentHit.Index = consts.INTENT_INDEX_BY_TYPE[intent.Type]
intentHit.Type = consts.INTENT_HIT_TYPE_BY_CT[intentValue.ContentType]
source, err := json.Marshal(intentValue)
if err != nil {
return err, nil
}
intentHit.Source = (*json.RawMessage)(&source)
sh.Hits = append(sh.Hits, intentHit)
}
// log.Infof("Added intent %s %s %s boost score:%f exist:%t", intentValue.Title, intent.Type, intent.Language, boostedScore, intentValue.Exist)
}
return nil, srMap
}
func haveHits(r *elastic.SearchResult) bool {
return r != nil && r.Hits != nil && r.Hits.Hits != nil && len(r.Hits.Hits) > 0
}
func score(score *float64) float64 {
if score == nil {
return 0
} else {
return *score
}
}
func compareHits(h1 *elastic.SearchHit, h2 *elastic.SearchHit, sortBy string) (bool, error) {
if sortBy == consts.SORT_BY_RELEVANCE {
return score(h1.Score) > score(h2.Score), nil
} else if sortBy == consts.SORT_BY_SOURCE_FIRST {
var rt1, rt2 es.ResultType
if err := json.Unmarshal(*h1.Source, &rt1); err != nil {
return false, err
}
if err := json.Unmarshal(*h2.Source, &rt2); err != nil {
return false, err
}
// Order by sources first, then be score.
return rt1.ResultType == consts.ES_RESULT_TYPE_SOURCES && rt2.ResultType != consts.ES_RESULT_TYPE_SOURCES || score(h1.Score) > score(h2.Score), nil
} else {
var ed1, ed2 es.EffectiveDate
if err := json.Unmarshal(*h1.Source, &ed1); err != nil {
return false, err
}
if err := json.Unmarshal(*h2.Source, &ed2); err != nil {
return false, err
}
if ed1.EffectiveDate == nil {
ed1.EffectiveDate = &utils.Date{time.Time{}}
}
if ed2.EffectiveDate == nil {
ed2.EffectiveDate = &utils.Date{time.Time{}}
}
if sortBy == consts.SORT_BY_OLDER_TO_NEWER {
// Oder by older to newer, break ties using score.
return ed2.EffectiveDate.Time.After(ed1.EffectiveDate.Time) ||
ed2.EffectiveDate.Time.Equal(ed1.EffectiveDate.Time) && score(h1.Score) > score(h2.Score), nil
} else {
log.Infof("%+v %+v %+v %+v", ed1, ed2, h1, h2)
// Order by newer to older, break ties using score.
return ed2.EffectiveDate.Time.Before(ed1.EffectiveDate.Time) ||
ed2.EffectiveDate.Time.Equal(ed1.EffectiveDate.Time) && score(h1.Score) > score(h2.Score), nil
}
}
}
func joinResponses(sortBy string, from int, size int, results ...*elastic.SearchResult) (*elastic.SearchResult, error) {
if len(results) == 0 {
return nil, nil
}
// Concatenate all result hits to single slice.
concatenated := make([]*elastic.SearchHit, 0)
for _, result := range results {
concatenated = append(concatenated, result.Hits.Hits...)
}
// Apply sorting.
if sortBy == consts.SORT_BY_RELEVANCE {
sort.Stable(byRelevance(concatenated))
} else if sortBy == consts.SORT_BY_OLDER_TO_NEWER {
sort.Stable(byOlderToNewer(concatenated))
} else if sortBy == consts.SORT_BY_NEWER_TO_OLDER {
sort.Stable(byNewerToOlder(concatenated))
} else if sortBy == consts.SORT_BY_SOURCE_FIRST {
sort.Stable(bySourceFirst(concatenated))
}
// Filter by relevant page.
concatenated = concatenated[from:utils.Min(from+size, len(concatenated))]
// Take arbitrary result to use as base and set it's hits.
// TODO: Rewrite this to be cleaner.
result := results[0]
// Get hits count and max score
totalHits := int64(0)
var maxScore float64
if result.Hits.MaxScore != nil {
maxScore = *result.Hits.MaxScore
} else {
maxScore = 0
}
for _, result := range results {
totalHits += result.Hits.TotalHits
if sortBy == consts.SORT_BY_RELEVANCE {
if result.Hits.MaxScore != nil {
maxScore = math.Max(maxScore, *result.Hits.MaxScore)
}
}
}
result.Hits.Hits = concatenated
result.Hits.TotalHits = totalHits
result.Hits.MaxScore = &maxScore
return result, nil
}
func (e *ESEngine) timeTrack(start time.Time, operation string) {
elapsed := time.Since(start)
e.ExecutionTimeLog[operation] = elapsed
}
func (e *ESEngine) DoSearch(ctx context.Context, query Query, sortBy string, from int, size int, preference string) (*QueryResult, error) {
defer e.timeTrack(time.Now(), "DoSearch")
if err := e.AddIntents(&query, preference); err != nil {
return nil, errors.Wrap(err, "ESEngine.DoSearch - Error adding intents.")
}
multiSearchService := e.esc.MultiSearch()
multiSearchService.Add(NewResultsSearchRequests(
SearchRequestOptions{
resultTypes: consts.ES_SEARCH_RESULT_TYPES,
index: "",
query: query,
sortBy: sortBy,
from: 0,
size: from + size,
preference: preference,
useHighlight: true,
partialHighlight: false})...)
// Do search.
beforeDoSearch := time.Now()
mr, err := multiSearchService.Do(context.TODO())
e.timeTrack(beforeDoSearch, "DoSearch.MultisearchDo")
if err != nil {
return nil, errors.Wrap(err, "ESEngine.DoSearch - Error multisearch Do.")
}
if len(mr.Responses) != len(query.LanguageOrder) {
return nil, errors.New(fmt.Sprintf("Unexpected number of results %d, expected %d",
len(mr.Responses), len(query.LanguageOrder)))
}
resultsByLang := make(map[string][]*elastic.SearchResult)
// Responses are ordered by language by index, i.e., for languages [bg, ru, en].
// We want the first matching language that has at least any result.
for i, currentResults := range mr.Responses {
if currentResults.Error != nil {
log.Warnf("%+v", currentResults.Error)
return nil, errors.New(fmt.Sprintf("Failed multi get: %+v", currentResults.Error))
}
if haveHits(currentResults) {
lang := query.LanguageOrder[i]
if _, ok := resultsByLang[lang]; !ok {
resultsByLang[lang] = make([]*elastic.SearchResult, 0)
}
resultsByLang[lang] = append(resultsByLang[lang], currentResults)
}
}
err, intentResultsMap := e.IntentsToResults(&query)
if err != nil {
return nil, errors.Wrap(err, "ESEngine.DoSearch - Error adding intents to results.")
}
for lang, intentResults := range intentResultsMap {
if haveHits(intentResults) {
if _, ok := resultsByLang[lang]; !ok {
resultsByLang[lang] = make([]*elastic.SearchResult, 0)
}
resultsByLang[lang] = append(resultsByLang[lang], intentResults)
}
}
results := make([]*elastic.SearchResult, 0)
for _, lang := range query.LanguageOrder {
if r, ok := resultsByLang[lang]; ok {
results = r
break
}
}
ret, err := joinResponses(sortBy, from, size, results...)
if ret != nil && ret.Hits != nil {
return &QueryResult{ret, query.Intents}, err
}
if len(mr.Responses) > 0 {
// This happens when there are no responses with hits.
// Note, we don't filter here intents by language.
return &QueryResult{mr.Responses[0], query.Intents}, err
} else {
return nil, errors.Wrap(err, "ESEngine.DoSearch - No responses from multi search.")
}
}