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::Solr::Response, 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.
5 6 7 8 9 10 11 |
# File 'app/services/blacklight/search_service.rb', line 5 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
14 15 16 |
# File 'app/services/blacklight/search_service.rb', line 14 def blacklight_config @blacklight_config end |
#context ⇒ Object (readonly)
The blacklight_config + controller are accessed by the search_builder
14 15 16 |
# File 'app/services/blacklight/search_service.rb', line 14 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
58 59 60 61 |
# File 'app/services/blacklight/search_service.rb', line 58 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::Solr::Response, Blacklight::SolrDocument
retrieve a document, given the doc id
47 48 49 50 51 52 53 |
# File 'app/services/blacklight/search_service.rb', line 47 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.
84 85 86 87 88 89 90 91 |
# File 'app/services/blacklight/search_service.rb', line 84 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
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/services/blacklight/search_service.rb', line 65 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
16 17 18 |
# File 'app/services/blacklight/search_service.rb', line 16 def search_builder search_builder_class.new(self) end |
#search_results {|search_builder| ... } ⇒ Blacklight::Solr::Response
a solr query method
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/services/blacklight/search_service.rb', line 27 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, response.documents] end end |
#search_state_class ⇒ Object
20 21 22 |
# File 'app/services/blacklight/search_service.rb', line 20 def search_state_class @search_state.class end |