Skip to content

Commit

Permalink
Add a raw solr endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 31, 2018
1 parent 2a23401 commit c881900
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Metrics/MethodLength:
# Offense count: 8
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 210
Max: 215

# Offense count: 1
# Configuration parameters: CountKeywordArgs.
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def show
end
end

# get a single document from the index
def raw
raise(ActionController::RoutingError, 'Not Found') unless blacklight_config.raw_endpoint.enabled
_, @document = search_service.fetch(params[:id])
render json: @document
end

# updates the search counter (allows the show view to paginate)
def track
search_session['counter'] = params[:counter]
Expand Down
3 changes: 2 additions & 1 deletion lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def default_values
# proc for determining whether the session is a crawler/bot
# ex.: crawler_detector: lambda { |req| req.env['HTTP_USER_AGENT'] =~ /bot/ }
crawler_detector: nil,
autocomplete_suggester: 'mySuggester'
autocomplete_suggester: 'mySuggester',
raw_endpoint: OpenStructWithHashAccess.new(enabled: false)
}
end
# rubocop:enable Metrics/MethodLength
Expand Down
1 change: 1 addition & 0 deletions lib/blacklight/routes/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def call(mapper, _options = {})
mapper.match '/', action: 'index', as: 'search', via: [:get, :post]

mapper.post ":id/track", action: 'track', as: 'track'
mapper.get ":id/raw", action: 'raw', as: 'raw', defaults: { format: 'json' }

mapper.get "opensearch"
mapper.get 'suggest', as: 'suggest_index', defaults: { format: 'json' }
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/blacklight/templates/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class <%= controller_name.classify %>Controller < ApplicationController
#
## Model that maps search index responses to the blacklight response model
# config.response_model = Blacklight::Solr::Response
#
## Should the raw solr document endpoint (e.g. /catalog/:id/raw) be enabled
# config.raw_endpoint.enabled = false
## Default parameters to send to solr for all search-like requests. See also SearchBuilder#processed_parameters
config.default_solr_params = {
Expand Down
28 changes: 28 additions & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@
end
end

describe '#raw' do
context 'when disabled' do
it "returns 404" do
expect { get :raw, params: { id: doc_id, format: 'json' } }.to raise_error ActionController::RoutingError
end
end

context 'when enabled' do
before do
allow(controller.blacklight_config.raw_endpoint).to receive(:enabled).and_return(true)
end

it "gets the raw solr document" do
get :raw, params: { id: doc_id, format: 'json' }
expect(response).to be_successful
json = JSON.parse response.body
expect(json.keys).to match_array(
%w[id _version_ author_addl_tsim author_tsim format isbn_ssim
language_ssim lc_1letter_ssim lc_alpha_ssim lc_b4cutter_ssim
lc_callnum_ssim marc_ss material_type_ssim pub_date_ssim
published_ssim subject_addl_ssim subject_geo_ssim subject_ssim
subject_tsim subtitle_tsim timestamp title_addl_tsim title_tsim
url_suppl_ssim]
)
end
end
end

# SHOW ACTION
describe "show action" do
describe "with format :html" do
Expand Down

0 comments on commit c881900

Please sign in to comment.