diff --git a/app/extensions/alchemy/pg_search/page_extension.rb b/app/extensions/alchemy/pg_search/page_extension.rb index 18c5a03..43b4a47 100644 --- a/app/extensions/alchemy/pg_search/page_extension.rb +++ b/app/extensions/alchemy/pg_search/page_extension.rb @@ -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 diff --git a/app/extensions/alchemy/search/element_extension.rb b/app/extensions/alchemy/search/element_extension.rb index 85ff1fc..cf4741b 100644 --- a/app/extensions/alchemy/search/element_extension.rb +++ b/app/extensions/alchemy/search/element_extension.rb @@ -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) diff --git a/app/extensions/alchemy/search/page_extension.rb b/app/extensions/alchemy/search/page_extension.rb index b59dc5b..d455b49 100644 --- a/app/extensions/alchemy/search/page_extension.rb +++ b/app/extensions/alchemy/search/page_extension.rb @@ -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) diff --git a/lib/alchemy-pg_search.rb b/lib/alchemy-pg_search.rb index 4b7f7e1..6899928 100644 --- a/lib/alchemy-pg_search.rb +++ b/lib/alchemy-pg_search.rb @@ -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 ##