@@ -3,18 +3,14 @@ package search
33import (
44 "bytes"
55 "context"
6- "github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
7- "github.com/algolia/algoliasearch-client-go/v3/algolia/search"
86 "github.com/ansel1/merry/v2"
97 "github.com/bcc-code/bcc-media-platform/backend/common"
108 "github.com/bcc-code/bcc-media-platform/backend/loaders"
119 "github.com/bcc-code/bcc-media-platform/backend/log"
1210 "github.com/elastic/go-elasticsearch/v8"
13- "github.com/google/uuid"
1411 "github.com/orsinium-labs/enum"
1512 "github.com/samber/lo"
1613 "strconv"
17- "strings"
1814)
1915
2016const tempIndexName = "bccm-temp"
@@ -111,113 +107,6 @@ func (service *Service) ReindexElastic(ctx context.Context) error {
111107 return nil
112108}
113109
114- // Reindex every supported collection with Algolia
115- func (service * Service ) Reindex (ctx context.Context ) error {
116- res , err := service .algoliaClient .CopyIndex (indexName , tempIndexName )
117- if err != nil {
118- return err
119- }
120- _ = res .Wait ()
121-
122- index := service .algoliaClient .InitIndex (tempIndexName )
123- _ , err = index .ClearObjects ()
124- if err != nil {
125- return err
126- }
127-
128- // Make sure we're not fetching from cache anywhere,
129- // although that shouldn't be an issue, as we're priming on fetch anyway
130- service .loaders .ShowLoader .ClearAll ()
131- service .loaders .ShowPermissionLoader .ClearAll ()
132- service .loaders .SeasonLoader .ClearAll ()
133- service .loaders .SeasonPermissionLoader .ClearAll ()
134- service .loaders .EpisodeLoader .ClearAll ()
135- service .loaders .EpisodePermissionLoader .ClearAll ()
136- service .loaders .PlaylistLoader .ClearAll ()
137- service .loaders .PlaylistPermissionLoader .ClearAll ()
138-
139- // Makes it possible to filter in query, which fields you are searching on
140- // Also configures hits per page
141- titleFields , err := service .getTranslatedTitleFields ()
142- if err != nil {
143- return err
144- }
145- descriptionFields , err := service .getTranslatedDescriptionFields ()
146- if err != nil {
147- return err
148- }
149- relationalFields , err := service .getRelationalTranslatedFields ()
150- if err != nil {
151- return err
152- }
153- searchableAttributes := opt .SearchableAttributes (
154- strings .Join (titleFields , ", " ),
155- tagsField ,
156- strings .Join (descriptionFields , ", " ),
157- strings .Join (relationalFields , ", " ),
158- strings .Join (getFunctionalFields (), ", " ),
159- )
160- languages , err := service .getLanguageKeys ()
161- if err != nil {
162- return err
163- }
164-
165- supportedLanguages := []string {"da" , "de" , "en" , "es" , "fi" , "fr" , "hu" , "it" , "nl" , "no" , "pl" , "pt" , "ro" , "ru" , "tr" }
166-
167- languages = lo .Filter (languages , func (l string , _ int ) bool {
168- return lo .Contains (supportedLanguages , l )
169- })
170-
171- _ , err = index .SetSettings (search.Settings {
172- IndexLanguages : opt .IndexLanguages (languages ... ),
173- QueryLanguages : opt .QueryLanguages (languages ... ),
174- SearchableAttributes : searchableAttributes ,
175- AttributesForFaceting : opt .AttributesForFaceting (service .getFilterFields ()... ),
176- HitsPerPage : opt .HitsPerPage (hitsPerPage ),
177- })
178- if err != nil {
179- return err
180- }
181-
182- log .L .Debug ().Str ("collection" , "shows" ).Msg ("Indexing" )
183- err = service .indexShows (ctx , index )
184- if err != nil {
185- return err
186- }
187- log .L .Debug ().Str ("collection" , "seasons" ).Msg ("Indexing" )
188- err = service .indexSeasons (ctx , index )
189- if err != nil {
190- return err
191- }
192- log .L .Debug ().Str ("collection" , "episodes" ).Msg ("Indexing" )
193- err = service .indexEpisodes (ctx , index )
194- if err != nil {
195- return err
196- }
197- log .L .Debug ().Str ("collection" , "episodes" ).Msg ("Indexing" )
198- err = service .indexPlaylists (ctx , index )
199- if err != nil {
200- return err
201- }
202-
203- res , err = service .algoliaClient .MoveIndex (tempIndexName , indexName )
204- if err != nil {
205- return err
206- }
207- return res .Wait ()
208- }
209-
210- func (service * Service ) indexShows (ctx context.Context , index * search.Index ) error {
211- return indexCollection [int , common.Show ](
212- ctx ,
213- index ,
214- service .loaders .ShowLoader ,
215- service .loaders .ShowPermissionLoader ,
216- service .queries .ListShows ,
217- service .showToSearchItem ,
218- )
219- }
220-
221110func (service * Service ) indexShow (ctx context.Context , id int ) error {
222111 i , err := service .loaders .ShowLoader .Load (ctx , id )()
223112 if err != nil {
@@ -228,23 +117,7 @@ func (service *Service) indexShow(ctx context.Context, id int) error {
228117 return err
229118 }
230119
231- err = indexObjectElastic [int , common.Show ](ctx , service .elasticClient , IndexShows , * i , p , service .showToSearchItem )
232- if err != nil {
233- log .L .Error ().Err (err ).Msg ("Could not index show in elastic" )
234- }
235-
236- return indexObject [int , common.Show ](ctx , service , * i , p , service .showToSearchItem )
237- }
238-
239- func (service * Service ) indexSeasons (ctx context.Context , index * search.Index ) error {
240- return indexCollection [int , common.Season ](
241- ctx ,
242- index ,
243- service .loaders .SeasonLoader ,
244- service .loaders .SeasonPermissionLoader ,
245- service .queries .ListSeasons ,
246- service .seasonToSearchItem ,
247- )
120+ return indexObjectElastic [int , common.Show ](ctx , service .elasticClient , IndexShows , * i , p , service .showToSearchItem )
248121}
249122
250123func (service * Service ) indexSeason (ctx context.Context , id int ) error {
@@ -257,22 +130,7 @@ func (service *Service) indexSeason(ctx context.Context, id int) error {
257130 return err
258131 }
259132
260- err = indexObjectElastic [int , common.Season ](ctx , service .elasticClient , IndexSeasons , * i , p , service .seasonToSearchItem )
261- if err != nil {
262- log .L .Error ().Err (err ).Msg ("Could not index season in elastic" )
263- }
264- return indexObject [int , common.Season ](ctx , service , * i , p , service .seasonToSearchItem )
265- }
266-
267- func (service * Service ) indexEpisodes (ctx context.Context , index * search.Index ) error {
268- return indexCollection [int , common.Episode ](
269- ctx ,
270- index ,
271- service .loaders .EpisodeLoader ,
272- service .loaders .EpisodePermissionLoader ,
273- service .queries .ListEpisodes ,
274- service .episodeToSearchItem ,
275- )
133+ return indexObjectElastic [int , common.Season ](ctx , service .elasticClient , IndexSeasons , * i , p , service .seasonToSearchItem )
276134}
277135
278136func (service * Service ) indexEpisode (ctx context.Context , id int ) error {
@@ -285,22 +143,7 @@ func (service *Service) indexEpisode(ctx context.Context, id int) error {
285143 return err
286144 }
287145
288- err = indexObjectElastic [int , common.Episode ](ctx , service .elasticClient , IndexEpisodes , * i , p , service .episodeToSearchItem )
289- if err != nil {
290- log .L .Error ().Err (err ).Msg ("Could not index episode in elastic" )
291- }
292- return indexObject [int , common.Episode ](ctx , service , * i , p , service .episodeToSearchItem )
293- }
294-
295- func (service * Service ) indexPlaylists (ctx context.Context , index * search.Index ) error {
296- return indexCollection [uuid.UUID , common.Playlist ](
297- ctx ,
298- index ,
299- service .loaders .PlaylistLoader ,
300- service .loaders .PlaylistPermissionLoader ,
301- service .queries .ListPlaylists ,
302- service .playlistToSearchItem ,
303- )
146+ return indexObjectElastic [int , common.Episode ](ctx , service .elasticClient , IndexEpisodes , * i , p , service .episodeToSearchItem )
304147}
305148
306149func indexCollectionElastic [k comparable , t indexable [k ]](
@@ -362,80 +205,6 @@ type indexable[k comparable] interface {
362205 GetKey () k
363206}
364207
365- func indexCollection [k comparable , t indexable [k ]](
366- ctx context.Context ,
367- index * search.Index ,
368- loader * loaders.Loader [k , * t ],
369- permissionLoader * loaders.Loader [k , * common.Permissions [k ]],
370- factory func (context.Context ) ([]t , error ),
371- converter func (context.Context , t ) (searchItem , error ),
372- ) error {
373- items , err := factory (ctx )
374- if err != nil {
375- return err
376- }
377-
378- ids := lo .Map (items , func (i t , _ int ) k {
379- return i .GetKey ()
380- })
381-
382- permissionLoader .LoadMany (ctx , ids )()
383-
384- var searchItems []searchObject
385- pushItems := func (force bool ) error {
386- if len (searchItems ) > 200 || (force && len (searchItems ) > 0 ) {
387- _ , err := index .SaveObjects (searchItems )
388- if err != nil {
389- return err
390- }
391- searchItems = []searchObject {}
392- }
393- return nil
394- }
395-
396- for _ , i := range items {
397- p := i
398- loader .Prime (ctx , p .GetKey (), & p )
399-
400- item , err := converter (ctx , p )
401- if err != nil {
402- return err
403- }
404-
405- perm , err := permissionLoader .Get (ctx , i .GetKey ())
406- if err != nil {
407- return err
408- }
409-
410- if perm != nil {
411- item .assignVisibility (perm .Availability )
412- item .assignRoles (perm .Roles )
413- }
414-
415- searchItems = append (searchItems , item .toSearchObject ())
416- err = pushItems (false )
417- }
418- return pushItems (true )
419- }
420-
421- func indexObject [k comparable , t indexable [k ]](
422- ctx context.Context ,
423- service * Service ,
424- obj t ,
425- perms * common.Permissions [k ],
426- converter func (context.Context , t ) (searchItem , error ),
427- ) error {
428- item , err := converter (ctx , obj )
429- if err != nil {
430- return err
431- }
432-
433- item .assignVisibility (perms .Availability )
434- item .assignRoles (perms .Roles )
435- _ , err = service .index .SaveObject (item )
436- return err
437- }
438-
439208func indexObjectElastic [k comparable , t indexable [k ]](
440209 ctx context.Context ,
441210 elasticClient * elasticsearch.TypedClient ,
@@ -473,11 +242,7 @@ func (service *Service) DeleteModel(ctx context.Context, collection string, key
473242 return nil
474243 }
475244
476- _ , err := service .index .DeleteObject (collection + "-" + key )
477- if err != nil {
478- return err
479- }
480-
245+ var err error
481246 if index , ok := typeToIndexMap [collection ]; ok {
482247 _ , err = service .elasticClient .Delete (index , collection + "-" + key ).Do (ctx )
483248 }
0 commit comments