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



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

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 form rsolr



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

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

#delete(id) ⇒ Object

Wraps rsolr delete



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

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

#delete_by_query(query, **args) ⇒ Object

Wraps rsolr :delete_by_query



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

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>

Wraps get by default

Returns:

  • (Array<SolrHit>)

    the response docs wrapped in SolrHit objects



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

def query(query, **args)
  Rails.logger.warn rows_warning unless args.key?(:rows)
  method = args.delete(:method) || :get

  result = case method
           when :get
             get(query, **args)
           when :post
             post(query, **args)
           else
             raise "Unsupported HTTP method for querying SolrService (#{method.inspect})"
           end
  result['response']['docs'].map do |doc|
    ::SolrHit.new(doc)
  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:



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

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



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

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