Skip to content

Commit

Permalink
Replaces read_timout with timout in Solr initializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Scherz committed Jan 17, 2025
1 parent 5ef61ae commit cf64d80
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Metrics/MethodLength:
- 'app/helpers/mark_helper.rb'
- 'bin/importer'
- 'spec/controllers/bulkrax/importers_controller_spec.rb'
- 'lib/active_fedora/solr_service.rb'

Metrics/ClassLength:
Exclude:
Expand Down
97 changes: 97 additions & 0 deletions lib/active_fedora/solr_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true
require 'rsolr'

module ActiveFedora
class SolrService
attr_reader :options
attr_writer :conn

MAX_ROWS = 10_000

def initialize(options = {})
@options = { timeout: 120, open_timeout: 120, url: 'http://localhost:8080/solr' }.merge(options)
end

def conn
@conn ||= RSolr.connect @options
end

class << self
# @param [Hash] options
def register(options = {})
ActiveFedora::RuntimeRegistry.solr_service = new(options)
end

def reset!
ActiveFedora::RuntimeRegistry.solr_service = nil
end

def select_path
ActiveFedora.solr_config.fetch(:select_path, 'select')
end

def instance
# Register Solr

register(ActiveFedora.solr_config) unless ActiveFedora::RuntimeRegistry.solr_service

ActiveFedora::RuntimeRegistry.solr_service
end

def get(query, args = {})
args = args.merge(q: query, qt: 'standard')
SolrService.instance.conn.get(select_path, params: args)
end

def post(query, args = {})
args = args.merge(q: query, qt: 'standard')
SolrService.instance.conn.post(select_path, data: args)
end

def query(query, args = {})
unless args.key?(:rows)
Base.logger.warn "Calling ActiveFedora::SolrService.get without passing an explicit value for ':rows' is not recommended. You will end up with
Solr's default (usually set to 10)\nCalled by #{caller[0]}"
end
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|
ActiveFedora::SolrHit.new(doc)
end
end

def delete(id)
SolrService.instance.conn.delete_by_id(id, params: { 'softCommit' => true })
end

# Get the count of records that match the query
# @param [String] query a solr query
# @param [Hash] args arguments to pass through to `args' param of SolrService.query (note that :rows will be overwritten to 0)
# @return [Integer] number of records matching
def count(query, args = {})
args = args.merge(rows: 0)
SolrService.get(query, args)['response']['numFound'].to_i
end

# @param [Hash] doc the document to index, or an array of docs
# @param [Hash] params
# :commit => commits immediately
# :softCommit => commit to memory, but don't flush to disk
def add(doc, params = {})
SolrService.instance.conn.add(doc, params: params)
end

def commit
SolrService.instance.conn.commit
end
end
end # SolrService
end # ActiveFedora

0 comments on commit cf64d80

Please sign in to comment.