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

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

Indexing a post for search is slow and there is no reason for us to have to block saving a post due to search indexing.

diff --git a/app/models/post.rb b/app/models/post.rb
index e07a78f..76db8dd 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 01a24cc..3cf2ff4 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

GitHub sha: 337f062f