Skip to content

Commit

Permalink
Rename solr_repository to repository to support multiple backends
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Feb 12, 2015
1 parent 67c6c17 commit 482ed97
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
43 changes: 27 additions & 16 deletions lib/blacklight/solr_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def find *args

solr_params[:qt] ||= blacklight_config.qt

solr_repository.send_and_receive path, solr_params
repository.send_and_receive path, solr_params
end
deprecation_deprecate :find

Expand All @@ -80,7 +80,7 @@ def solr_doc_params(id=nil)
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")
query = search_builder(user_params).query(extra_controller_params)
solr_response = solr_repository.search(query)
solr_response = repository.search(query)

case
when (solr_response.grouped? && grouped_key_for_results)
Expand All @@ -99,7 +99,7 @@ def get_search_results(user_params = params || {}, extra_controller_params = {})
# @return [Blacklight::SolrResponse] the solr response object
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)
solr_response = repository.search(query)

case
when (solr_response.grouped? && grouped_key_for_results)
Expand All @@ -118,7 +118,7 @@ def search_results(user_params, solr_search_params_logic)
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)
repository.search(query)
end

# a solr query method
Expand All @@ -139,7 +139,7 @@ def get_solr_response_for_doc_id(id=nil, extra_controller_params={})
extra_controller_params = extra_controller_params.merge(old_solr_doc_params)
end

solr_response = solr_repository.find id, extra_controller_params
solr_response = repository.find id, extra_controller_params
[solr_response, solr_response.documents.first]
end

Expand All @@ -159,7 +159,7 @@ def get_solr_response_for_document_ids(ids=[], *args)
end

query = search_builder(user_params).query(extra_controller_params.merge(solr_document_ids_params(ids)))
solr_response = solr_repository.search(query)
solr_response = repository.search(query)

[solr_response, solr_response.documents]
end
Expand All @@ -168,7 +168,7 @@ def get_solr_response_for_document_ids(ids=[], *args)
# @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 = {})
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 = repository.search(query)


[solr_response, solr_response.documents]
Expand All @@ -180,7 +180,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 = {})
query = search_builder(user_params).query(extra_controller_params.merge(solr_facet_params(facet_field, user_params, extra_controller_params)))
solr_repository.search(query)
repository.search(query)
end

# a solr query method
Expand Down Expand Up @@ -211,10 +211,12 @@ 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)
# 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_params = search_builder(request_params).processed_parameters

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 = repository.search(solr_params)
solr_response.documents.first
end
deprecation_deprecate :get_single_doc_via_search
Expand All @@ -224,7 +226,7 @@ def get_single_doc_via_search(index, request_params)
def get_previous_and_next_documents_for_search(index, request_params, extra_controller_params={})

query = search_builder(request_params).query(extra_controller_params.merge(previous_and_next_document_params(index)))
solr_response = solr_repository.search(query)
solr_response = repository.search(query)

document_list = solr_response.documents

Expand All @@ -245,7 +247,7 @@ def get_opensearch_response(field=nil, request_params = params || {}, extra_cont
field ||= blacklight_config.view_config('opensearch').title_field

query = search_builder(request_params).query(solr_opensearch_params(field).merge(extra_controller_params))
response = solr_repository.search(query)
response = repository.search(query)

[response.params[:q], response.documents.flat_map {|doc| doc[field] }.uniq]
end
Expand All @@ -260,12 +262,21 @@ def grouped_key_for_results
blacklight_config.index.group
end

def repository_class
Blacklight::SolrRepository
end

def repository
@repository ||= repository_class.new(blacklight_config)
end

def solr_repository
@solr_repository ||= Blacklight::SolrRepository.new(blacklight_config)
repository
end
deprecation_deprecate :solr_repository

def blacklight_solr
solr_repository.blacklight_solr
repository.blacklight_solr
end
deprecation_deprecate :blacklight_solr

Expand Down
12 changes: 6 additions & 6 deletions spec/lib/blacklight/solr_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class SolrHelperTestClass
include Blacklight::SolrHelper

attr_accessor :blacklight_config
attr_accessor :solr_repository
attr_accessor :repository

def initialize blacklight_config, blacklight_solr
self.blacklight_config = blacklight_config
self.solr_repository = Blacklight::SolrRepository.new(blacklight_config)
self.solr_repository.blacklight_solr = blacklight_solr
self.repository = Blacklight::SolrRepository.new(blacklight_config)
self.repository.blacklight_solr = blacklight_solr
end

def params
Expand Down Expand Up @@ -909,21 +909,21 @@ def params
end
it "should contruct a solr query based on the field and value pair" do
Deprecation.silence(Blacklight::SolrHelper) do
allow(subject.solr_repository).to receive(:send_and_receive).with('select', hash_including("q" => "{!lucene}field_name:(value)")).and_return(@mock_response)
allow(subject.repository).to receive(:send_and_receive).with('select', hash_including("q" => "{!lucene}field_name:(value)")).and_return(@mock_response)
subject.get_solr_response_for_field_values('field_name', 'value')
end
end

it "should OR multiple values together" do
Deprecation.silence(Blacklight::SolrHelper) do
allow(subject.solr_repository).to receive(:send_and_receive).with('select', hash_including("q" => "{!lucene}field_name:(a OR b)")).and_return(@mock_response)
allow(subject.repository).to receive(:send_and_receive).with('select', hash_including("q" => "{!lucene}field_name:(a OR b)")).and_return(@mock_response)
subject.get_solr_response_for_field_values('field_name', ['a', 'b'])
end
end

it "should escape crazy identifiers" do
Deprecation.silence(Blacklight::SolrHelper) do
allow(subject.solr_repository).to receive(:send_and_receive).with('select', hash_including("q" => "{!lucene}field_name:(\"h://\\\"\\\'\")")).and_return(@mock_response)
allow(subject.repository).to receive(:send_and_receive).with('select', hash_including("q" => "{!lucene}field_name:(\"h://\\\"\\\'\")")).and_return(@mock_response)
subject.get_solr_response_for_field_values('field_name', 'h://"\'')
end
end
Expand Down

0 comments on commit 482ed97

Please sign in to comment.