Skip to content

Commit

Permalink
Merge pull request #8121 from alphagov/delete-search-index-records-wh…
Browse files Browse the repository at this point in the history
…en-edition-becomes-unindexable

Delete search index records when an edition becomes unindexable
  • Loading branch information
ryanb-gds authored Aug 17, 2023
2 parents 4050917 + 402f27c commit 6a6eee6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/services/service_listeners/search_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def index!

Whitehall::SearchIndex.add(edition)
reindex_collection_documents
elsif edition.previous_edition&.can_index_in_search?
# If the previous edition was indexed but the current edition cannot be, we must delete the previous edition from the index
Whitehall::SearchIndex.delete(edition.previous_edition)
reindex_collection_documents
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RemoveOrphanedEditionSearchIndexRecords < ActiveRecord::Migration[7.0]
def change
# We want to find all editions which are publicly visible and therefore might once have been indexed, but are currently not searchable
# We then ask Search API to remove the record for these editions if a corresponding record is present
# Note that this doesn't seem to capture all foreign language content in the search index, but it has removed at least some
editions_with_possible_orphaned_search_index_records = Edition.publicly_visible.where.not(id: Edition.search_only.select(:id))
editions_with_possible_orphaned_search_index_records.each { |e| ServiceListeners::SearchIndexer.new(e).remove! }
end
end
11 changes: 11 additions & 0 deletions test/unit/app/services/service_listeners/search_indexer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class ServiceListeners::SearchIndexerTest < ActiveSupport::TestCase
ServiceListeners::SearchIndexer.new(edition).index!
end

test "#index! removes the edition from the search index if the current edition cannot be indexed, but the previous edition was indexed" do
english_edition = create(:news_article_world_news_story, :published)
ServiceListeners::SearchIndexer.new(english_edition).index!
non_english_edition = I18n.with_locale(:fr) do
create(:news_article_world_news_story, :published, primary_locale: :fr, document: english_edition.document)
end

expect_removal_from_index(english_edition)
ServiceListeners::SearchIndexer.new(non_english_edition).index!
end

test "#remove! removes the edition from the search index" do
edition = create(:published_news_article)

Expand Down

0 comments on commit 6a6eee6

Please sign in to comment.