Skip to content

Commit

Permalink
Rename get_search_results to search_results
Browse files Browse the repository at this point in the history
`search_results` lets you pass in the filter methods.  This is helpful
for when you have different methods in the same class (e.g. index, show)
both calling `search_results`, but with different filters to run.
  • Loading branch information
jcoyne committed Feb 5, 2015
1 parent a9db4e4 commit fd20951
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 147 deletions.
2 changes: 1 addition & 1 deletion lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Blacklight::Catalog

# get search results from the solr index
def index
(@response, @document_list) = get_search_results
(@response, @document_list) = search_results(params, {}, solr_search_params_logic)

respond_to do |format|
format.html { preferred_view }
Expand Down
14 changes: 11 additions & 3 deletions lib/blacklight/request_builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,22 @@ module RequestBuilders
# specified otherwise.
#
# Incoming parameter :f is mapped to :fq solr parameter.
def solr_search_params(user_params = params || {})
def solr_search_params(user_params = nil, processor_chain = nil)
unless user_params
Deprecation.warn(RequestBuilders, "Calling solr_search_params without a `user_params' argument is deprecated and will be removed in blacklight-6.0")
user_params = params || {}
end
unless processor_chain
Deprecation.warn(RequestBuilders, "Calling solr_search_params without a `processor_chain' argument is deprecated and will be removed in blacklight-6.0")
processor_chain = solr_search_params_logic
end
Blacklight::Solr::Request.new.tap do |solr_parameters|
solr_search_params_logic.each do |method_name|
processor_chain.each do |method_name|
send(method_name, solr_parameters, user_params)
end
end
end

##
# Retrieve the results for a list of document ids
def solr_document_ids_params(ids = [])
Expand Down
45 changes: 34 additions & 11 deletions lib/blacklight/solr_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,17 @@ def solr_doc_params(id=nil)
# Returns a two-element array (aka duple) with first the solr response object,
# and second an array of SolrDocuments representing the response.docs
def get_search_results(user_params = params || {}, extra_controller_params = {})
solr_response = query_solr(user_params, extra_controller_params)
Deprecation.warn(self, "get_search_results is deprecated and will be removed in blacklight-6.0. Use `search_results' instead")
search_results(user_params, extra_controller_params, solr_search_params_logic)
end

# a solr query method
# @param [Hash,HashWithIndifferentAccess] user_params ({}) the user provided parameters (e.g. query, facets, sort, etc)
# @param [Hash,HashWithIndifferentAccess] extra_controller_params ({}) extra parameters to add to the search
# @param [List<Symbol] processor_chain a list of filter methods to run
# @return [Blacklight::SolrResponse] the solr response object
def search_results(user_params, extra_controller_params, solr_search_parms_logic)
solr_response = query_solr(user_params, extra_controller_params, solr_search_params_logic)

case
when (solr_response.grouped? && grouped_key_for_results)
Expand All @@ -90,12 +100,25 @@ def get_search_results(user_params = params || {}, extra_controller_params = {})
end
end


# a solr query method
# given a user query,
# @param [Hash,HashWithIndifferentAccess] user_params ({}) the user provided parameters (e.g. query, facets, sort, etc)
# @param [Hash,HashWithIndifferentAccess] extra_controller_params ({}) extra parameters to add to the search
# @param [List<Symbol] processor_chain a list of filter methods to run
# @return [Blacklight::SolrResponse] the solr response object
def query_solr(user_params = params || {}, extra_controller_params = {})
solr_params = self.solr_search_params(user_params).merge(extra_controller_params)
def query_solr(user_params = nil, extra_controller_params = nil, processor_chain = nil)
unless user_params
Deprecation.warn(self, "Calling query_solr without a `user_params' argument is deprecated and will be removed in blacklight-6.0")
user_params = params || {}
end
unless extra_controller_params
Deprecation.warn(self, "Calling query_solr without a `extra_controller_params' argument is deprecated and will be removed in blacklight-6.0")
extra_controller_params = {}
end
unless processor_chain
Deprecation.warn(self, "Calling query_solr without a `processor_chain' argument is deprecated and will be removed in blacklight-6.0")
processor_chain = solr_search_params_logic
end
solr_params = solr_search_params(user_params, processor_chain).merge(extra_controller_params)

