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

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.



19
20
21
22
# File 'app/services/hyrax/solr_service.rb', line 19

def initialize(use_valkyrie: Hyrax.config.query_index_from_valkyrie)
  @old_service = ActiveFedora::SolrService
  @use_valkyrie = use_valkyrie
end

Instance Attribute Details

#use_valkyrieObject (readonly)

Returns the value of attribute use_valkyrie.



15
16
17
# File 'app/services/hyrax/solr_service.rb', line 15

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)


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

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



118
119
120
# File 'app/services/hyrax/solr_service.rb', line 118

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



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

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

#delete(id) ⇒ Object

Wraps rsolr delete



106
107
108
# File 'app/services/hyrax/solr_service.rb', line 106

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

#delete_by_query(query, **args) ⇒ Object

Wraps rsolr :delete_by_query



101
102
103
# File 'app/services/hyrax/solr_service.rb', line 101

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



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

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(q: query) if query.present?

  args = args.merge(qt: 'standard') unless query.blank? || use_valkyrie
  connection.get(solr_path, params: args)
end

#instanceObject



24
25
26
27
28
29
# File 'app/services/hyrax/solr_service.rb', line 24

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



59
60
61
62
# File 'app/services/hyrax/solr_service.rb', line 59

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



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

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(q: query) if query.present?

  args = args.merge(qt: 'standard') unless query.blank? || use_valkyrie
  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



94
95
96
97
98
# File 'app/services/hyrax/solr_service.rb', line 94

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/services/hyrax/solr_service.rb', line 77

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:



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

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



111
112
113
114
# File 'app/services/hyrax/solr_service.rb', line 111

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