-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.go
204 lines (159 loc) · 4.72 KB
/
search.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
package main
import (
"database/sql"
"encoding/json"
"github.com/lib/pq"
"github.com/meilisearch/meilisearch-go"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strings"
)
func (app *Application) SetupSearch() {
index := os.Getenv("MEILISEARCH_INDEX")
if strings.ToLower(os.Getenv("MEILISEARCH_ENABLED")) != "true" {
log.Info("meilisearch integration is disabled")
return
} else {
log.Info("meilisearch integration is enabled")
}
app.searchClient = meilisearch.NewClient(meilisearch.ClientConfig{
Host: os.Getenv("MEILISEARCH_URL"),
APIKey: os.Getenv("MEILISEARCH_BACKEND_API_KEY"),
})
if _, err := app.searchClient.GetIndex(index); err != nil {
// index does not exist, create it
_, err := app.searchClient.CreateIndex(&meilisearch.IndexConfig{
Uid: index,
PrimaryKey: "id",
})
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to create meilisearch index")
return
}
}
app.search = app.searchClient.Index(index)
// upload existing documents to the search instance
videos, err := AllVideos(app.db)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to retrieve newest version of archived videos for the search engine")
return
}
bytes, err := json.Marshal(videos)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to marshal videos into bytes")
return
}
var flattenedVideos []map[string]any
if err := json.Unmarshal(bytes, &flattenedVideos); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to unmarshal bytes into videos")
return
}
info, err := app.search.AddDocuments(&flattenedVideos)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to upload newest version of archived videos to search engine")
return
}
distinctTaskInfo, _ := app.search.UpdateDistinctAttribute("id")
filterableTaskInfo, _ := app.search.UpdateFilterableAttributes(&[]string{
"id",
"submitters",
"scheduledStart",
"finished",
"title",
"channelName",
"channelId",
"fileSizeBytes",
"length",
})
sortableTaskInfo, _ := app.search.UpdateSortableAttributes(&[]string{
"scheduledStart",
"length",
"fileSizeBytes",
})
log.WithFields(log.Fields{
"data_task_uid": info.TaskUID,
"update_distinct_task_uid": distinctTaskInfo.TaskUID,
"update_filterable_task_uid": filterableTaskInfo.TaskUID,
"update_sortable_task_uid": sortableTaskInfo.TaskUID,
}).Info("successfully updated search index with previously archived versions")
}
func SearchMetadata(w http.ResponseWriter, _ *http.Request) {
enabled := strings.ToLower(os.Getenv("MEILISEARCH_ENABLED")) == "true"
response := map[string]any{
"enabled": enabled,
}
if enabled {
response["index"] = os.Getenv("MEILISEARCH_INDEX")
response["url"] = os.Getenv("MEILISEARCH_URL")
response["apiKey"] = os.Getenv("MEILISEARCH_FRONTEND_API_KEY")
}
SerializeJson(w, response)
}
func (app *Application) UpsertVideo(video Video) error {
videos := []Video{video}
if _, err := app.search.AddDocuments(videos); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to upsert video")
return err
}
return nil
}
func (video *Video) asMeilisearch() (map[string]any, error) {
bytes, err := json.Marshal(video)
if err != nil {
return nil, err
}
var structured map[string]any
if err := json.Unmarshal(bytes, &structured); err != nil {
return nil, err
}
// meilisearch wants unix timestamp instead of rfc 3339
structured["start"] = video.Start.Unix()
// downloads should not be stored in Meilisearch
delete(structured, "downloads")
return structured, nil
}
func AllVideos(db *sql.DB) ([]Video, error) {
tx, err := db.Begin()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to start transaction")
return nil, err
}
defer tx.Rollback()
rows, err := tx.Query("select * from videos order by start")
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to prepare query")
return nil, err
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to close row")
}
}(rows)
var videos []Video
for rows.Next() {
var video Video
if err := rows.Scan(
&video.Id,
pq.Array(&video.Submitters),
&video.Start,
&video.Finished,
&video.Title,
&video.ChannelName,
&video.ChannelId,
&video.Thumbnail,
&video.FileSize,
&video.Length,
&video.Downloads); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to scan row into Video")
continue
}
videos = append(videos, video)
}
if err = tx.Commit(); err != nil {
log.WithFields(log.Fields{"error": err}).Warn("failed to commit transaction")
return videos, err
}
return videos, nil
}