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.
This commit is contained in:
Guo Xiang Tan 2019-04-02 07:36:53 +08:00
parent 3fc5dbb045
commit aa2311a7b0
2 changed files with 34 additions and 10 deletions

View File

@ -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)

View File

@ -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!