Sha256: 08f95f640cc035616a603f0b399b573451b4dc9193df6dd58ccdfc396684ac91

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'json'
require 'active_support/core_ext'

module Dor
  # Used by Argo and dor-services-app
  class SearchService
    class << self
      def index_version
        Dor::VERSION
      end

      def query(query, args = {})
        params = args.merge(q: query)
        params[:start] ||= 0
        resp = solr.get 'select', params: params
        return resp unless block_given?

        cont = true
        while cont && resp['response']['docs'].length > 0
          cont = yield(resp)
          params[:rows] ||= resp['response']['docs'].length
          params[:start] += params[:rows]
          resp = solr.get 'select', params: params
        end
      end

      def query_by_id(id)
        case id
        when Hash # Single valued: { :google => 'STANFORD_0123456789' }
          id = id.collect { |*v| v.join(':') }.first
        when Array # Two values: [ 'google', 'STANFORD_0123456789' ]
          id = id.join(':')
        end
        q = "{!term f=#{Solrizer.solr_name 'identifier', :symbol}}#{id}"
        result = []
        query(q, fl: 'id', rows: 1000, defType: 'lucene') do |resp|
          result += resp['response']['docs'].collect { |doc| doc['id'] }
          true
        end
        result
      end

      # @return String druid of the SDR Graveyard APO
      #   nil if APO does not exist in the currently configured environment
      def sdr_graveyard_apo_druid
        @@sdr_graveyard_apo ||= find_sdr_graveyard_apo_druid
      end

      def find_sdr_graveyard_apo_druid
        r = Dor::SearchService.query('dc_title_tesim:"SDR Graveyard"', fl: 'id')
        if r['response']['docs'].empty?
          nil
        else
          r['response']['docs'].first[:id]
        end
      end

      private

      def solr
        ActiveFedora.solr.conn
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dor-services-9.6.2 lib/dor/services/search_service.rb
dor-services-9.6.1 lib/dor/services/search_service.rb
dor-services-9.6.0 lib/dor/services/search_service.rb