8000 Extract a search_builder for a find one search by jcoyne · Pull Request #3633 · projectblacklight/blacklight · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Extract a search_builder for a find one search #3633

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions lib/blacklight/solr/field_reflection_search_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Blacklight::Solr
class FieldReflectionSearchBuilder < SearchBuilder
self.default_processor_chain = [:add_params]

def add_params(request)
request.reverse_merge!({ fl: '*', 'json.nl' => 'map' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like all the other request keys are strings; is this intended to be a symbol for fl?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just moved from Solr::Repository#reflect_fields. I was not trying to change what was submitted.

end
end
end
9 changes: 4 additions & 5 deletions lib/blacklight/solr/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class Repository < Blacklight::AbstractRepository
# @param [String] id document's unique key value
# @param [Hash] params additional solr query parameters
def find id, params = {}
doc_params = params.reverse_merge(blacklight_config.default_document_solr_params)
.reverse_merge(qt: blacklight_config.document_solr_request_handler)
.merge(blacklight_config.document_unique_id_param => id)
doc_params = SingleDocSearchBuilder.new(self, id, params)

solr_response = send_and_receive blacklight_config.document_solr_path || blacklight_config.solr_path, doc_params
raise Blacklight::Exceptions::RecordNotFound if solr_response.documents.empty?
Expand All @@ -25,7 +23,7 @@ def find_many(params)

##
# Execute a search query against solr
# @param [Hash] params solr query parameters
# @param [Hash,Blacklight::SearchBuilder] params solr query parameters
# @param [String] path solr request handler path
def search pos_params = nil, path: nil, params: nil, **kwargs
if pos_params
Expand All @@ -48,7 +46,8 @@ def suggestions(request_params)
# Gets a list of available fields
# @return [Hash]
def reflect_fields
send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
doc_params = FieldReflectionSearchBuilder.new(self)
send_and_receive('admin/luke', doc_params)['fields']
end

##
Expand Down
25 changes: 25 additions & 0 deletions lib/blacklight/solr/single_doc_search_builder.rb
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Blacklight::Solr
class SingleDocSearchBuilder < SearchBuilder
self.default_processor_chain = [:add_defaults, :add_qt, :add_unique_id]

def initialize(scope, id, other_params)
@other_params = other_params
@id = id
super(scope)
end

def add_defaults(request)
request.reverse_merge!(blacklight_config.default_document_solr_params).reverse_merge!(@other_params)
end

def add_qt(request)
request[:qt] ||= blacklight_config.document_solr_request_handler if blacklight_config.document_solr_request_handler
end

def add_unique_id(request)
request[blacklight_config.document_unique_id_param] = @id
end
end
end
4 changes: 2 additions & 2 deletions spec/models/blacklight/solr/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@

it "uses the provided :qt param" do
blacklight_config.document_solr_request_handler = 'xyz'
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { id: '123', qt: 'abc' })).and_return(mock_response)
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { 'id' => '123', 'qt' => 'abc' })).and_return(mock_response)
expect(subject.find("123", qt: 'abc')).to be_a Blacklight::Solr::Response
end

it "uses the :qt parameter from the default_document_solr_params" do
blacklight_config.default_document_solr_params[:qt] = 'abc'
blacklight_config.document_solr_request_handler = 'xyz'
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { id: '123', qt: 'abc' })).and_return(mock_response)
allow(subject.connection).to receive(:send_and_receive).with('select', hash_including(params: { 'id' => '123', 'qt' => 'abc' })).and_return(mock_response)
expect(subject.find("123")).to be_a Blacklight::Solr::Response
end
end
Expand Down
0