Class: Dor::SearchService

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/dor/services/search_service.rb

Constant Summary collapse

RISEARCH_TEMPLATE =
"select $object from <#ri> where $object <dc:identifier> '%s'"
@@index_version =
nil

Class Method Summary collapse

Class Method Details

.find_sdr_graveyard_apo_druidObject



93
94
95
96
97
98
99
100
# File 'lib/dor/services/search_service.rb', line 93

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

.index_versionObject



13
14
15
# File 'lib/dor/services/search_service.rb', line 13

def index_version
  Dor::VERSION
end

.iterate_over_pids(opts = {}) ⇒ Object

Deprecated.

because this depends on Fedora 3 having sparql turned on



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dor/services/search_service.rb', line 35

def iterate_over_pids(opts = {})
  Deprecation.warn(self, 'iterate_over_pids is deprecated and will be removed in dor-services 7')
  opts[:query] ||= 'select $object from <#ri> where $object <info:fedora/fedora-system:def/model#label> $label'
  opts[:in_groups_of] ||= 100
  opts[:mode] ||= :single
  start = 0
  pids = Dor::SearchService.risearch("#{opts[:query]} limit #{opts[:in_groups_of]} offset #{start}")
  while pids.present?
    if opts[:mode] == :single
      pids.each { |pid| yield pid }
    else
      yield pids
    end
    start += pids.length
    pids = Dor::SearchService.risearch("#{opts[:query]} limit #{opts[:in_groups_of]} offset #{start}")
  end
end

.query(query, args = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dor/services/search_service.rb', line 53

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

.query_by_id(id) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dor/services/search_service.rb', line 68

def query_by_id(id)
  if id.is_a?(Hash) # Single valued: { :google => 'STANFORD_0123456789' }
    id = id.collect { |*v| v.join(':') }.first
  elsif id.is_a?(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

.risearch(query, opts = {}) ⇒ Object

Deprecated.

because this depends on Fedora 3 having sparql turned on



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dor/services/search_service.rb', line 18

def risearch(query, opts = {})
  Deprecation.warn(self, 'risearch is deprecated and will be removed in dor-services 7')
  client = Config.fedora.client['risearch']
  client.options[:timeout] = opts.delete(:timeout)
  query_params = {
    type: 'tuples',
    lang: 'itql',
    format: 'CSV',
    limit: '1000',
    stream: 'on',
    query: query
  }.merge(opts)
  result = client.post(query_params)
  result.split(/\n/)[1..-1].collect { |pid| pid.chomp.sub(/^info:fedora\//, '') }
end

.sdr_graveyard_apo_druidObject

Returns String druid of the SDR Graveyard APO nil if APO does not exist in the currently configured environment.

Returns:

  • String druid of the SDR Graveyard APO nil if APO does not exist in the currently configured environment



89
90
91
# File 'lib/dor/services/search_service.rb', line 89

def sdr_graveyard_apo_druid
  @@sdr_graveyard_apo ||= find_sdr_graveyard_apo_druid
end

.solrObject



83
84
85
# File 'lib/dor/services/search_service.rb', line 83

def solr
  @@solr ||= ActiveFedora.solr.conn.is_a?(RSolr::Client) ? ActiveFedora.solr.conn : Dor::Config.make_solr_connection
end