diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index a1998eeb084..0c771dccfe1 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -938,6 +938,7 @@ en: search_tokenize_chinese_japanese_korean: "Force search to tokenize Chinese/Japanese/Korean even on non CJK sites" search_prefer_recent_posts: "If searching your large forum is slow, this option tries an index of more recent posts first" search_recent_posts_size: "How many recent posts to keep in the index" + prioritize_open_topics_in_search: "Closed topics in search results will show up after open topics" allow_uncategorized_topics: "Allow topics to be created without a category. WARNING: If there are any uncategorized topics, you must recategorize them before turning this off." allow_duplicate_topic_titles: "Allow topics with identical, duplicate titles." unique_posts_mins: "How many minutes before a user can make a post with the same content again" diff --git a/config/site_settings.yml b/config/site_settings.yml index 62a6b009bd8..49867b8b6fa 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1132,6 +1132,7 @@ search: search_tokenize_chinese_japanese_korean: false search_prefer_recent_posts: false search_recent_posts_size: 100000 + prioritize_open_topics_in_search: false uncategorized: version_checks: diff --git a/lib/search.rb b/lib/search.rb index 543149b18e2..44153f47550 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -694,6 +694,8 @@ class Search posts = posts.order("posts.like_count DESC") end else + posts = posts.order("topics.closed") if SiteSetting.prioritize_open_topics_in_search + posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC") data_ranking = "TS_RANK_CD(post_search_data.search_data, #{ts_query})" diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index d215c8835c3..b3f89733037 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -662,6 +662,26 @@ describe Search do expect(Search.execute('Topic order:latest_topic').posts.map(&:id)).to eq([latest_irelevant_topic_post.id, old_relevant_topic_post.id]) end + context 'can prioritize open topics in search' do + before do + open_topic = Fabricate(:topic, title: 'Open Topic, testing prioritize open topics setting') + closed_topic = Fabricate(:topic, title: 'Closed Topic, testing prioritize open topics setting', closed: true) + + @open_irrelevant_topic_post = Fabricate(:post, topic: open_topic, raw: 'Not Relevant') + @closed_relevant_topic_post = Fabricate(:post, topic: closed_topic, raw: 'Relevant Topic') + end + + it "when prioritize_open_topics_in_search is disabled" do + SiteSetting.prioritize_open_topics_in_search = false + expect(Search.execute('Topic').posts.map(&:id)).to eq([@closed_relevant_topic_post.id, @open_irrelevant_topic_post.id]) + end + + it "when prioritize_open_topics_in_search is enabled" do + SiteSetting.prioritize_open_topics_in_search = true + expect(Search.execute('Topic').posts.map(&:id)).to eq([@open_irrelevant_topic_post.id, @closed_relevant_topic_post.id]) + end + end + it 'can tokenize dots' do post = Fabricate(:post, raw: 'Will.2000 Will.Bob.Bill...') expect(Search.execute('bill').posts.map(&:id)).to eq([post.id])