Skip to content

Commit c8c5062

Browse files
authored
Use generated columns for Movie and TvShow tsv search indexes (#33)
1 parent 42d6532 commit c8c5062

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class AddGeneratedTsvColumnToMovies < ActiveRecord::Migration[7.2]
2+
def up
3+
execute "DROP TRIGGER IF EXISTS tsvectorupdate ON movies"
4+
5+
remove_column :movies, :tsv, :tsvector
6+
7+
add_column :movies, :tsv, :tsvector, as: "to_tsvector('english', coalesce(title, '') || ' ' || coalesce(tagline, '') || ' ' || coalesce(overview, ''))", stored: true
8+
add_index :movies, :tsv, using: 'gin'
9+
end
10+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class AddGeneratedTsvColumnToTVShows < ActiveRecord::Migration[7.2]
2+
def up
3+
execute "DROP TRIGGER IF EXISTS tsvectorupdate ON tv_shows"
4+
5+
remove_column :tv_shows, :tsv, :tsvector
6+
7+
add_column :tv_shows, :tsv, :tsvector, as: "to_tsvector('english', coalesce(name, '') || ' ' || coalesce(overview, ''))", stored: true
8+
add_index :tv_shows, :tsv, using: 'gin'
9+
end
10+
end

db/schema.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.2].define(version: 2024_10_03_013148) do
13+
ActiveRecord::Schema[7.2].define(version: 2024_10_09_064855) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

@@ -89,7 +89,7 @@
8989
t.integer "vote_count", default: 0
9090
t.datetime "created_at", precision: nil, null: false
9191
t.datetime "updated_at", precision: nil, null: false
92-
t.tsvector "tsv"
92+
t.virtual "tsv", type: :tsvector, as: "to_tsvector('english'::regconfig, (((((COALESCE(title, ''::character varying))::text || ' '::text) || (COALESCE(tagline, ''::character varying))::text) || ' '::text) || (COALESCE(overview, ''::character varying))::text))", stored: true
9393
t.index ["release_date"], name: "index_movies_on_release_date"
9494
t.index ["title"], name: "index_movies_on_title"
9595
t.index ["tmdb_id"], name: "index_movies_on_tmdb_id"
@@ -138,7 +138,7 @@
138138
t.integer "vote_count", default: 0
139139
t.datetime "created_at", precision: nil, null: false
140140
t.datetime "updated_at", precision: nil, null: false
141-
t.tsvector "tsv"
141+
t.virtual "tsv", type: :tsvector, as: "to_tsvector('english'::regconfig, (((COALESCE(name, ''::character varying))::text || ' '::text) || (COALESCE(overview, ''::character varying))::text))", stored: true
142142
t.index ["name"], name: "index_tv_shows_on_name"
143143
t.index ["tmdb_id"], name: "index_tv_shows_on_tmdb_id"
144144
t.index ["tsv"], name: "index_tv_shows_on_tsv", using: :gin

0 commit comments

Comments
 (0)