ch9.5
This commit is contained in:
@@ -138,12 +138,14 @@ func (m MovieModel) GetAll(title string, genres []string, filter Filters) ([]*Mo
|
|||||||
query := `
|
query := `
|
||||||
SELECT id, created_at, title, year, runtime, genres, version
|
SELECT id, created_at, title, year, runtime, genres, version
|
||||||
FROM movies
|
FROM movies
|
||||||
|
WHERE (to_tsvector('simple', title) @@ plainto_tsquery('simple', $1) OR $1 = '')
|
||||||
|
AND (genres @> $2 OR $2 = '{}')
|
||||||
ORDER BY id`
|
ORDER BY id`
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
rows, err := m.DB.QueryContext(ctx, query)
|
rows, err := m.DB.QueryContext(ctx, query, title, pq.Array(genres))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
2
migrations/000003_add_movies_indexes.down.sql
Normal file
2
migrations/000003_add_movies_indexes.down.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
DROP INDEX IF EXISTS movies_title_idx;
|
||||||
|
DROP INDEX IF EXISTS movies_genres_idx;
|
||||||
2
migrations/000003_add_movies_indexes.up.sql
Normal file
2
migrations/000003_add_movies_indexes.up.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
CREATE INDEX IF NOT EXISTS movies_title_idx ON movies USING GIN (to_tsvector('simple', title));
|
||||||
|
CREATE INDEX IF NOT EXISTS movies_genres_idx ON movies USING GIN (genres);
|
||||||
Reference in New Issue
Block a user