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
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
Fetch query results from solr.
- #search_state_class ⇒ Object
Constructor Details
#initialize(config:, search_state:, 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:, search_builder_class: config.search_builder_class, **context) @blacklight_config = config @search_state = search_state @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
62 63 64 65 |
# File 'app/services/blacklight/search_service.rb', line 62 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
67 68 69 70 |
# File 'app/services/blacklight/search_service.rb', line 67 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
51 52 53 54 55 56 57 |
# File 'app/services/blacklight/search_service.rb', line 51 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.
93 94 95 96 97 98 99 100 |
# File 'app/services/blacklight/search_service.rb', line 93 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
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/services/blacklight/search_service.rb', line 74 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(params: 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
Fetch query results from solr
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/services/blacklight/search_service.rb', line 30 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 |
#search_state_class ⇒ Object
21 22 23 |
# File 'app/services/blacklight/search_service.rb', line 21 def search_state_class @search_state.class end |