Adding full-text search.
This commit is contained in:
2
.idea/dictionaries/project.xml
generated
2
.idea/dictionaries/project.xml
generated
@@ -2,7 +2,9 @@
|
|||||||
<dictionary name="project">
|
<dictionary name="project">
|
||||||
<words>
|
<words>
|
||||||
<w>httprouter</w>
|
<w>httprouter</w>
|
||||||
|
<w>plainto</w>
|
||||||
<w>servemux</w>
|
<w>servemux</w>
|
||||||
|
<w>tsvector</w>
|
||||||
</words>
|
</words>
|
||||||
</dictionary>
|
</dictionary>
|
||||||
</component>
|
</component>
|
||||||
@@ -195,9 +195,14 @@ func (m MovieModel) Delete(id int64) error {
|
|||||||
// GetAll : Returns a slice of movies. Although we're not using them right now, we've set this up to accept the various filter parameters as arguments
|
// GetAll : Returns a slice of movies. Although we're not using them right now, we've set this up to accept the various filter parameters as arguments
|
||||||
func (m MovieModel) GetAll(title string, genres []string, filters Filters) ([]*Movie, error) {
|
func (m MovieModel) GetAll(title string, genres []string, filters Filters) ([]*Movie, error) {
|
||||||
// Construct the SQL query to retrieve all movie records.
|
// Construct the SQL query to retrieve all movie records.
|
||||||
|
// to_tsvector('simple', title) transforms 'The Breakfast Club' into 'breakfast' 'club' 'the'. The 'simple' parameter's value is the configuration.
|
||||||
|
// plainto_tsquery('simple', $1) takes a search value and turns it into a formatted query term that PostgreSQL full-text search can understand. As an example : "The Club" would result in the query term 'the' & 'club'
|
||||||
|
// The @@ operator is the matches operator. To continue the example, the query term 'the' & 'club' will match rows which contain both lexemes 'the' and 'club'.
|
||||||
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`
|
||||||
|
|
||||||
// Create a context with a 3-second timeout
|
// Create a context with a 3-second timeout
|
||||||
@@ -205,7 +210,7 @@ func (m MovieModel) GetAll(title string, genres []string, filters Filters) ([]*M
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Use QueryContext() to execute the query. This returns a sql.Rows resultset containing the result
|
// Use QueryContext() to execute the query. This returns a sql.Rows resultset containing the result
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user