-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweeter_index.go
216 lines (193 loc) · 6.83 KB
/
tweeter_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
package es
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"sync/atomic"
"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/models"
"github.com/Bnei-Baruch/archive-backend/utils"
)
func MakeTweeterIndex(namespace string, indexDate string, db *sql.DB, esc *elastic.Client) *TweeterIndex {
ti := new(TweeterIndex)
ti.resultType = consts.ES_RESULT_TYPE_TWEETS
ti.baseName = consts.ES_RESULTS_INDEX
ti.namespace = namespace
ti.indexDate = indexDate
ti.db = db
ti.esc = esc
return ti
}
type TweeterIndex struct {
BaseIndex
Progress uint64
}
func (index *TweeterIndex) userIdToLanguageMapping() map[int]string {
return map[int]string{
1: consts.LANG_RUSSIAN,
2: consts.LANG_HEBREW,
3: consts.LANG_ENGLISH,
4: consts.LANG_SPANISH,
}
}
func (index *TweeterIndex) ReindexAll() error {
log.Info("TweeterIndex.Reindex All.")
_, indexErrors := index.RemoveFromIndexQuery(index.FilterByResultTypeQuery(index.resultType))
if err := indexErrors.CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "TweeterIndex"); err != nil {
return err
}
// SQL to always match any tweet
return indexErrors.Join(index.addToIndexSql("1=1"), "").CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "TweeterIndex")
}
func (index *TweeterIndex) RemoveFromIndex(scope Scope) (map[string][]string, error) {
log.Debugf("TweeterIndex.RemoveFromIndex - Scope: %+v.", scope)
removed, indexErrors := index.removeFromIndex(scope)
return removed, indexErrors.CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "TweeterIndex")
}
func (index *TweeterIndex) AddToIndex(scope Scope, removedUIDs []string) error {
log.Debugf("TweeterIndex.AddToIndex - Scope: %+v, removedUIDs: %+v.", scope, removedUIDs)
return index.addToIndex(scope, removedUIDs).CheckErrors(LANGUAGES_MAX_FAILURE, DOCUMENT_MAX_FAILIRE_RATIO, "TweeterIndex")
}
func (index *TweeterIndex) addToIndex(scope Scope, removedIDs []string) *IndexErrors {
ids := removedIDs
if scope.TweetTID != "" {
ids = append(ids, scope.TweetTID)
}
if len(ids) == 0 {
return MakeIndexErrors()
}
quoted := make([]string, len(ids))
for i, id := range ids {
quoted[i] = fmt.Sprintf("'%s'", id)
}
sqlScope := fmt.Sprintf("t.twitter_id IN (%s)", strings.Join(quoted, ","))
return index.addToIndexSql(sqlScope)
}
func (index *TweeterIndex) removeFromIndex(scope Scope) (map[string][]string, *IndexErrors) {
if scope.TweetTID != "" {
elasticScope := index.FilterByResultTypeQuery(index.resultType).
Filter(elastic.NewTermsQuery("mdb_uid", scope.TweetTID))
return index.RemoveFromIndexQuery(elasticScope)
}
// Nothing to remove.
return make(map[string][]string), MakeIndexErrors()
}
func (index *TweeterIndex) bulkIndexTweets(bulk OffsetLimitJob, sqlScope string) *IndexErrors {
var tweets []*mdbmodels.TwitterTweet
if err := mdbmodels.NewQuery(index.db,
qm.From("twitter_tweets as t"),
qm.Where(sqlScope),
qm.OrderBy("id"), // Required for same order results in each query
qm.Offset(bulk.Offset),
qm.Limit(bulk.Limit)).Bind(&tweets); err != nil {
return MakeIndexErrors().SetError(err).Wrap(fmt.Sprintf("bulkIndexTweets error at offset %d. error: %v", bulk.Offset, err))
}
log.Infof("Adding %d tweets (offset %d, total %d).", len(tweets), bulk.Offset, bulk.Total)
indexErrors := MakeIndexErrors()
for _, tweet := range tweets {
indexErrors.Join(index.indexTweet(tweet), "")
}
indexErrors.PrintIndexCounts(fmt.Sprintf("TweeterIndex %d - %d", bulk.Offset, bulk.Offset+bulk.Limit))
return indexErrors
}
func (index *TweeterIndex) addToIndexSql(sqlScope string) *IndexErrors {
var count int
if err := mdbmodels.NewQuery(index.db,
qm.Select("count(id)"),
qm.From("twitter_tweets as t"),
qm.Where(sqlScope)).QueryRow().Scan(&count); err != nil {
return MakeIndexErrors().SetError(err).Wrap(fmt.Sprintf("Failed TwitterIndex addToIndexSql: %s", sqlScope))
}
log.Debugf("Tweeter Index - Adding %d tweets. Scope: %s.", count, sqlScope)
limit := utils.MaxInt(10, utils.MinInt(1000, (int)(count/10)))
tasks := make(chan OffsetLimitJob, (count/limit + limit))
errors := make(chan *IndexErrors, 300)
doneAdding := make(chan bool, 1)
tasksCount := 0
go func() {
offset := 0
for offset < int(count) {
tasks <- OffsetLimitJob{offset, limit, count}
tasksCount++
offset += limit
}
close(tasks)
doneAdding <- true
}()
for w := 1; w <= 10; w++ {
go func(tasks <-chan OffsetLimitJob, errs chan<- *IndexErrors) {
for task := range tasks {
errors <- index.bulkIndexTweets(task, sqlScope)
}
}(tasks, errors)
}
<-doneAdding
indexErrors := MakeIndexErrors()
for a := 1; a <= tasksCount; a++ {
indexErrors.Join(<-errors, "")
}
return indexErrors
}
func (index *TweeterIndex) indexTweet(mdbTweet *mdbmodels.TwitterTweet) *IndexErrors {
langMapping := index.userIdToLanguageMapping()
tweetLang := langMapping[int(mdbTweet.UserID)]
indexErrors := MakeIndexErrors().ShouldIndex(tweetLang)
title := ""
if mdbTweet.Raw.Valid {
var raw interface{}
err := json.Unmarshal(mdbTweet.Raw.JSON, &raw)
indexErrors.DocumentError(tweetLang, err, fmt.Sprintf("Cannot unmarshal raw from tweet id %d", mdbTweet.ID))
if err != nil {
return indexErrors
}
r := raw.(map[string]interface{})
if val, ok := r["text"]; ok {
title = val.(string)
}
}
tweet := Result{
ResultType: index.resultType,
MDB_UID: mdbTweet.TwitterID, // TwitterID is taken instead of UID
TypedUids: []string{keyValue(consts.ES_UID_TYPE_TWEET, mdbTweet.TwitterID)},
FilterValues: []string{keyValue("content_type", consts.SCT_TWEET), keyValue(consts.FILTER_LANGUAGE, tweetLang)},
Title: title,
TitleSuggest: Suffixes(title),
EffectiveDate: &utils.Date{Time: mdbTweet.TweetAt},
Content: mdbTweet.FullText,
}
indexName := index.IndexName(tweetLang)
vBytes, err := json.Marshal(tweet)
indexErrors.DocumentError(tweetLang, err, fmt.Sprintf("Failed marshling tweet: %d", mdbTweet.ID))
if err != nil {
return indexErrors
}
log.Debugf("Tweets Index - Add tweet %s to index %s", string(vBytes), indexName)
resp, err := index.esc.Index().
Index(indexName).
Type("result").
BodyJson(tweet).
Do(context.TODO())
indexErrors.DocumentError(tweetLang, err, fmt.Sprintf("Index tweet %s %d", indexName, mdbTweet.ID))
if err != nil {
return indexErrors
}
errNotCreated := (error)(nil)
if resp.Result != "created" {
errNotCreated = errors.New(fmt.Sprintf("Not created: tweet %s %d", indexName, mdbTweet.ID))
} else {
indexErrors.Indexed(tweetLang)
}
indexErrors.DocumentError(tweetLang, errNotCreated, "TweeterIndex")
atomic.AddUint64(&index.Progress, 1)
progress := atomic.LoadUint64(&index.Progress)
if progress%10 == 0 {
log.Debugf("Progress tweet %d", progress)
}
return indexErrors
}