diff --git a/lib/active_fedora/solr_service.rb b/lib/active_fedora/solr_service.rb new file mode 100644 index 00000000..50296ed4 --- /dev/null +++ b/lib/active_fedora/solr_service.rb @@ -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