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:
parent
730ebdfcba
commit
daeda80ada
|
@ -9,6 +9,7 @@ module Jobs
|
||||||
rebuild_problem_categories
|
rebuild_problem_categories
|
||||||
rebuild_problem_users
|
rebuild_problem_users
|
||||||
rebuild_problem_tags
|
rebuild_problem_tags
|
||||||
|
clean_post_search_data
|
||||||
end
|
end
|
||||||
|
|
||||||
def rebuild_problem_categories(limit = 500)
|
def rebuild_problem_categories(limit = 500)
|
||||||
|
@ -60,8 +61,15 @@ module Jobs
|
||||||
|
|
||||||
private
|
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)
|
def load_problem_post_ids(limit)
|
||||||
Post.joins(:topic)
|
Post
|
||||||
.where('posts.id IN (
|
.where('posts.id IN (
|
||||||
SELECT p2.id FROM posts p2
|
SELECT p2.id FROM posts p2
|
||||||
LEFT JOIN post_search_data pd ON pd.locale = ? AND pd.version = ? AND p2.id = pd.post_id
|
LEFT JOIN post_search_data pd ON pd.locale = ? AND pd.version = ? AND p2.id = pd.post_id
|
||||||
|
|
|
@ -134,7 +134,7 @@ class SearchIndexer
|
||||||
category_name = topic.category&.name if topic
|
category_name = topic.category&.name if topic
|
||||||
tag_names = topic.tags.pluck(:name).join(' ') 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_cooked? ||
|
||||||
obj.saved_change_to_topic_id? ||
|
obj.saved_change_to_topic_id? ||
|
||||||
|
|
|
@ -28,4 +28,15 @@ describe Jobs::ReindexSearch do
|
||||||
expect(model.send("#{m}_search_data").version).to eq Search::INDEX_VERSION
|
expect(model.send("#{m}_search_data").version).to eq Search::INDEX_VERSION
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -103,5 +103,12 @@ describe SearchIndexer do
|
||||||
expect { post.update!(topic_id: Fabricate(:topic).id) }
|
expect { post.update!(topic_id: Fabricate(:topic).id) }
|
||||||
.to change { post.reload.post_search_data.raw_data }
|
.to change { post.reload.post_search_data.raw_data }
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue