Skip to content

Commit

Permalink
wip: Use PgSearch::Document.rebuild for page reindex
Browse files Browse the repository at this point in the history
Adds a searchable_content method to page extension as well and configure multisearch that it uses this to build its content for pg search. That way we can remove our custom rebuild implementation.
  • Loading branch information
tvdeyen committed Dec 9, 2024
1 parent 9e3f997 commit 3d0f379
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
10 changes: 2 additions & 8 deletions app/extensions/alchemy/pg_search/page_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@ def self.prepended(base)
base.after_save :remove_unpublished_page
base.multisearchable(
against: [
:meta_description,
:meta_keywords,
:name,
:searchable_content
],
additional_attributes: ->(page) { { page_id: page.id, searchable_created_at: page.public_on } },
if: :searchable?,
)
end

def searchable?
(definition.key?(:searchable) ? definition[:searchable] : true) &&
searchable && public? && !layoutpage?
end

private

def remove_unpublished_page
Alchemy::PgSearch.remove_page(self) unless searchable?
::PgSearch::Document.delete_by(page_id: id) unless searchable?
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/extensions/alchemy/search/element_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def searchable
def searchable?
searchable && public? && page.searchable? && page_version.public?
end

def searchable_content
ingredients.select(&:searchable?).map(&:searchable_content).join(" ")
end
end

Alchemy::Element.prepend(Alchemy::Search::ElementExtension)
4 changes: 4 additions & 0 deletions app/extensions/alchemy/search/page_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def searchable?
(definition.key?(:searchable) ? definition[:searchable] : true) &&
searchable && public? && !layoutpage?
end

def searchable_content
all_elements.includes(ingredients: {element: :page}).map(&:searchable_content).join(" ")
end
end

Alchemy::Page.prepend(Alchemy::Search::PageExtension)
38 changes: 7 additions & 31 deletions lib/alchemy-pg_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,13 @@ module PgSearch
extend Config

##
# index all supported Alchemy pages
def self.rebuild
ActiveRecord::Base.transaction do
::PgSearch::Document.delete_all
Alchemy::Page.all.each{ |page| index_page(page) }
end
end

##
# remove the whole index for the page
#
# @param page [Alchemy::Page]
def self.remove_page(page)
::PgSearch::Document.delete_by(page_id: page.id)
end

##
# index a single page and indexable ingredients
#
# @param page [Alchemy::Page]
def self.index_page(page)
page.update_pg_search_document

document = page.pg_search_document
return if document.nil?

ingredient_content = page.all_elements.includes(ingredients: {element: :page}).map do |element|
element.ingredients.select { |i| i.searchable? }.map(&:searchable_content).join(" ")
end.join(" ")

document.update_column(:content, "#{document.content} #{ingredient_content}".squish)
# Reindex all supported Alchemy pages
def self.rebuild(clean_up: true, transactional: true)
::PgSearch::Multisearch.rebuild(
Alchemy::Page,
clean_up: clean_up,
transactional: transactional
)
end

##
Expand Down

0 comments on commit 3d0f379

Please sign in to comment.