diff --git a/app/jobs/scheduled/reindex_search.rb b/app/jobs/scheduled/reindex_search.rb index ef341ab782d..408fd1f517d 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 8eef1922050..0fe89adabfe 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!