diff --git a/app/models/search_builder.rb b/app/models/search_builder.rb index a4113c7ff..9cd253d71 100644 --- a/app/models/search_builder.rb +++ b/app/models/search_builder.rb @@ -12,6 +12,7 @@ class SearchBuilder < Blacklight::SearchBuilder self.default_processor_chain += [:database_prefix_search] self.default_processor_chain += [:modify_params_for_cjk, :modify_params_for_cjk_advanced] self.default_processor_chain += [:consolidate_home_page_params] + self.default_processor_chain += [:modify_single_term_qf] # Override range limit to only add parameters on search pages, not the home page def add_range_limit_params(*args) @@ -34,6 +35,18 @@ def database_prefix_search(solr_params) solr_params[:fq] << "title_sort:/[#{blacklight_params[:prefix]}].*/" end + # Solr edismax does not apply phrase boosts to single-term matches. When we have a single term query, + # we can use a different set of query boosts to give better results. + def modify_single_term_qf(solr_params) + return unless Settings.search.use_single_term_query_fields + return unless blacklight_params && blacklight_params[:q].present? && solr_params[:qf].blank? + + q_str = blacklight_params[:q] + return unless q_str.split.size == 1 + + solr_params[:qf] = '${qf_single_term}' + end + private def page_location diff --git a/config/settings.yml b/config/settings.yml index 5e2e09c59..780779c39 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -98,6 +98,9 @@ dynamic_sitemap_solr_endpoint: 'select' browse_nearby: enabled: true +search: + use_single_term_query_fields: false + libraries: ARS: about_url: https://library.stanford.edu/libraries/archive-recorded-sound diff --git a/spec/models/search_builder_spec.rb b/spec/models/search_builder_spec.rb index 4413c3fe3..a91cc85fe 100644 --- a/spec/models/search_builder_spec.rb +++ b/spec/models/search_builder_spec.rb @@ -34,6 +34,27 @@ end end + describe 'single term query' do + before do + allow(Settings.search).to receive(:use_single_term_query_fields).and_return(true) + end + + it "sets the single-term query fields" do + solr_params = search_builder.with(q: 'singleterm').to_hash + expect(solr_params[:qf]).to eq '${qf_single_term}' + end + + it "leaves multi-term queries alone" do + solr_params = search_builder.with(q: 'multi term').to_hash + expect(solr_params[:qf]).to be_nil + end + + it "leaves fielded search queries alone" do + solr_params = search_builder.with(q: 'multi term', search_field: 'search_title').to_hash + expect(solr_params[:qf]).to eq '${qf_title}' + end + end + describe "advanced search" do let(:action_name) { 'advanced_search' }