Class: Hyrax::SolrService
- Inherits:
-
Object
- Object
- Hyrax::SolrService
- 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
-
#additional_solr_get_and_post_params ⇒ Hash<Symbol, String>
ActiveFedora::SolrService always assumed that it would pass ‘qt: ’standard’‘.
Instance Attribute Summary collapse
-
#use_valkyrie ⇒ Object
readonly
Returns the value of attribute use_valkyrie.
Class Method Summary collapse
-
.select_path ⇒ Object
We don’t implement ‘.select_path` instead configuring this at the Hyrax level.
Instance Method Summary collapse
-
#add(solr_doc, commit: true) ⇒ Hash
Wraps rsolr add.
- #connection ⇒ Object private
-
#count(query) ⇒ Hash
Wraps rsolr count.
-
#delete(id) ⇒ Object
Wraps rsolr delete.
-
#delete_by_query(query, **args) ⇒ Object
Wraps rsolr :delete_by_query.
-
#get(query = nil, **args) ⇒ Hash
Wraps rsolr get.
-
#initialize(use_valkyrie: Hyrax.config.query_index_from_valkyrie) ⇒ SolrService
constructor
A new instance of SolrService.
- #instance ⇒ Object
-
#ping ⇒ Boolean
Sends a ping request to solr.
-
#post(query = nil, **args) ⇒ Hash
Wraps rsolr post.
-
#query(query, **args) ⇒ Array<SolrHit>
Execute the provided query.
- #query_in_batches(query, **args) ⇒ Object
-
#query_result(query, **args) ⇒ Hash
Query solr using the provided or default http method, returning the result as a hash.
-
#search_by_id(id, **opts) ⇒ Array<SolrHit>
Wraps ActiveFedora::Base#search_by_id(id, opts).
-
#wipe! ⇒ Object
Deletes all solr documents.
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_params ⇒ Hash<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.
24 |
# File 'app/services/hyrax/solr_service.rb', line 24 class_attribute :additional_solr_get_and_post_params, default: { qt: 'standard' } |
#use_valkyrie ⇒ Object (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_path ⇒ Object
We don’t implement ‘.select_path` instead configuring this at the Hyrax level
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
146 147 148 |
# File 'app/services/hyrax/solr_service.rb', line 146 def add(solr_doc, commit: true) connection.add(solr_doc, params: { softCommit: commit }) end |
#connection ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
168 169 170 171 |
# File 'app/services/hyrax/solr_service.rb', line 168 def connection return self.class.instance.conn unless use_valkyrie valkyrie_index.connection end |
#count(query) ⇒ Hash
Wraps rsolr count
152 153 154 155 |
# File 'app/services/hyrax/solr_service.rb', line 152 def count(query) args = { rows: 0 } query_result(query, **args)['response']['numFound'].to_i end |
#delete(id) ⇒ Object
Wraps rsolr delete
134 135 136 |
# File 'app/services/hyrax/solr_service.rb', line 134 def delete(id) connection.delete_by_id(id, params: COMMIT_PARAMS) end |
#delete_by_query(query, **args) ⇒ Object
Wraps rsolr :delete_by_query
129 130 131 |
# File 'app/services/hyrax/solr_service.rb', line 129 def delete_by_query(query, **args) connection.delete_by_query(query, params: args) end |
#get(query = nil, **args) ⇒ Hash
Wraps rsolr get
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 |
#instance ⇒ Object
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 |
#ping ⇒ Boolean
Sends a ping request to solr
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
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.
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_in_batches(query, **args) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/services/hyrax/solr_service.rb', line 114 def query_in_batches(query, **args) args[:rows] ||= 500 args[:start] ||= 0 loop do result = query_result(query, **args) break if result['response']['docs'].blank? || result['response']['numFound'] <= args[:start] result['response']['docs'].select do |doc| yield ::SolrHit.new(doc) nil end args[:start] += args[:rows] end end |
#query_result(query, **args) ⇒ Hash
Query solr using the provided or default http method, returning the result as a hash.
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)
159 160 161 162 163 164 |
# File 'app/services/hyrax/solr_service.rb', line 159 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
139 140 141 142 |
# File 'app/services/hyrax/solr_service.rb', line 139 def wipe! delete_by_query("*:*") commit end |