Class: Blacklight::SearchService

Inherits:
Object
  • Object
show all
Defined in:
app/services/blacklight/search_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_configObject (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

#contextObject (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

Returns:



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

Parameters:

  • id (Array{#to_s}, #to_s) (defaults to: nil)

Returns:

  • (Blacklight::SolrDocument)

    the solr document (or array of documents if an array of ids was given)



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

Returns:



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_builderObject



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

Yields:

  • (search_builder)

    optional block yields configured SearchBuilder, caller can modify or create new SearchBuilder to be used. Block should return SearchBuilder to be used.

Returns:



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_classObject



21
22
23
# File 'app/services/blacklight/search_service.rb', line 21

def search_state_class
  @search_state.class
end