Skip to content

Commit

Permalink
Extract SearchBuilder from SolrHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Feb 6, 2015
1 parent fd20951 commit 2af0cbe
Show file tree
Hide file tree
Showing 9 changed files with 619 additions and 526 deletions.
1 change: 1 addition & 0 deletions lib/blacklight.rb
Expand Up @@ -6,6 +6,7 @@ module Blacklight
autoload :Configurable, 'blacklight/configurable'
autoload :Configuration, 'blacklight/configuration'
autoload :SearchFields, 'blacklight/search_fields'
autoload :SearchBuilder, 'blacklight/search_builder'

autoload :Solr, 'blacklight/solr'

Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/catalog.rb
Expand Up @@ -30,7 +30,7 @@ module Blacklight::Catalog

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

respond_to do |format|
format.html { preferred_view }
Expand Down
28 changes: 14 additions & 14 deletions lib/blacklight/request_builders.rb
Expand Up @@ -45,20 +45,20 @@ module RequestBuilders
# specified otherwise.
#
# Incoming parameter :f is mapped to :fq solr parameter.
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|
processor_chain.each do |method_name|
send(method_name, solr_parameters, user_params)
end
end
def solr_search_params(user_params = params || {}, processor_chain = solr_search_params_logic)
Deprecation.warn(RequestBuilders, "solr_search_params is deprecated and will be removed in blacklight-6.0. Use SearchBuilder#processed_parameters instead.")

Blacklight::SearchBuilder.new(user_params, processor_chain, self).processed_parameters
end

##
# @param [Hash] user_params a hash of user submitted parameters
# @param [Array] processor_chain a list of processor methods to run
# @param [Hash] extra_params an optional hash of parameters that should be
# added to the query post processing
def build_solr_query(user_params, processor_chain, extra_params=nil)
Deprecation.warn(RequestBuilders, "build_solr_query is deprecated and will be removed in blacklight-6.0. Use SearchBuilder#query instead")
Blacklight::SearchBuilder.new(user_params, processor_chain, self).query(extra_params)
end

##
Expand Down
43 changes: 43 additions & 0 deletions lib/blacklight/search_builder.rb
@@ -0,0 +1,43 @@
module Blacklight
class SearchBuilder
# @param [Hash,HashWithIndifferentAccess] user_params the user provided parameters (e.g. query, facets, sort, etc)
# @param [List<Symbol>] processor_chain a list of filter methods to run
# @param [Object] scope the scope where the filter methods reside in.
def initialize(user_params, processor_chain, scope)
@user_params = user_params
@processor_chain = processor_chain
@scope = scope
end

# a solr query method
# @param [Hash,HashWithIndifferentAccess] extra_controller_params (nil) extra parameters to add to the search
# @return [Blacklight::SolrResponse] the solr response object
def query(extra_params = nil)
extra_params ? processed_parameters.merge(extra_params) : processed_parameters
end

# @returns a params hash for searching solr.
# The CatalogController #index action uses this.
# Solr parameters can come from a number of places. From lowest
# precedence to highest:
# 1. General defaults in blacklight config (are trumped by)
# 2. defaults for the particular search field identified by params[:search_field] (are trumped by)
# 3. certain parameters directly on input HTTP query params
# * not just any parameter is grabbed willy nilly, only certain ones are allowed by HTTP input)
# * for legacy reasons, qt in http query does not over-ride qt in search field definition default.
# 4. extra parameters passed in as argument.
#
# spellcheck.q will be supplied with the [:q] value unless specifically
# specified otherwise.
#
# Incoming parameter :f is mapped to :fq solr parameter.
def processed_parameters
Blacklight::Solr::Request.new.tap do |solr_parameters|
@processor_chain.each do |method_name|
@scope.send(method_name, solr_parameters, @user_params)
end
end
end

end
end
71 changes: 38 additions & 33 deletions lib/blacklight/solr_helper.rb
Expand Up @@ -79,16 +79,27 @@ def solr_doc_params(id=nil)
# and second an array of SolrDocuments representing the response.docs
def get_search_results(user_params = 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)
query = search_builder(user_params).query(extra_controller_params)
solr_response = solr_repository.search(query)

