Class: Hyrax::SearchService

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

Overview

Copied from Blacklight 7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, user_params: nil, search_builder_class: config.search_builder_class, **context) ⇒ SearchService



7
8
9
10
11
12
# File 'app/services/hyrax/search_service.rb', line 7

def initialize(config:, user_params: nil, search_builder_class: config.search_builder_class, **context)
  @blacklight_config = config
  @user_params = user_params || {}
  @search_builder_class = search_builder_class
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (private)



66
67
68
69
70
71
72
73
74
75
# File 'app/services/hyrax/search_service.rb', line 66

def method_missing(method_name, *arguments, &block)
  if scope&.respond_to?(method_name)
    Deprecation.warn(self.class, "Calling `#{method_name}` on scope " \
      'is deprecated and will be removed in Blacklight 8. Call #to_h first if you ' \
      ' need to use hash methods (or, preferably, use your own SearchState implementation)')
    scope&.public_send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#blacklight_configObject (readonly)

The blacklight_config + controller are accessed by the search_builder



15
16
17
# File 'app/services/hyrax/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/hyrax/search_service.rb', line 15

def context
  @context
end

Instance Method Details

#fetch(id = nil, extra_controller_params = {}) ⇒ Blacklight::Solr::Response, Blacklight::SolrDocument

retrieve a document, given the doc id



46
47
48
49
50
51
52
# File 'app/services/hyrax/search_service.rb', line 46

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

#search_builderObject



17
18
19
# File 'app/services/hyrax/search_service.rb', line 17

def search_builder
  search_builder_class.new(self)
end

#search_results {|search_builder| ... } ⇒ Blacklight::Solr::Response

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity 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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/hyrax/search_service.rb', line 25

def search_results
  builder = search_builder.with(user_params)
  builder.page = user_params[:page] if user_params[:page]
  builder.rows = (user_params[:per_page] || user_params[:rows]) if user_params[:per_page] || user_params[:rows]

  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