Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents error when search query ends with empty parenthesis #2642

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/models/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SearchBuilder < Blacklight::SearchBuilder
include BlacklightAdvancedSearch::AdvancedSearchBuilder
include BlacklightHelper

self.default_processor_chain += %i[cleanup_boolean_operators add_advanced_search_to_solr
self.default_processor_chain += %i[parslet_trick cleanup_boolean_operators add_advanced_search_to_solr
cjk_mm wildcard_char_strip excessive_paging_error
only_home_facets left_anchor_escape_whitespace
series_title_results pul_holdings html_facets
Expand All @@ -17,6 +17,16 @@ def cleanup_boolean_operators(solr_parameters)
solr_parameters[:q] = cleaned_query(solr_parameters[:q])
end

# Blacklight uses Parslet https://rubygems.org/gems/parslet/versions/2.0.0 to parse the user query
# and unfortunately Parslet gets confused when the user's query ends with "()". Here we tweak the
# query to prevent the error and let the query to be parsed as if the ending "()" was not present.
# Notice that we must update the value in `blacklight_params[:q]`
def parslet_trick(_solr_parameters)
return unless blacklight_params[:q].is_a?(String)
return unless blacklight_params[:q].strip.end_with?("()")
blacklight_params[:q] = blacklight_params[:q].strip.gsub("()", "")
end

# Only search for coin records when querying with the numismatics advanced search
def numismatics_advanced(solr_parameters)
return unless blacklight_params[:advanced_type] == 'numismatics'
Expand Down
6 changes: 6 additions & 0 deletions spec/models/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@
search_builder.blacklight_params[:search_field] = 'advanced'
expect(search_builder.excessive_paging?).to be false
end

it 'handles query ending with empty parenthesis' do
search_builder.blacklight_params[:q] = 'hello world ()'
search_builder.parslet_trick({})
expect(search_builder.blacklight_params[:q].end_with?("()")).to be false
end
end
end