Skip to content

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

PERF: Update index for category in a background job.

Guo Xiang Tan650da7b626ae
diff --git a/app/jobs/regular/index_category_for_search.rb b/app/jobs/regular/index_category_for_search.rb
new file mode 100644
index 00000000000..396e95320f5
--- /dev/null
+++ b/app/jobs/regular/index_category_for_search.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+class Jobs::IndexCategoryForSearch < Jobs::Base
+ def execute(args)
+ category = Category.find_by(id: args[:category_id])
+ raise Discourse::InvalidParameters.new(:category_id) if category.blank?
+
+ SearchIndexer.index(category, force: args[:force] || false)
+ end
+end
diff --git a/app/models/category.rb b/app/models/category.rb
index 2623b153de5..8f2a4812392 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -778,11 +778,10 @@ class Category < ActiveRecord::Base
end
def index_search
- if saved_change_to_attribute?(:name)
- SearchIndexer.queue_category_posts_reindex(self.id)
- end
-
- SearchIndexer.index(self)
+ Jobs.enqueue(:index_category_for_search,
+ category_id: self.id,
+ force: saved_change_to_attribute?(:name),
+ )
end
def update_reviewables
diff --git a/app/services/search_indexer.rb b/app/services/search_indexer.rb
index 93a4c7bc6b9..12bdcbc4aaf 100644
--- a/app/services/search_indexer.rb
+++ b/app/services/search_indexer.rb
@@ -243,6 +243,7 @@ class SearchIndexer
end
if Category === obj && (obj.saved_change_to_name? || force)
+ SearchIndexer.queue_category_posts_reindex(obj.id)
SearchIndexer.update_categories_index(obj.id, obj.name)
end
diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb
index 7e9b0cb7976..236f562e5b8 100644
--- a/spec/components/search_spec.rb
+++ b/spec/components/search_spec.rb
@@ -8,6 +8,7 @@ describe Search do
before do
SearchIndexer.enable
+ Jobs.run_immediately!
end
context 'post indexing' do