Skip to content

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

DEV: Add hidden site setting to configure search ranking weights (#20086)

Alan Guo Xiang Tan6934edd97cfd
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 0ec95ded873..4d40276b7af 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2202,6 +2202,9 @@ search:
search_ranking_normalization:
default: "0"
hidden: true
+ search_ranking_weights:
+ default: ""
+ hidden: true
min_search_term_length:
client: true
default: 3
diff --git a/lib/search.rb b/lib/search.rb
index 0429afe2bbd..994e37fadf8 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -1134,6 +1134,7 @@ class Search
elsif !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
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index 4388a13ebde..fdc91daad44 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -113,6 +113,28 @@ RSpec.describe Search do
expect(Search.execute("oeuvre").posts).to contain_exactly(post_2)
end
end
+
+ context "when search_ranking_weights site setting has been configured" do
+ fab!(:topic) { Fabricate(:topic, title: "Some random topic title start") }
+ fab!(:topic2) { Fabricate(:topic, title: "Some random topic title") }
+ fab!(:post1) { Fabricate(:post, raw: "start", topic: topic) }
+ fab!(:post2) { Fabricate(:post, raw: "#{"start " * 100}", topic: topic2) }
+
+ before do
+ SearchIndexer.enable
+ [post1, post2].each { |post| SearchIndexer.index(post, force: true) }
+ end
+
+ after { SearchIndexer.disable }
+
+ it "should apply the custom ranking weights correctly" do
+ expect(Search.execute("start").posts).to eq([post2, post1])
+
+ SiteSetting.search_ranking_weights = "{0.00001,0.2,0.4,1.0}"
+
+ expect(Search.execute("start").posts).to eq([post1, post2])
+ end
+ end
end
context "with apostrophes" do