Skip to content

Commit

Permalink
i3664: Fix parsing error from JSON DSL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sandbergja committed Aug 2, 2023
1 parent 1d17b1d commit 69c9cfe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def numismatics_facets(solr_parameters)
end

def cleaned_query(query)
return query if query.nil?
query.gsub(/([A-Z]) (NOT|OR|AND) ([A-Z])/) do
"#{Regexp.last_match(1)} #{Regexp.last_match(2).downcase} #{Regexp.last_match(3)}"
end
Expand Down
47 changes: 47 additions & 0 deletions spec/models/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,51 @@
expect(search_builder.blacklight_params[:q].end_with?("()")).to be false
end
end

describe '#cleanup_boolean_operators' do
let(:solr_parameters) do
{ q: 'Douglas fir' }
end
let(:blacklight_config) do
Blacklight::Configuration.new do |config|
config.add_search_field('all_fields')
end
end
before do
allow(subject).to receive(:blacklight_params).and_return(solr_parameters)
allow(subject).to receive(:field_def).and_return([])
end
context 'when using edismax via a q param' do
context 'when q does not contain boolean operators' do
it 'does not change the q parameter' do
subject.cleanup_boolean_operators(solr_parameters)
expect(solr_parameters[:q]).to eq('Douglas fir')
end
end
context 'when q contains a boolean operator' do
let(:solr_parameters) do
{ q: 'solr AND blacklight' }
end
it 'parses the query' do
subject.cleanup_boolean_operators(solr_parameters)
expect(solr_parameters[:q]).to eq('+solr +blacklight')
end
end
end
context 'when using the JSON query DSL' do
let(:solr_parameters) do
{ "json" =>
{ "query" =>
{ "bool" =>
{ "must" =>
[{ edismax: { query: "solr AND blacklight" } }] } } } }
end
it 'does not change the solr_parameters json query' do
allow(subject).to receive(:blacklight_params).and_return({ q: 'solr AND blacklight' })
expect do
subject.cleanup_boolean_operators(solr_parameters)
end.not_to change { solr_parameters['json'] }
end
end
end
end

0 comments on commit 69c9cfe

Please sign in to comment.