A platform for community discussion. Free, open, simple.
FEATURE: prioritize_exact_search_title_match hidden setting (#20089)
Samc5345d0e5402
diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 1963eef2620..d0d1fd371f2 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2194,6 +2194,9 @@ backups:client: truesearch:+ prioritize_exact_search_title_match:+ default: false+ hidden: truemax_duplicate_search_index_terms:default: -1hidden: truediff --git a/lib/search.rb b/lib/search.rbindex 994e37fadf8..2afd2d1bae0 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -1132,14 +1132,13 @@ class Searchposts = posts.order("posts.like_count DESC")endelsif !is_topic_search- rank = <<~SQL- TS_RANK_CD(- #{SiteSetting.search_ranking_weights.present? ? "'#{SiteSetting.search_ranking_weights}'," : ""}- post_search_data.search_data,- #{@term.blank? ? "" : ts_query(weight_filter: weights)},- #{SiteSetting.search_ranking_normalization}|32- )- SQL+ exact_rank = nil++ if SiteSetting.prioritize_exact_search_title_match+ exact_rank = ts_rank_cd(weight_filter: "A", prefix_match: false)+ end++ rank = ts_rank_cd(weight_filter: weights)if type_filter != "private_messages"category_search_priority = <<~SQL@@ -1170,6 +1169,22 @@ class Search)SQL+ posts =+ if aggregate_search+ posts.order("MAX(#{category_search_priority}) DESC")+ else+ posts.order("#{category_search_priority} DESC")+ end++ if @term.present? && exact_rank+ posts =+ if aggregate_search+ posts.order("MAX(#{exact_rank} * #{category_priority_weights}) DESC")+ else+ posts.order("#{exact_rank} * #{category_priority_weights} DESC")+ end+ end+data_ranking =if @term.blank?"(#{category_priority_weights})"@@ -1179,9 +1194,9 @@ class Searchposts =if aggregate_search- posts.order("MAX(#{category_search_priority}) DESC", "MAX(#{data_ranking}) DESC")+ posts.order("MAX(#{data_ranking}) DESC")else- posts.order("#{category_search_priority} DESC", "#{data_ranking} DESC")+ posts.order("#{data_ranking} DESC")endend@@ -1211,6 +1226,17 @@ class Searchposts.limit(limit)end+ def ts_rank_cd(weight_filter:, prefix_match: true)+ <<~SQL+ TS_RANK_CD(+ #{SiteSetting.search_ranking_weights.present? ? "'#{SiteSetting.search_ranking_weights}'," : ""}+ post_search_data.search_data,+ #{@term.blank? ? "" : ts_query(weight_filter: weight_filter, prefix_match: prefix_match)},+ #{SiteSetting.search_ranking_normalization}|32+ )+ SQL+ end+def categories_ignored(posts)posts.where(<<~SQL, Searchable::PRIORITIES[:ignore])(categories.search_priority IS NULL OR categories.search_priority IS NOT NULL AND categories.search_priority <> ?)@@ -1225,8 +1251,11 @@ class Searchself.class.default_ts_configend- def self.ts_query(term:, ts_config: nil, joiner: nil, weight_filter: nil)- to_tsquery(ts_config: ts_config, term: set_tsquery_weight_filter(term, weight_filter))+ def self.ts_query(term:, ts_config: nil, joiner: nil, weight_filter: nil, prefix_match: true)+ to_tsquery(+ ts_config: ts_config,+ term: set_tsquery_weight_filter(term, weight_filter, prefix_match: prefix_match),+ )enddef self.to_tsquery(ts_config: nil, term:, joiner: nil)@@ -1237,8 +1266,8 @@ class Searchtsqueryend- def self.set_tsquery_weight_filter(term, weight_filter)- "'#{self.escape_string(term)}':*#{weight_filter}"+ def self.set_tsquery_weight_filter(term, weight_filter, prefix_match: true)+ "'#{self.escape_string(term)}':#{prefix_match ? "*" : ""}#{weight_filter}"enddef self.escape_string(term)@@ -1251,11 +1280,16 @@ class SearchPG::Connection.escape_string(term).gsub('\\', '\\\\\\')end- def ts_query(ts_config = nil, weight_filter: nil)+ def ts_query(ts_config = nil, weight_filter: nil, prefix_match: true)@ts_query_cache ||= {}@ts_query_cache[- "#{ts_config || default_ts_config} #{@term} #{weight_filter}"- ] ||= Search.ts_query(term: @term, ts_config: ts_config, weight_filter: weight_filter)+ "#{ts_config || default_ts_config} #{@term} #{weight_filter} #{prefix_match}"+ ] ||= Search.ts_query(+ term: @term,+ ts_config: ts_config,+ weight_filter: weight_filter,+ prefix_match: prefix_match,+ )enddef wrap_rows(query)diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex 05c1f9b9870..758d72e4f0a 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -2603,6 +2603,35 @@ RSpec.describe Search doendend+ context "when prioritize_exact_search_match is enabled" do+ before { SearchIndexer.enable }++ after { SearchIndexer.disable }++ it "correctly ranks topics" do+ SiteSetting.prioritize_exact_search_title_match = true++ topic1 = Fabricate(:topic, title: "saml saml saml is the best")+ post1 = Fabricate(:post, topic: topic1, raw: "this topic is a story about saml")++ topic2 = Fabricate(:topic, title: "sam has ideas about lots of things")+ post2 = Fabricate(:post, topic: topic2, raw: "this topic is not about saml saml saml")++ topic3 = Fabricate(:topic, title: "jane has ideas about lots of things")+ post3 = Fabricate(:post, topic: topic3, raw: "sam sam sam sam lets add sams")++ SearchIndexer.index(post1, force: true)+ SearchIndexer.index(post2, force: true)+ SearchIndexer.index(post3, force: true)++ result = Search.execute("sam")+ expect(result.posts.length).to eq(3)++ # title match should win cause we limited duplication+ expect(result.posts.pluck(:id)).to eq([post2.id, post1.id, post3.id])+ end+ end+context "when max_duplicate_search_index_terms limits duplication" dobefore { SearchIndexer.enable }