Class: Blacklight::SearchService
- Inherits:
-
Object
- Object
- Blacklight::SearchService
- Defined in:
- app/services/blacklight/search_service.rb
Instance Attribute Summary collapse
-
#blacklight_config ⇒ Object
readonly
The blacklight_config + controller are accessed by the search_builder.
-
#context ⇒ Object
readonly
The blacklight_config + controller are accessed by the search_builder.
Instance Method Summary collapse
-
#facet_field_response(facet_field, extra_controller_params = {}) ⇒ Blacklight::Solr::Response
Get the solr response when retrieving only a single facet field.
- #facet_suggest_response(facet_field, facet_suggestion_query, extra_controller_params = {}) ⇒ Object
-
#fetch(id = nil, extra_controller_params = {}) ⇒ Blacklight::SolrDocument
retrieve a document, given the doc id.
-
#initialize(config:, search_state:, search_builder_class: config.search_builder_class, **context) ⇒ SearchService
constructor
A new instance of SearchService.
- #opensearch_response(field = nil, extra_controller_params = {}) ⇒ Object deprecated Deprecated.
-
#previous_and_next_documents_for_search(index, request_params, params: nil, **extra_controller_params) ⇒ Blacklight::Solr::Response, Array<Blacklight::SolrDocument>
Get the previous and next document from a search result.
- #search_builder ⇒ Object
-
#search_results {|search_builder| ... } ⇒ Blacklight::Solr::Response
Fetch query results from solr.
Constructor Details
#initialize(config:, search_state:, search_builder_class: config.search_builder_class, **context) ⇒ SearchService
Returns a new instance of SearchService.
10 11 12 13 14 15 16 17 18 |
# File 'app/services/blacklight/search_service.rb', line 10 def initialize(config:, search_state:, search_builder_class: config.search_builder_class, **context) @blacklight_config = config @search_state = search_state @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 @context = context end |
Instance Attribute Details
#blacklight_config ⇒ Object (readonly)
The blacklight_config + controller are accessed by the search_builder
21 22 23 |
# File 'app/services/blacklight/search_service.rb', line 21 def blacklight_config @blacklight_config end |
#context ⇒ Object (readonly)
The blacklight_config + controller are accessed by the search_builder
21 22 23 |
# File 'app/services/blacklight/search_service.rb', line 21 def context @context end |
Instance Method Details
#facet_field_response(facet_field, extra_controller_params = {}) ⇒ Blacklight::Solr::Response
Get the solr response when retrieving only a single facet field
64 65 66 67 |
# File 'app/services/blacklight/search_service.rb', line 64 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 |
#facet_suggest_response(facet_field, facet_suggestion_query, extra_controller_params = {}) ⇒ Object
69 70 71 72 |
# File 'app/services/blacklight/search_service.rb', line 69 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)) end |
#fetch(id = nil, extra_controller_params = {}) ⇒ Blacklight::SolrDocument
retrieve a document, given the doc id
53 54 55 56 57 58 59 |
# File 'app/services/blacklight/search_service.rb', line 53 def fetch(id = nil, extra_controller_params = {}) if id.is_a? Array fetch_many(id, extra_controller_params) else fetch_one(id, extra_controller_params) end end |
#opensearch_response(field = nil, extra_controller_params = {}) ⇒ Object
a solr query method does a standard search but returns a simplified object. an array is returned, the first item is the query string, the second item is an other array. This second array contains all of the field values for each of the documents... where the field is the "field" argument passed in.
109 110 111 112 113 114 115 116 |
# File 'app/services/blacklight/search_service.rb', line 109 def opensearch_response(field = nil, extra_controller_params = {}) field ||= blacklight_config.view_config(:opensearch).title_field 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] end |
#previous_and_next_documents_for_search(index, request_params, params: nil, **extra_controller_params) ⇒ Blacklight::Solr::Response, Array<Blacklight::SolrDocument>
Get the previous and next document from a search result
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/services/blacklight/search_service.rb', line 76 def previous_and_next_documents_for_search(index, request_params, params: nil, **extra_controller_params) unless params new_state = request_params.is_a?(Blacklight::SearchState) ? request_params : Blacklight::SearchState.new(request_params, blacklight_config) builder = search_builder.with(new_state) search_service_params = Blacklight.deprecation.silence do previous_and_next_document_params(index) end builder = if search_service_params.empty? builder.for_previous_and_next_documents(index).merge(extra_controller_params) else Blacklight.deprecation.warn("SearchService#previous_and_next_document_params returned parameters. Implement customizations in SearchBuilder#for_previous_and_next_document instead") builder.start(search_service_params.delete(:start)).rows(search_service_params.delete(:rows)).merge(extra_controller_params).merge(search_service_params) end end response = repository.search(params: params || builder) document_list = response.documents # only get the previous doc if there is one prev_doc = document_list.first if index > 0 next_doc = document_list.last if (index + 1) < response.total [response, [prev_doc, next_doc]] end |
#search_builder ⇒ Object
23 24 25 |
# File 'app/services/blacklight/search_service.rb', line 23 def search_builder search_builder_class.new(self, blacklight_config: blacklight_config) end |
#search_results {|search_builder| ... } ⇒ Blacklight::Solr::Response
Fetch query results from solr
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/blacklight/search_service.rb', line 32 def search_results 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(params: builder) if response.grouped? && grouped_key_for_results response.group(grouped_key_for_results) elsif response.grouped? && response.grouped.length == 1 response.grouped.first else response end end |