-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces read_timout with timout in Solr initializer.
- Loading branch information
Thomas Scherz
committed
Jan 17, 2025
1 parent
5ef61ae
commit cf64d80
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |