Skip to content
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
12 changes: 7 additions & 5 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ def facet
@facet = blacklight_config.facet_fields[params[:id]]
raise ActionController::RoutingError, 'Not Found' unless @facet

@response = if params[:query_fragment].present?
search_service.facet_suggest_response(@facet.key, params[:query_fragment])
else
search_service.facet_field_response(@facet.key)
end
builder = search_builder.with(search_state).facet(@facet.key)
if params[:query_fragment].present?
builder = builder.facet_suggestion_query(params[:query_fragment])
end

@response = retrieve_search_results(params: builder)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This conflates search results (documents) from facet results. Can we keep these two concerns separated?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is there any value in doing that? I find the current state incredibly confusing and it's not clear to me what behavior the seam is intending to provide that isn't better suited for the search builder.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that it is confusing, and having one method handle the one case makes it less so.


# @display_facet is a Blacklight::Solr::Response::Facets::FacetField
@display_facet = @response.aggregations[@facet.field]

Expand Down
16 changes: 14 additions & 2 deletions app/controllers/concerns/blacklight/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def search_service

# This method may be overridden to customize search behavior.
# @return [Blacklight::Solr::Response] the solr response object
def retrieve_search_results
search_service.search_results
def retrieve_search_results(params: nil)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we change this kwarg to builder since we're always passing a SearchBuilder here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm just matching the argument name to the repository for consistency.

search_service.search_results(params: params || search_builder.with(search_state).rows(search_state.per_page).page(search_state.page))
end

# This method may be overridden to customize search behavior.
Expand All @@ -42,6 +42,18 @@ def retrieve_documents(ids)
search_service.fetch(Array(ids))
end

# @return [Blacklight::SearchBuilder]
def search_builder
klass = blacklight_config.search_builder_class

if klass.initialize_supports_blacklight_config_parameter?
klass.new(self, blacklight_config: blacklight_config)
else
# deprecated behavior for implementations that don't support the new initializer signature.
klass.new(self)
end
end

# Override this method on the class that includes Blacklight::Searchable to provide more context to the search service if necessary.
# For example, if your search builder needs to be aware of the current user, override this method to return a hash including the current user.
# Then the search builder could use some property about the current user to construct a constraint on the search.
Expand Down
2 changes: 1 addition & 1 deletion app/services/blacklight/bookmarks_search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BookmarksSearchBuilder < ::SearchBuilder
# @return [void]
def bookmarked(solr_parameters)
solr_parameters[:fq] ||= []
bookmarks = @scope.context.fetch(:bookmarks)
bookmarks = @scope.search_service_context.fetch(:bookmarks)
return unless bookmarks

document_ids = bookmarks.collect { |b| b.document_id.to_s }
Expand Down
20 changes: 11 additions & 9 deletions app/services/blacklight/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class SearchService
# @params [Blacklight::SearchState] search_state
# @params [Class] search_builder_class a class that inherits from Blacklight::SearchBuilder
# @params [Hash] context any data the search builder needs to access. For example, the current user.
def initialize(config:, search_state:, search_builder_class: config.search_builder_class, **context)
def initialize(config:, search_state: nil, search_builder_class: config.search_builder_class, **context)
@blacklight_config = config
@search_state = search_state
@user_params = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(@search_state.params,
@user_params = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(@search_state&.params || {},
'Use @search_state.params instead of @user_params',
Blacklight.deprecation)
@search_builder_class = search_builder_class
Expand All @@ -29,13 +29,13 @@ def search_builder
# SearchBuilder to be used. Block should return SearchBuilder to be used.
# This is used in blacklight_range_limit
# @return [Blacklight::Solr::Response] the solr response object
def search_results
builder = search_builder.with(search_state)
builder.page = search_state.page
builder.rows = search_state.per_page
def search_results(params: nil)
unless params
builder = search_builder.with(search_state).page(search_state&.page).rows(search_state&.per_page)
builder = yield(builder) if block_given?
end

builder = yield(builder) if block_given?
response = repository.search(params: builder)
response = repository.search(params: params || builder)

if response.grouped? && grouped_key_for_results
response.group(grouped_key_for_results)
Expand All @@ -60,12 +60,14 @@ def fetch(id = nil, extra_controller_params = {})

##
# Get the solr response when retrieving only a single facet field
# @deprecated
# @return [Blacklight::Solr::Response] the solr response
def facet_field_response(facet_field, extra_controller_params = {})
query = search_builder.with(search_state).facet(facet_field)
repository.search(params: query.merge(extra_controller_params))
end

# @deprecated
def facet_suggest_response(facet_field, facet_suggestion_query, extra_controller_params = {})
query = search_builder.with(search_state).facet(facet_field).facet_suggestion_query(facet_suggestion_query)
repository.search(params: query.merge(extra_controller_params))
Expand Down Expand Up @@ -112,7 +114,7 @@ def opensearch_response(field = nil, extra_controller_params = {})
query = search_builder.with(search_state).merge(solr_opensearch_params(field)).merge(extra_controller_params)
response = repository.search(params: query)

[search_state.query_param, response.documents.flat_map { |doc| doc[field] }.uniq]
[search_state&.query_param, response.documents.flat_map { |doc| doc[field] }.uniq]
end
Blacklight.deprecation.deprecate_methods Blacklight::SearchService, opensearch_response: "The opensearch_response method is deprecated without replacement."

Expand Down
8 changes: 8 additions & 0 deletions lib/blacklight/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def initialize(*options, blacklight_config: nil)
@reverse_merged_params = {}
end

def self.initialize_supports_blacklight_config_parameter?
return @initialize_supports_blacklight_config_parameter if defined?(@initialize_supports_blacklight_config_parameter)

@initialize_supports_blacklight_config_parameter = instance_method(:initialize).parameters.any? { |_type, name| name == :blacklight_config }

Blacklight.deprecation.warn "#{self} initializer should accept a blacklight_config keyword argument in its initializer. This will be required in Blacklight 10."
end

def search_state
@search_state ||= begin
search_state_class = @scope.try(:search_state_class) || Blacklight::SearchState
Expand Down
8 changes: 8 additions & 0 deletions spec/services/blacklight/search_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
expect(solr_response.docs).to have(0).results
end
end

describe 'when passing in the params' do
it 'returns the results' do
solr_response = service.search_results(params: { q: '', rows: 5 })

expect(solr_response.docs).to have(5).results
end
end
end # Search Results

# SPECS FOR SEARCH RESULTS FOR FACETS
Expand Down
Loading