A platform for community discussion. Free, open, simple.
FEATURE: allow to extend topic_eager_loads in Search (#10625)
Krzysztof Kotlarekcb58cbbc2c95
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 4729223a9e5..882c6184419 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -206,6 +206,17 @@ class Plugin::InstanceSearch.advanced_filter(trigger, &block)end+ # Allow to eager load additional tables in Search. Useful to avoid N+1 performance problems.+ # Example usage:+ # register_search_topic_eager_load do |opts|+ # %i(example_table)+ # end+ # OR+ # register_search_topic_eager_load(%i(example_table))+ def register_search_topic_eager_load(tables = nil, &block)+ Search.custom_topic_eager_load(tables, &block)+ end+# Request a new size for topic thumbnails# Will respect plugin enabled setting is enabled# Size should be an array with two elements [max_width, max_height]diff --git a/lib/search.rb b/lib/search.rbindex caa5edca206..f2ded80c485 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -297,6 +297,14 @@ class Search@advanced_filtersend+ def self.custom_topic_eager_load(tables = nil, &block)+ (@custom_topic_eager_loads ||= []) << (tables || block)+ end++ def self.custom_topic_eager_loads+ Array.wrap(@custom_topic_eager_loads)+ end+advanced_filter(/^in:personal-direct$/) do |posts|if @guardian.userposts@@ -1194,11 +1202,13 @@ class Searchtopic_eager_loads << :tagsend+ Search.custom_topic_eager_loads.each do |custom_loads|+ topic_eager_loads.concat(custom_loads.is_a?(Array) ? custom_loads : custom_loads.call(search_pms: @search_pms).to_a)+ end+query.includes(topic: topic_eager_loads)end- private-# Limited for performance reasons since `TS_HEADLINE` is slow when the text# document is too long.MAX_LENGTH_FOR_HEADLINE = 2500diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex 784b4f8c544..1aaf6edb49e 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -94,4 +94,34 @@ describe Search doendendend++ context "custom_eager_load" do+ fab!(:topic) { Fabricate(:topic) }+ fab!(:post) { Fabricate(:post, topic: topic) }++ before do+ SearchIndexer.enable+ SearchIndexer.index(topic, force: true)+ end++ it "includes custom tables" do+ begin+ expect(Search.execute("test").posts[0].topic.association(:category).loaded?).to be true+ expect(Search.execute("test").posts[0].topic.association(:tags).loaded?).to be false++ SiteSetting.tagging_enabled = true+ Search.custom_topic_eager_load([:topic_users])+ Search.custom_topic_eager_load() do+ [:bookmarks]+ end++ expect(Search.execute("test").posts[0].topic.association(:tags).loaded?).to be true+ expect(Search.execute("test").posts[0].topic.association(:topic_users).loaded?).to be true+ expect(Search.execute("test").posts[0].topic.association(:bookmarks).loaded?).to be true+ ensure+ SiteSetting.tagging_enabled = false+ Search.instance_variable_set(:@custom_topic_eager_loads, [])+ end+ end+ endend