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 end
def load_problem_post_ids(limit) def load_problem_post_ids(limit)
Post params = {
.where('posts.id IN ( locale: SiteSetting.default_locale,
SELECT p2.id FROM posts p2 version: Search::INDEX_VERSION,
LEFT JOIN post_search_data pd ON pd.locale = ? AND pd.version = ? AND p2.id = pd.post_id limit: limit
WHERE pd.post_id IS NULL }
)', SiteSetting.default_locale, Search::INDEX_VERSION)
.where("posts.raw != ''") DB.query_single(<<~SQL, params)
.limit(limit) SELECT
.order('posts.id DESC') posts.id
.pluck(: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 end
def load_problem_category_ids(limit) def load_problem_category_ids(limit)

View File

@ -49,6 +49,18 @@ describe Jobs::ReindexSearch do
FakeIndexer.reset FakeIndexer.reset
end 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 it 'should not reindex posts with empty raw' do
post = Fabricate(:post) post = Fabricate(:post)
post.post_search_data.destroy! post.post_search_data.destroy!