diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 74d7cc3c3fc..13e1a21a0bd 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -799,6 +799,7 @@ en: dominating_topic_minimum_percent: "What percentage of posts a user has to make in a topic before we consider it dominating." suppress_uncategorized_badge: "Don't show the badge for uncategorized topics in topic lists" + min_posts_for_search_in_topic: "Disable search within topic if topics have less than minimum number posts" enable_names: "Allow users to show their full names" display_name_on_posts: "Also show a user's full name on their posts" diff --git a/config/site_settings.yml b/config/site_settings.yml index 5f1579fce50..63ec26a78e6 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -437,4 +437,5 @@ uncategorized: meta_category_id: default: -1 hidden: true + min_posts_for_search_in_topic: 10 diff --git a/lib/search.rb b/lib/search.rb index 6339bcb92f6..bf198b7040b 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -46,6 +46,10 @@ class Search @search_context = @opts[:search_context] @limit = Search.per_facet * Search.facets.size @results = GroupedSearchResults.new(@opts[:type_filter]) + + if Topic === @search_context && @search_context.posts_count < SiteSetting.min_posts_for_search_in_topic + @search_context = nil + end end # Query a term diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 8d9eaeb5553..0f4bbb16a57 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -133,6 +133,9 @@ describe Search do end it 'displays multiple results within a topic' do + + SiteSetting.stubs(:min_posts_for_search_in_topic).returns(3) + topic = Fabricate(:topic) topic2 = Fabricate(:topic) @@ -144,6 +147,9 @@ describe Search do post4 = new_post('this is my fourth post I am posting', topic) new_post('this is my fifth post I am posting', topic2) + # update posts_count + topic.reload + results = Search.new('posting', search_context: post1.topic).execute.find do |r| r[:type] == "topic" end[:results] @@ -160,6 +166,11 @@ describe Search do # trigger expanded search results = Search.new('birds', search_context: post1.topic).execute + SiteSetting.stubs(:min_posts_for_search_in_topic).returns(10) + results = Search.new('posting', search_context: post1.topic).execute.find do |r| + r[:type] == "topic" + end[:results].length.should == 2 + end end