case
when (solr_response.grouped? && grouped_key_for_results)
[solr_response.group(grouped_key_for_results), []]
when (solr_response.grouped? && solr_response.grouped.length == 1)
[solr_response.grouped.first, []]
else
[solr_response, solr_response.documents]
end
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)
def search_results(user_params, solr_search_params_logic)
query = search_builder(user_params, solr_search_params_logic).query
solr_response = solr_repository.search(query)

case
when (solr_response.grouped? && grouped_key_for_results)
Expand All @@ -103,24 +114,11 @@ def search_results(user_params, extra_controller_params, solr_search_parms_logic
# 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 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)
def query_solr(user_params = params || {}, extra_controller_params = {})
Deprecation.warn(self, "query_solr is deprecated and will be removed in blacklight-6.0")
query = search_builder(user_params).query(extra_controller_params)
solr_repository.search(query)
end

# a solr query method
Expand Down Expand Up @@ -160,15 +158,18 @@ 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_search_params_logic)
query = search_builder(user_params).query(extra_controller_params.merge(solr_document_ids_params(ids)))
solr_response = solr_repository.search(query)

[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_search_params_logic)
query = search_builder(params).query(extra_controller_params.merge(solr_documents_by_field_values_params(field, values)))
solr_response = solr_repository.search(query)


[solr_response, solr_response.documents]
end
Expand All @@ -178,8 +179,8 @@ def get_solr_response_for_field_values(field, values, extra_controller_params =
# Get the solr response when retrieving only a single facet field
# @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)), solr_search_params_logic)
query = search_builder(user_params).query(extra_controller_params.merge(solr_facet_params(facet_field, user_params, extra_controller_params)))
solr_repository.search(query)
end

# a solr query method
Expand All @@ -195,7 +196,7 @@ def get_facet_pagination(facet_field, user_params=params || {}, extra_controller
# NOTE: The sniffing of the proper sort from the solr response is not
# currently tested for, tricky to figure out how to test, since the
# default setup we test against doesn't use this feature.
return Blacklight::Solr::FacetPaginator.new(response.facets.first.items,
Blacklight::Solr::FacetPaginator.new(response.facets.first.items,
:offset => response.params[:"f.#{facet_field}.facet.offset"],
:limit => limit,
:sort => response.params[:"f.#{facet_field}.facet.sort"] || response.params["facet.sort"]
Expand All @@ -210,12 +211,10 @@ 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_search_params_logic)

solr_params[:start] = (index - 1) # start at 0 to get 1st doc, 1 to get 2nd.
solr_params[:rows] = 1
solr_params[:fl] = '*'
solr_response = solr_repository.search(solr_params)
# start at 0 to get 1st doc, 1 to get 2nd.
extra_params = { start: (index - 1), rows: 1, fl: '*' }
query = search_builder(request_params).query(extra_params)
solr_response = solr_repository.search(query)
solr_response.documents.first
end
deprecation_deprecate :get_single_doc_via_search
Expand All @@ -224,7 +223,8 @@ 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_search_params_logic)
query = search_builder(request_params).query(extra_controller_params.merge(previous_and_next_document_params(index)))
solr_response = solr_repository.search(query)

document_list = solr_response.documents

Expand All @@ -244,11 +244,16 @@ 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), solr_search_params_logic)
query = search_builder(request_params).query(solr_opensearch_params(field).merge(extra_controller_params))
response = solr_repository.search(query)

[response.params[:q], response.documents.flat_map {|doc| doc[field] }.uniq]
end

def search_builder(user_params, processor_chain = solr_search_params_logic)
@search_builder ||= Blacklight::SearchBuilder.new(user_params, processor_chain, self)
end

##
# The key to use to retrieve the grouped field to display
def grouped_key_for_results
Expand Down

0 comments on commit 2af0cbe

Please sign in to comment.