Skip to content

A platform for community discussion. Free, open, simple.

PERF: Defer indexing post for search when saving a post.

Guo Xiang Tan337f062f0f2a
diff --git a/app/models/post.rb b/app/models/post.rb
index e07a78f8410..76db8ddd769 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -57,7 +57,7 @@ class Post < ActiveRecord::Base
validates_with PostValidator, unless: :skip_validation
- after_save :index_search
+ after_commit :index_search
# We can pass several creating options to a post via attributes
attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check, :skip_validation
@@ -898,7 +898,9 @@ class Post < ActiveRecord::Base
end
def index_search
- SearchIndexer.index(self)
+ Scheduler::Defer.later "Index post for search" do
+ SearchIndexer.index(self)
+ end
end
def locked?
diff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rb
index 01a24cc5c0c..3cf2ff4550d 100644
--- a/app/services/search_indexer.rb
+++ b/app/services/search_indexer.rb
@@ -201,9 +201,9 @@ class SearchIndexer
if Post === obj && obj.raw.present? &&
(
+ force ||
obj.saved_change_to_cooked? ||
- obj.saved_change_to_topic_id? ||
- force
+ obj.saved_change_to_topic_id?
)
if topic