app/services/blacklight/search_service.rb in blacklight-7.12.1 vs app/services/blacklight/search_service.rb in blacklight-7.13.0

- old
+ new

@@ -1,12 +1,13 @@ # frozen_string_literal: true # SearchService returns search results from the repository module Blacklight class SearchService - def initialize(config:, user_params: {}, search_builder_class: config.search_builder_class, **context) + def initialize(config:, search_state: nil, user_params: nil, search_builder_class: config.search_builder_class, **context) @blacklight_config = config - @user_params = user_params + @search_state = search_state || Blacklight::SearchState.new(user_params || {}, config) + @user_params = @search_state.params @search_builder_class = search_builder_class @context = context end # The blacklight_config + controller are accessed by the search_builder @@ -18,13 +19,13 @@ # a solr query method # @yield [search_builder] optional block yields configured SearchBuilder, caller can modify or create new SearchBuilder to be used. Block should return SearchBuilder to be used. # @return [Blacklight::Solr::Response] the solr response object def search_results - builder = search_builder.with(user_params) - builder.page = user_params[:page] if user_params[:page] - builder.rows = (user_params[:per_page] || user_params[:rows]) if user_params[:per_page] || user_params[:rows] + builder = search_builder.with(search_state) + builder.page = search_state.page + builder.rows = search_state.per_page builder = yield(builder) if block_given? response = repository.search(builder) if response.grouped? && grouped_key_for_results @@ -49,19 +50,20 @@ ## # Get the solr response when retrieving only a single facet field # @return [Blacklight::Solr::Response] the solr response def facet_field_response(facet_field, extra_controller_params = {}) - query = search_builder.with(user_params).facet(facet_field) + query = search_builder.with(search_state).facet(facet_field) repository.search(query.merge(extra_controller_params)) end # Get the previous and next document from a search result # @return [Blacklight::Solr::Response, Array<Blacklight::SolrDocument>] the solr response and a list of the first and last document def previous_and_next_documents_for_search(index, request_params, extra_controller_params = {}) p = previous_and_next_document_params(index) - query = search_builder.with(request_params).start(p.delete(:start)).rows(p.delete(:rows)).merge(extra_controller_params).merge(p) + new_state = request_params.is_a?(Blacklight::SearchState) ? request_params : Blacklight::SearchState.new(request_params, blacklight_config) + query = search_builder.with(new_state).start(p.delete(:start)).rows(p.delete(:rows)).merge(extra_controller_params).merge(p) response = repository.search(query) document_list = response.documents # only get the previous doc if there is one prev_doc = document_list.first if index > 0 @@ -76,19 +78,19 @@ # all of the field values for each of the documents... # where the field is the "field" argument passed in. def opensearch_response(field = nil, extra_controller_params = {}) field ||= blacklight_config.view_config(:opensearch).title_field - query = search_builder.with(user_params).merge(solr_opensearch_params(field)).merge(extra_controller_params) + query = search_builder.with(search_state).merge(solr_opensearch_params(field)).merge(extra_controller_params) response = repository.search(query) - [user_params[:q], response.documents.flat_map { |doc| doc[field] }.uniq] + [search_state.query_param, response.documents.flat_map { |doc| doc[field] }.uniq] end private - attr_reader :search_builder_class, :user_params + attr_reader :search_builder_class, :user_params, :search_state delegate :repository, to: :blacklight_config ## # The key to use to retrieve the grouped field to display @@ -133,10 +135,10 @@ # @param [HashWithIndifferentAccess] extra_controller_params def fetch_many(ids, extra_controller_params) extra_controller_params ||= {} query = search_builder - .with(user_params) + .with(search_state) .where(blacklight_config.document_model.unique_key => ids) .merge(blacklight_config.fetch_many_document_params) .merge(extra_controller_params) solr_response = repository.search(query)