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.
-
#fetch(id = nil, extra_controller_params = {}) ⇒ Blacklight::SolrDocument
retrieve a document, given the doc id.
-
#initialize(config:, search_state: nil, user_params: nil, search_builder_class: config.search_builder_class, **context) ⇒ SearchService
constructor
A new instance of SearchService.
-
#opensearch_response(field = nil, extra_controller_params = {}) ⇒ Object
a solr query method does a standard search but returns a simplified object.
-
#previous_and_next_documents_for_search(index, request_params, 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
a solr query method.
- #search_state_class ⇒ Object
Constructor Details
#initialize(config:, search_state: nil, user_params: nil, search_builder_class: config.search_builder_class, **context) ⇒ SearchService
Returns a new instance of SearchService.
6 7 8 9 10 11 12 |
# File 'app/services/blacklight/search_service.rb', line 6 def initialize(config:, search_state: nil, user_params: nil, search_builder_class: config.search_builder_class, **context) @blacklight_config = config @search_state = search_state || Blacklight::SearchState.new(user_params || {}, config) @user_params = @search_state.params @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
15 16 17 |
# File 'app/services/blacklight/search_service.rb', line 15 def blacklight_config @blacklight_config end |
#context ⇒ Object (readonly)
The blacklight_config + controller are accessed by the search_builder
15 16 17 |
# File 'app/services/blacklight/search_service.rb', line 15 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
60 61 62 63 |
# File 'app/services/blacklight/search_service.rb', line 60 def facet_field_response(facet_field, extra_controller_params = {}) query = search_builder.with(search_state).facet(facet_field) repository.search(query.merge(extra_controller_params)) end |
#fetch(id = nil, extra_controller_params = {}) ⇒ Blacklight::SolrDocument
retrieve a document, given the doc id
49 50 51 52 53 54 55 |
# File 'app/services/blacklight/search_service.rb', line 49 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.
86 87 88 89 90 91 92 93 |
# File 'app/services/blacklight/search_service.rb', line 86 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(query) [search_state.query_param, response.documents.flat_map { |doc| doc[field] }.uniq] end |
#previous_and_next_documents_for_search(index, request_params, extra_controller_params = {}) ⇒ Blacklight::Solr::Response, Array<Blacklight::SolrDocument>
Get the previous and next document from a search result
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/services/blacklight/search_service.rb', line 67 def previous_and_next_documents_for_search(index, request_params, extra_controller_params = {}) p = previous_and_next_document_params(index) 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 next_doc = document_list.last if (index + 1) < response.total [response, [prev_doc, next_doc]] end |
#search_builder ⇒ Object
17 18 19 |
# File 'app/services/blacklight/search_service.rb', line 17 def search_builder search_builder_class.new(self) end |
#search_results {|search_builder| ... } ⇒ Blacklight::Solr::Response
a solr query method
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/services/blacklight/search_service.rb', line 28 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(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 |
#search_state_class ⇒ Object
21 22 23 |
# File 'app/services/blacklight/search_service.rb', line 21 def search_state_class @search_state.class end |