-
Notifications
You must be signed in to change notification settings - Fork 0
/
intent_engine.go
236 lines (220 loc) · 9.05 KB
/
intent_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
package search
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v6"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/es"
log "github.com/Sirupsen/logrus"
)
func (e *ESEngine) AddIntentSecondRound(h *elastic.SearchHit, intent Intent, query Query) (error, *Intent, *Query) {
var classificationIntent 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, size int, sortBy string) ([]Intent, error) {
intents := make([]Intent, 0)
if (len(query.Term) == 0 && len(query.ExactTerms) == 0) ||
sortBy == consts.SORT_BY_NEWER_TO_OLDER ||
sortBy == consts.SORT_BY_OLDER_TO_NEWER {
return intents, nil
}
for filterKey := range query.Filters {
if _, ok := consts.ES_INTENT_SUPPORTED_FILTERS[filterKey]; !ok {
return intents, nil
}
}
if contentTypes, ok := query.Filters[consts.FILTERS[consts.FILTER_UNITS_CONTENT_TYPES]]; ok {
for _, contentType := range contentTypes {
if _, ok := consts.ES_INTENT_SUPPORTED_CONTENT_TYPES[contentType]; !ok {
return intents, 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)
}
queryWithoutFilters := *query
queryWithoutFilters.Filters = make(map[string][]string)
for filterName, values := range query.Filters {
queryWithoutFilters.Filters[filterName] = values
}
// Keep only source and tag filters.
if _, ok := queryWithoutFilters.Filters[consts.FILTERS[consts.FILTER_UNITS_CONTENT_TYPES]]; ok {
delete(queryWithoutFilters.Filters, consts.FILTERS[consts.FILTER_UNITS_CONTENT_TYPES])
}
if _, ok := queryWithoutFilters.Filters[consts.FILTERS[consts.FILTER_COLLECTIONS_CONTENT_TYPES]]; ok {
delete(queryWithoutFilters.Filters, consts.FILTERS[consts.FILTER_COLLECTIONS_CONTENT_TYPES])
}
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: 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: 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 intents, 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 intents, 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 intents, 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: 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 intents, errors.New("ESEngine.AddIntents - Second Run Failed multi get (S).")
}
intentValue, intentOk := finalIntents[i].Value.(ClassificationIntent)
if !intentOk {
return intents, 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 ClassificationIntent
if err := json.Unmarshal(*h.Source, &classificationIntent); err != nil {
return intents, 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)
}
intents = append(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 intents {
for _, contentUnitType := range checkContentUnitsTypes {
if intentValue, ok := intents[intentIdx].Value.(ClassificationIntent); ok {
intentP := &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, intents[intentIdx])
intentP = &moreIntents[len(moreIntents)-1]
copyIntentValue := intentP.Value.(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
}
}
}
intents = append(intents, moreIntents...)
return intents, nil
}