FIX: Don't index posts with empty `Post#raw` for search. (#7263)

* DEV: Remove unnecessary join in `Jobs::ReindexSearch`.

* FIX: Don't index posts with empty `Post#raw` for search.
This commit is contained in:
Guo Xiang Tan 2019-04-01 10:06:27 +08:00 committed by GitHub
parent 730ebdfcba
commit daeda80ada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 2 deletions

View File

@ -9,6 +9,7 @@ module Jobs
rebuild_problem_categories
rebuild_problem_users
rebuild_problem_tags
clean_post_search_data
end
def rebuild_problem_categories(limit = 500)
@ -60,8 +61,15 @@ module Jobs
private
def clean_post_search_data
PostSearchData
.joins("LEFT JOIN posts p ON p.id = post_search_data.post_id")
.where("p.raw = ''")
.delete_all
end
def load_problem_post_ids(limit)
Post.joins(:topic)
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

View File

@ -134,7 +134,7 @@ class SearchIndexer
category_name = topic.category&.name if topic
tag_names = topic.tags.pluck(:name).join(' ') if topic
if Post === obj &&
if Post === obj && obj.raw.present? &&
(
obj.saved_change_to_cooked? ||
obj.saved_change_to_topic_id? ||

View File

@ -28,4 +28,15 @@ describe Jobs::ReindexSearch do
expect(model.send("#{m}_search_data").version).to eq Search::INDEX_VERSION
end
end
it "should clean up post_search_data of posts with empty raw" do
post = Fabricate(:post)
post2 = Fabricate(:post, post_type: Post.types[:small_action])
post2.raw = ""
post2.save!(validate: false)
expect { subject.execute({}) }.to change { PostSearchData.count }.by(-1)
expect(Post.all).to contain_exactly(post, post2)
expect(PostSearchData.all).to contain_exactly(post.post_search_data)
end
end

View File

@ -103,5 +103,12 @@ describe SearchIndexer do
expect { post.update!(topic_id: Fabricate(:topic).id) }
.to change { post.reload.post_search_data.raw_data }
end
it 'should not index posts with empty raw' do
expect do
post = Fabricate.build(:post, raw: "", post_type: Post.types[:small_action])
post.save!(validate: false)
end.to_not change { PostSearchData.count }
end
end
end