|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'rsolr' |
| 3 | + |
| 4 | +module ActiveFedora |
| 5 | + class SolrService |
| 6 | + attr_reader :options |
| 7 | + attr_writer :conn |
| 8 | + |
| 9 | + MAX_ROWS = 10_000 |
| 10 | + |
| 11 | + def initialize(options = {}) |
| 12 | + @options = { timeout: 120, open_timeout: 120, url: 'http://localhost:8080/solr' }.merge(options) |
| 13 | + end |
| 14 | + |
| 15 | + def conn |
| 16 | + @conn ||= RSolr.connect @options |
| 17 | + end |
| 18 | + |
| 19 | + class << self |
| 20 | + # @param [Hash] options |
| 21 | + def register(options = {}) |
| 22 | + ActiveFedora::RuntimeRegistry.solr_service = new(options) |
| 23 | + end |
| 24 | + |
| 25 | + def reset! |
| 26 | + ActiveFedora::RuntimeRegistry.solr_service = nil |
| 27 | + end |
| 28 | + |
| 29 | + def select_path |
| 30 | + ActiveFedora.solr_config.fetch(:select_path, 'select') |
| 31 | + end |
| 32 | + |
| 33 | + def instance |
| 34 | + # Register Solr |
| 35 | + |
| 36 | + register(ActiveFedora.solr_config) unless ActiveFedora::RuntimeRegistry.solr_service |
| 37 | + |
| 38 | + ActiveFedora::RuntimeRegistry.solr_service |
| 39 | + end |
| 40 | + |
| 41 | + def get(query, args = {}) |
| 42 | + args = args.merge(q: query, qt: 'standard') |
| 43 | + SolrService.instance.conn.get(select_path, params: args) |
| 44 | + end |
| 45 | + |
| 46 | + def post(query, args = {}) |
| 47 | + args = args.merge(q: query, qt: 'standard') |
| 48 | + SolrService.instance.conn.post(select_path, data: args) |
| 49 | + end |
| 50 | + |
| 51 | + def query(query, args = {}) |
| 52 | + unless args.key?(:rows) |
| 53 | + Base.logger.warn "Calling ActiveFedora::SolrService.get without passing an explicit value for ':rows' is not recommended. You will end up with |
| 54 | +Solr's default (usually set to 10)\nCalled by #{caller[0]}" |
| 55 | + end |
| 56 | + method = args.delete(:method) || :get |
| 57 | + |
| 58 | + result = case method |
| 59 | + when :get |
| 60 | + get(query, args) |
| 61 | + when :post |
| 62 | + post(query, args) |
| 63 | + else |
| 64 | + raise "Unsupported HTTP method for querying SolrService (#{method.inspect})" |
| 65 | + end |
| 66 | + result['response']['docs'].map do |doc| |
| 67 | + ActiveFedora::SolrHit.new(doc) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def delete(id) |
| 72 | + SolrService.instance.conn.delete_by_id(id, params: { 'softCommit' => true }) |
| 73 | + end |
| 74 | + |
| 75 | + # Get the count of records that match the query |
| 76 | + # @param [String] query a solr query |
| 77 | + # @param [Hash] args arguments to pass through to `args' param of SolrService.query (note that :rows will be overwritten to 0) |
| 78 | + # @return [Integer] number of records matching |
| 79 | + def count(query, args = {}) |
| 80 | + args = args.merge(rows: 0) |
| 81 | + SolrService.get(query, args)['response']['numFound'].to_i |
| 82 | + end |
| 83 | + |
| 84 | + # @param [Hash] doc the document to index, or an array of docs |
| 85 | + # @param [Hash] params |
| 86 | + # :commit => commits immediately |
| 87 | + # :softCommit => commit to memory, but don't flush to disk |
| 88 | + def add(doc, params = {}) |
| 89 | + SolrService.instance.conn.add(doc, params: params) |
| 90 | + end |
| 91 | + |
| 92 | + def commit |
| 93 | + SolrService.instance.conn.commit |
| 94 | + end |
| 95 | + end |
| 96 | + end # SolrService |
| 97 | +end # ActiveFedora |
0 commit comments