solr_repository.search(solr_params)
end
Expand Down Expand Up @@ -137,15 +160,15 @@ def get_solr_response_for_document_ids(ids=[], *args)
extra_controller_params ||= {}
end

solr_response = query_solr(user_params, extra_controller_params.merge(solr_document_ids_params(ids)))
solr_response = query_solr(user_params, extra_controller_params.merge(solr_document_ids_params(ids)), solr_search_params_logic)

[solr_response, solr_response.documents]
end

# given a field name and array of values, get the matching SOLR documents
# @return [Blacklight::SolrResponse, Array<Blacklight::SolrDocument>] the solr response object and a list of solr documents
def get_solr_response_for_field_values(field, values, extra_controller_params = {})
solr_response = query_solr(params, extra_controller_params.merge(solr_documents_by_field_values_params(field, values)))
solr_response = query_solr(params, extra_controller_params.merge(solr_documents_by_field_values_params(field, values)), solr_search_params_logic)

[solr_response, solr_response.documents]
end
Expand All @@ -156,7 +179,7 @@ def get_solr_response_for_field_values(field, values, extra_controller_params =
# @return [Blacklight::SolrResponse] the solr response
def get_facet_field_response(facet_field, user_params = params || {}, extra_controller_params = {})
solr_params = solr_facet_params(facet_field, user_params, extra_controller_params)
query_solr(user_params, extra_controller_params.merge(solr_facet_params(facet_field, user_params, extra_controller_params)))
query_solr(user_params, extra_controller_params.merge(solr_facet_params(facet_field, user_params, extra_controller_params)), solr_search_params_logic)
end

# a solr query method
Expand Down Expand Up @@ -187,7 +210,7 @@ def get_facet_pagination(facet_field, user_params=params || {}, extra_controller
# the Blacklight app-level request params that define the search.
# @return [Blacklight::SolrDocument, nil] the found document or nil if not found
def get_single_doc_via_search(index, request_params)
solr_params = solr_search_params(request_params)
solr_params = solr_search_params(request_params, solr_search_params_logic)

solr_params[:start] = (index - 1) # start at 0 to get 1st doc, 1 to get 2nd.
solr_params[:rows] = 1
Expand All @@ -201,7 +224,7 @@ def get_single_doc_via_search(index, request_params)
# @return [Blacklight::SolrResponse, Array<Blacklight::SolrDocument>] the solr response and a list of the first and last document
def get_previous_and_next_documents_for_search(index, request_params, extra_controller_params={})

solr_response = query_solr(request_params, extra_controller_params.merge(previous_and_next_document_params(index)))
solr_response = query_solr(request_params, extra_controller_params.merge(previous_and_next_document_params(index)), solr_search_params_logic)

document_list = solr_response.documents

Expand All @@ -221,7 +244,7 @@ def get_previous_and_next_documents_for_search(index, request_params, extra_cont
def get_opensearch_response(field=nil, request_params = params || {}, extra_controller_params={})
field ||= blacklight_config.view_config('opensearch').title_field

response = query_solr(request_params, solr_opensearch_params(field).merge(extra_controller_params))
response = query_solr(request_params, solr_opensearch_params(field).merge(extra_controller_params), solr_search_params_logic)

[response.params[:q], response.documents.flat_map {|doc| doc[field] }.uniq]
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ def export_as_mock
it "should redirect the user to the root url for a bad search" do
req = {}
res = {}
fake_error = RSolr::Error::Http.new(req, res)
fake_error = RSolr::Error::Http.new(req, res)
allow(Rails.env).to receive_messages(:test? => false)
allow(controller).to receive(:get_search_results) { |*args| raise fake_error }
allow(controller).to receive(:search_results) { |*args| raise fake_error }
expect(controller.logger).to receive(:error).with(fake_error)
get :index, :q=>"+"

Expand Down

0 comments on commit fd20951

Please sign in to comment.