Skip to content

Commit

Permalink
Merge pull request #28 from projectblacklight/fix_parse_basic_q
Browse files Browse the repository at this point in the history
Make parse_basic_q function work again.
  • Loading branch information
jcoyne committed Aug 20, 2014
2 parents 396e681 + aeaf73e commit 02108a9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/blacklight_advanced_search/parse_basic_q.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'parslet'
require 'parsing_nesting/tree'

# A solr search logic mix-in to CatalogController.
# If mixed-in, adds Advanced Search parsing behavior
# to queries entered on basic/standard/simple search
Expand All @@ -12,6 +15,14 @@ module BlacklightAdvancedSearch::ParseBasicQ
included do
self.solr_search_params_logic += [:add_advanced_parse_q_to_solr]
end

# Different versions of Parslet raise different exception classes,
# need to figure out which one exists to rescue
@@parslet_failed_exceptions = if defined? Parslet::UnconsumedInput
[Parslet::UnconsumedInput]
else
[Parslet::ParseFailed]
end


# This method can be included in solr_search_params_logic to have us
Expand Down Expand Up @@ -41,10 +52,10 @@ def add_advanced_parse_q_to_solr(solr_parameters, req_params = params)
# and just allow basic search, perhaps with a warning.
begin
adv_search_params = ParsingNesting::Tree.parse(req_params[:q], blacklight_config.advanced_search[:query_parser]).to_single_query_params( solr_local_params )

BlacklightAdvancedSearch.deep_merge!(solr_parameters, solr_direct_params)
BlacklightAdvancedSearch.deep_merge!(solr_parameters, adv_search_params)
rescue Parslet::UnconsumedInput => e
rescue *@@parslet_failed_exceptions => e
# do nothing, don't merge our input in, keep basic search
# optional TODO, display error message in flash here, but hard to
# display a good one.
Expand Down
85 changes: 85 additions & 0 deletions spec/parsing_nesting/parse_basic_q_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "NestingParser" do

# Our ParseBasicQ mixin assumes a SolrHelper context.
# SolrHelper is a controller layer mixin, which depends
# on being mixed into a class which has #params (from Rails)
# and #blacklight_config
#
# This technique of testing is copied from Blacklight solr_helper_spec.rb
#
# It gets kind of a mess of dependencies, sorry.
class ParseBasicQTestClass
cattr_accessor :blacklight_config
cattr_accessor :blacklight_solr

include Blacklight::SolrHelper

include BlacklightAdvancedSearch::ParseBasicQ


def initialize blacklight_config, blacklight_solr
self.blacklight_config = blacklight_config
self.blacklight_solr = blacklight_solr
end

def params
{}
end

def logger
Rails.logger
end
end

describe "basic functionality" do
before do
@blacklight_config = Blacklight::Configuration.new do |config|
config.advanced_search = {

}

config.add_search_field "all_fields" do |field|

end

config.add_search_field "special_field" do |field|
field.advanced_parse = false
end
end
@obj = ParseBasicQTestClass.new @blacklight_config, Blacklight.solr
end

it "catches a simple example" do
solr_params = {}
@obj.add_advanced_parse_q_to_solr(solr_params, :q => "one two AND three OR four")

expect(solr_params[:defType]).to eq("lucene")
# We're not testing succesful parsing here, just that it's doing
# something that looks like we expect with subqueries.
expect(solr_params[:q]).to start_with("_query_:")
end

it "passes through an unparseable example" do
solr_params = {}
unparseable_q = "foo bar\'s AND"
@obj.add_advanced_parse_q_to_solr(solr_params, :q => unparseable_q)

expect(solr_params[:q]).to eq(unparseable_q)
end

it "ignores field with advanced_parse=false" do
solr_params = {}
original_q = "one two AND three OR four"
@obj.add_advanced_parse_q_to_solr(solr_params,
:search_field => "special_field",
:q => original_q
)

expect(solr_params).not_to have_key(:q)
end

end

end

0 comments on commit 02108a9

Please sign in to comment.