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 14, 2025
1 parent 5ef61ae commit 0c88c60
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions lib/active_fedora/solr_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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

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

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 = {})
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]}" 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|
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 0c88c60

Please # to comment.