Class: Hyrax::SolrService

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

Overview

Supports a range of basic Solr interactions.

This class replaces ‘ActiveFedora::SolrService`, which is deprecated for internal use.

Constant Summary collapse

COMMIT_PARAMS =
{ softCommit: true }.freeze

Class Attributes collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_valkyrie: Hyrax.config.query_index_from_valkyrie) ⇒ SolrService

Returns a new instance of SolrService.



35
36
37
38
# File 'app/services/hyrax/solr_service.rb', line 35

def initialize(use_valkyrie: Hyrax.config.query_index_from_valkyrie)
  @old_service = ActiveFedora::SolrService # What hopefully will be the lone reference to ActiveFedora::SolrService
  @use_valkyrie = use_valkyrie
end

Instance Attribute Details

#additional_solr_get_and_post_paramsHash<Symbol, String>

ActiveFedora::SolrService always assumed that it would pass ‘qt: ’standard’‘. However, as Valkyrie arrived, we have a case where we would not pass the `qt: standard`. We’re favoring a default value to preserve prior expectations that ActiveFedora::SolrService set and to not suddenly break those that switch to Valkyrie and Hyrax::SolrService.

Returns:

  • (Hash<Symbol, String>)

See Also:



24
# File 'app/services/hyrax/solr_service.rb', line 24

class_attribute :additional_solr_get_and_post_params, default: { qt: 'standard' }

#use_valkyrieObject (readonly)

Returns the value of attribute use_valkyrie.



31
32
33
# File 'app/services/hyrax/solr_service.rb', line 31

def use_valkyrie
  @use_valkyrie
end

Class Method Details

.select_pathObject

We don’t implement ‘.select_path` instead configuring this at the Hyrax level

Raises:

  • (NotImplementedError)


51
52
53
54
# File 'app/services/hyrax/solr_service.rb', line 51

def select_path
  raise NotImplementedError, 'This method is not available on this subclass.' \
                             'Use `Hyrax.config.solr_select_path` instead'
end

Instance Method Details

#add(solr_doc, commit: true) ⇒ Hash

Wraps rsolr add

Returns:

  • (Hash)

    the hash straight form rsolr



132
133
134
# File 'app/services/hyrax/solr_service.rb', line 132

def add(solr_doc, commit: true)
  connection.add(solr_doc, params: { softCommit: commit })
end

#count(query) ⇒ Hash

Wraps rsolr count

Returns:

  • (Hash)

    the hash straight from rsolr



138
139
140
141
# File 'app/services/hyrax/solr_service.rb', line 138

def count(query)
  args = { rows: 0 }
  query_result(query, **args)['response']['numFound'].to_i
end

#delete(id) ⇒ Object

Wraps rsolr delete



120
121
122
# File 'app/services/hyrax/solr_service.rb', line 120

def delete(id)
  connection.delete_by_id(id, params: COMMIT_PARAMS)
end

#delete_by_query(query, **args) ⇒ Object

Wraps rsolr :delete_by_query



115
116
117
# File 'app/services/hyrax/solr_service.rb', line 115

def delete_by_query(query, **args)
  connection.delete_by_query(query, params: args)
end

#get(query = nil, **args) ⇒ Hash

Wraps rsolr get

Returns:

  • (Hash)

    the hash straight form rsolr



62
63
64
65
66
67
68
# File 'app/services/hyrax/solr_service.rb', line 62

def get(query = nil, **args)
  # Make Hyrax.config.solr_select_path the default SOLR path
  solr_path = args.delete(:path) || Hyrax.config.solr_select_path
  args = args.merge(additional_solr_get_and_post_params.merge(q: query)) if query.present?

  connection.get(solr_path, params: args)
end

#instanceObject



40
41
42
43
44
45
# File 'app/services/hyrax/solr_service.rb', line 40

def instance
  # Deprecation warning for calling from outside of the Hyrax::SolrService class
  Deprecation.warn(self, rsolr_call_warning) unless caller[1].include?("#{self.class.name.underscore}.rb")

  @old_service.instance
end

#pingBoolean

Sends a ping request to solr

Returns:

  • (Boolean)

    ‘true` if the ping is successful



74
75
76
77
# File 'app/services/hyrax/solr_service.rb', line 74

def ping
  response = connection.get('admin/ping')
  response['status'] == "OK"
end

#post(query = nil, **args) ⇒ Hash

Wraps rsolr post

Returns:

  • (Hash)

    the hash straight form rsolr



81
82
83
84
85
86
87
# File 'app/services/hyrax/solr_service.rb', line 81

def post(query = nil, **args)
  # Make Hyrax.config.solr_select_path the default SOLR path
  solr_path = args.delete(:path) || Hyrax.config.solr_select_path
  args = args.merge(additional_solr_get_and_post_params.merge(q: query)) if query.present?

  connection.post(solr_path, data: args)
end

#query(query, **args) ⇒ Array<SolrHit>

Execute the provided query. Uses the configured http method by default.

Returns:

  • (Array<SolrHit>)

    the response docs wrapped in SolrHit objects



108
109
110
111
112
# File 'app/services/hyrax/solr_service.rb', line 108

def query(query, **args)
  query_result(query, **args)['response']['docs'].map do |doc|
    ::SolrHit.new(doc)
  end
end

#query_result(query, **args) ⇒ Hash

Query solr using the provided or default http method, returning the result as a hash.

Returns:

  • (Hash)

    raw query result from solr



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/services/hyrax/solr_service.rb', line 91

def query_result(query, **args)
  Hyrax.logger.warn rows_warning unless args.key?(:rows)
  # Use the provided solr query method, or fall back to the configured default
  method = args.delete(:method) || Hyrax.config.solr_default_method

  case method
  when :get
    get(query, **args)
  when :post
    post(query, **args)
  else
    raise "Unsupported HTTP method for querying SolrService (#{method.inspect})"
  end
end

#search_by_id(id, **opts) ⇒ Array<SolrHit>

Wraps ActiveFedora::Base#search_by_id(id, opts)

Returns:

  • (Array<SolrHit>)

    the response docs wrapped in SolrHit objects

Raises:



145
146
147
148
149
150
# File 'app/services/hyrax/solr_service.rb', line 145

def search_by_id(id, **opts)
  result = Hyrax::SolrService.query("id:#{id}", **opts.merge(rows: 1))

  raise Hyrax::ObjectNotFoundError, "Object '#{id}' not found in solr" if result.empty?
  result.first
end

#wipe!Object

Deletes all solr documents



125
126
127
128
# File 'app/services/hyrax/solr_service.rb', line 125

def wipe!
  delete_by_query("*:*")
  commit
end