Skip to content

Commit

Permalink
Merge pull request #4356 from sul-dlss/single-term
Browse files Browse the repository at this point in the history
Use a special qf for single-term queries
  • Loading branch information
jcoyne authored Aug 1, 2024
2 parents e8abd7d + 6dbb238 commit dccfdd4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/models/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/models/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down

0 comments on commit dccfdd4

Please sign in to comment.