Skip to content

Commit

Permalink
Use the JSON query API for all queries if configured (for consistency…
Browse files Browse the repository at this point in the history
… and to reduce duplicate configurations
  • Loading branch information
cbeer committed Mar 26, 2021
1 parent c310fc8 commit b7be3ff
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/blacklight/solr/search_builder_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def add_query_to_solr(solr_parameters)
##
if search_field&.query_builder.present?
add_search_field_query_builder_params(solr_parameters)
elsif search_field&.clause_params.present?
add_search_field_with_json_query_parameters(solr_parameters)
elsif search_field&.solr_local_parameters.present?
add_search_field_with_local_parameters(solr_parameters)
elsif search_state.query_param.is_a? Hash
Expand Down Expand Up @@ -86,6 +88,14 @@ def add_additional_filters(solr_parameters, additional_filters = nil)
solr_parameters[:spellcheck] = 'false'
end

def add_search_field_with_json_query_parameters(solr_parameters)
bool_query = search_field.clause_params.transform_values { |v| v.merge(query: search_state.query_param) }

solr_parameters[:json] ||= { query: { bool: { must: [] } } }
solr_parameters[:json][:query] ||= { bool: { must: [] } }
solr_parameters[:json][:query][:bool][:must] << bool_query
end

# Transform "clause" parameters into the Solr JSON Query DSL
def add_adv_search_clauses(solr_parameters)
return if search_state.clause_params.blank?
Expand Down
32 changes: 32 additions & 0 deletions spec/models/blacklight/solr/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,38 @@
end
end

describe "solr json query parameters from the fielded search" do
let(:user_params) { subject_search_params }

before do
blacklight_config.search_fields['subject'].solr_parameters = {
some: :parameter
}

blacklight_config.search_fields['subject'].clause_params = {
edismax: {
another: :parameter
}
}
end

it 'sets solr parameters from the field' do
expect(subject[:some]).to eq :parameter
end

it 'does not set a q parameter' do
expect(subject).not_to have_key :q
end

it 'includes the user query in the JSON query DSL request' do
expect(subject.dig(:json, :query, :bool, :must, 0, :edismax)).to include query: 'wome'
end

it 'includes addtional clause parameters for the field' do
expect(subject.dig(:json, :query, :bool, :must, 0, :edismax)).to include another: :parameter
end
end

describe "overriding of qt parameter" do
let(:user_params) do
{ qt: 'overridden' }
Expand Down

0 comments on commit b7be3ff

Please sign in to comment.