[Search 2.0] Change podcast_episode query (#13568)

* Change podcast_episode query

* Add the index of all indexes

* Update ATTRIBUTES
This commit is contained in:
Alex 2021-04-29 02:49:47 -04:00 committed by GitHub
parent 3837c3a221
commit 11138c4f18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 25 deletions

View file

@ -1,24 +1,20 @@
module Search
module Postgres
class PodcastEpisode
ATTRIBUTES = [
"podcasts.id",
"podcasts.image",
"podcasts.published",
"podcasts.slug",
"podcast_episodes.body",
"podcast_episodes.comments_count",
"podcast_episodes.id",
"podcast_episodes.podcast_id",
"podcast_episodes.processed_html",
"podcast_episodes.published_at",
"podcast_episodes.quote",
"podcast_episodes.reactions_count",
"podcast_episodes.slug",
"podcast_episodes.subtitle",
"podcast_episodes.summary",
"podcast_episodes.title",
"podcast_episodes.website_url",
ATTRIBUTES = %w[
body
comments_count
id
podcast_id
processed_html
published_at
quote
reactions_count
slug
subtitle
summary
title
website_url
].freeze
private_constant :ATTRIBUTES
@ -34,16 +30,26 @@ module Search
page = page.to_i + 1
per_page = [(per_page || DEFAULT_PER_PAGE).to_i, MAX_PER_PAGE].min
relation = ::PodcastEpisode.includes(:podcast).available
relation = ::PodcastEpisode
.reachable
.where(podcast_id: Podcast.published)
.includes(:podcast)
.references(:podcasts)
relation = relation.search_podcast_episodes(term) if term.present?
relation = relation.select(*ATTRIBUTES)
relation = relation.reorder(sort_by => sort_direction) if sort_by && sort_direction
relation = sort(relation, sort_by, sort_direction)
results = relation.page(page).per(per_page)
serialize(results)
end
def self.sort(relation, sort_by, sort_direction)
return relation.reorder(sort_by => sort_direction) if sort_by && sort_direction
relation.reorder(nil)
end
private_class_method :sort
def self.serialize(results)
Search::PostgresPodcastEpisodeSerializer
.new(results, is_collection: true)

View file

@ -0,0 +1,74 @@
class AddTsvectorIndexOnSearchableColumnnsToPodcastEpisodes < ActiveRecord::Migration[6.1]
disable_ddl_transaction!
def up
query = <<-SQL
((((
to_tsvector('simple'::regconfig, COALESCE(body, ''::text)) ||
to_tsvector('simple'::regconfig, COALESCE((subtitle)::text, ''::text))) ||
to_tsvector('simple'::regconfig, COALESCE((title)::text, ''::text
)))))
SQL
unless index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_search_fields_as_tsvector)
add_index :podcast_episodes,
query,
using: :gin,
name: :index_podcast_episodes_on_search_fields_as_tsvector,
algorithm: :concurrently
end
# Removing unused existing indexes
if index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_body_as_tsvector)
remove_index :podcast_episodes,
name: :index_podcast_episodes_on_body_as_tsvector,
algorithm: :concurrently
end
if index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_subtitle_as_tsvector)
remove_index :podcast_episodes,
name: :index_podcast_episodes_on_subtitle_as_tsvector,
algorithm: :concurrently
end
if index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_title_as_tsvector)
remove_index :podcast_episodes,
name: :index_podcast_episodes_on_title_as_tsvector,
algorithm: :concurrently
end
end
def down
if index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_search_fields_as_tsvector)
remove_index :podcast_episodes,
name: :index_podcast_episodes_on_search_fields_as_tsvector,
algorithm: :concurrently
end
# Add back old unused indexes
unless index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_body_as_tsvector)
add_index :podcast_episodes,
"to_tsvector('simple'::regconfig, COALESCE((body)::text, ''::text))",
using: :gin,
name: :index_podcast_episodes_on_body_as_tsvector,
algorithm: :concurrently
end
unless index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_subtitle_as_tsvector)
add_index :podcast_episodes,
"to_tsvector('simple'::regconfig, COALESCE((subtitle)::text, ''::text))",
using: :gin,
name: :index_podcast_episodes_on_subtitle_as_tsvector,
algorithm: :concurrently
end
unless index_name_exists?(:podcast_episodes, :index_podcast_episodes_on_title_as_tsvector)
add_index :podcast_episodes,
"to_tsvector('simple'::regconfig, COALESCE((title)::text, ''::text))",
using: :gin,
name: :index_podcast_episodes_on_title_as_tsvector,
algorithm: :concurrently
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_04_26_165234) do
ActiveRecord::Schema.define(version: 2021_04_28_190634) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
@ -892,9 +892,7 @@ ActiveRecord::Schema.define(version: 2021_04_26_165234) do
t.string "title", null: false
t.datetime "updated_at", null: false
t.string "website_url"
t.index "to_tsvector('simple'::regconfig, COALESCE((subtitle)::text, ''::text))", name: "index_podcast_episodes_on_subtitle_as_tsvector", using: :gin
t.index "to_tsvector('simple'::regconfig, COALESCE((title)::text, ''::text))", name: "index_podcast_episodes_on_title_as_tsvector", using: :gin
t.index "to_tsvector('simple'::regconfig, COALESCE(body, ''::text))", name: "index_podcast_episodes_on_body_as_tsvector", using: :gin
t.index "(((to_tsvector('simple'::regconfig, COALESCE(body, ''::text)) || to_tsvector('simple'::regconfig, COALESCE((subtitle)::text, ''::text))) || to_tsvector('simple'::regconfig, COALESCE((title)::text, ''::text))))", name: "index_podcast_episodes_on_search_fields_as_tsvector", using: :gin
t.index ["guid"], name: "index_podcast_episodes_on_guid", unique: true
t.index ["media_url"], name: "index_podcast_episodes_on_media_url", unique: true
t.index ["podcast_id"], name: "index_podcast_episodes_on_podcast_id"