Class: Dor::SearchService

Inherits:
Object
  • Object
show all
Includes:
Solrizer::FieldNameMapper
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


118
119
120
121
122
123
124
125
# File 'lib/dor/services/search_service.rb', line 118

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

.gsearch(params) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dor/services/search_service.rb', line 63

def gsearch(params)
  client = Config.gsearch.client
  query_params = params.merge(:wt => 'json')
  query_string = query_params.collect { |k,v|
    if v.is_a?(Array)
      v.collect { |vv| "#{k}=#{URI.encode(vv.to_s)}" }.join('&')
    else
      "#{k}=#{URI.encode(v.to_s)}"
    end
  }.join('&')
  result = JSON.parse(client["select?#{query_string}"].get)
end

.index_versionObject


14
15
16
17
18
19
20
# File 'lib/dor/services/search_service.rb', line 14

def index_version
  if @@index_version.nil?
    xsl_doc = Nokogiri::XML(File.read(File.expand_path('../../../gsearch/demoFoxmlToSolr.xslt',__FILE__)))
    @@index_version = xsl_doc.at_xpath('/xsl:stylesheet/xsl:variable[@name="INDEXVERSION"]/text()').to_s
  end
  @@index_version
end

.iterate_over_pids(opts = {}, &block) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dor/services/search_service.rb', line 46

def iterate_over_pids(opts = {}, &block)
  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


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dor/services/search_service.rb', line 76

def query query, args={}
  params = args.merge({ :q => query })
  params[:start] ||= 0
  resp = solr.find params
  if block_given?
    cont = true
    while cont and resp.docs.length > 0
      cont = yield(resp)
      params[:rows] ||= resp.docs.length
      params[:start] += params[:rows]
      resp = solr.find params
    end
  else
    return resp
  end
end

.query_by_id(id) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dor/services/search_service.rb', line 93

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 = %{#{solr_name 'identifier', :string}:"#{id}"}
  result = []
  resp = query(q, :fl => 'id', :rows => 1000) do |resp|
    result += resp.docs.collect { |doc| doc['id'] }
    true
  end
  result
end

.reindex(*pids) ⇒ Object


22
23
24
25
26
27
28
29
# File 'lib/dor/services/search_service.rb', line 22

def reindex(*pids)
  client = Config.gsearch.rest_client
  pids.in_groups_of(20, false) do |group|
    group.each { |pid| client["?operation=updateIndex&action=fromPid&value=#{pid}"].get }
    yield group if block_given?
  end
  pids
end

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


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dor/services/search_service.rb', line 31

def risearch(query, opts = {})
  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


114
115
116
# File 'lib/dor/services/search_service.rb', line 114

def sdr_graveyard_apo_druid
  @@sdr_graveyard_apo ||= find_sdr_graveyard_apo_druid
end

.solrObject


108
109
110
# File 'lib/dor/services/search_service.rb', line 108

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