FIX: Don't reindex posts belonging to a deleted topic for search.

FIX: Don’t reindex posts belonging to a deleted topic for search.

Posts belonging to a deleted topic can’t be index for search so we need to avoid loading those post ids.

diff --git a/app/jobs/scheduled/reindex_search.rb b/app/jobs/scheduled/reindex_search.rb
index ef341ab..408fd1f 100644
--- a/app/jobs/scheduled/reindex_search.rb
+++ b/app/jobs/scheduled/reindex_search.rb
@@ -69,16 +69,28 @@ module Jobs
     end
 
     def load_problem_post_ids(limit)
-      Post
-        .where('posts.id IN (
-                SELECT p2.id FROM posts p2
-                LEFT JOIN post_search_data pd ON pd.locale = ? AND pd.version = ? AND p2.id = pd.post_id
-                WHERE pd.post_id IS NULL
-                )', SiteSetting.default_locale, Search::INDEX_VERSION)
-        .where("posts.raw != ''")
-        .limit(limit)
-        .order('posts.id DESC')
-        .pluck(:id)
+      params = {
+        locale: SiteSetting.default_locale,
+        version: Search::INDEX_VERSION,
+        limit: limit
+      }
+
+      DB.query_single(<<~SQL, params)
+        SELECT
+          posts.id
+        FROM posts
+        LEFT JOIN post_search_data pd
+          ON pd.locale = :locale
+          AND pd.version = :version
+          AND pd.post_id = posts.id
+        LEFT JOIN topics ON topics.id = posts.topic_id
+        WHERE pd.post_id IS NULL
+        AND topics.id IS NOT NULL
+        AND topics.deleted_at IS NULL
+        AND posts.raw != ''
+        ORDER BY posts.id DESC
+        LIMIT :limit
+      SQL
     end
 
     def load_problem_category_ids(limit)
diff --git a/spec/jobs/reindex_search_spec.rb b/spec/jobs/reindex_search_spec.rb
index 8eef192..0fe89ad 100644
--- a/spec/jobs/reindex_search_spec.rb
+++ b/spec/jobs/reindex_search_spec.rb
@@ -49,6 +49,18 @@ describe Jobs::ReindexSearch do
       FakeIndexer.reset
     end
 
+    it 'should not reinex posts that belong to a deleted topic' do
+      post = Fabricate(:post)
+      post2 = Fabricate(:post)
+      post.post_search_data.destroy!
+      post2.post_search_data.destroy!
+      post2.topic.trash!
+
+      subject.rebuild_problem_posts(indexer: FakeIndexer)
+
+      expect(FakeIndexer.posts).to contain_exactly(post)
+    end
+
     it 'should not reindex posts with empty raw' do
       post = Fabricate(:post)
       post.post_search_data.destroy!

GitHub sha: aa2311a7

This commit has been mentioned on Discourse Meta. There might be relevant details there: