Class: Solr

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/solr.rb

Defined Under Namespace

Modules: Checksums Classes: ChecksumMismatchError, Config, NotFound, Query, Schema, Solrconfig

Class Method Summary collapse

Class Method Details

.add_search_hook(&block) ⇒ Object



80
81
82
# File 'backend/app/model/solr.rb', line 80

def self.add_search_hook(&block)
  @@search_hooks << block
end

.search(query) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'backend/app/model/solr.rb', line 394

def self.search(query)
  url = query.to_solr_url

  req = Net::HTTP::Post.new(url.path)
  req.body = url.query
  req.content_type = 'application/x-www-form-urlencoded'

  ASHTTP.start_uri(url) do |http|
    solr_response = http.request(req)

    if solr_response.code == '200'
      return solr_response.body unless query.get_writer_type == "json"
      json = ASUtils.json_parse(solr_response.body)

      result = {}

      page_size = query.page_size

      result['page_size'] = page_size
      result['first_page'] = 1
      result['last_page'] = (json['response']['numFound'] / page_size.to_f).ceil
      result['this_page'] = (json['response']['start'] / page_size) + 1

      result['offset_first'] = json['response']['start'] + 1
      result['offset_last'] = [(json['response']['start'] + page_size), json['response']['numFound']].min
      result['total_hits'] = json['response']['numFound']

      result['results'] = json['response']['docs'].map {|doc|
        doc['uri'] ||= doc['id']
        doc['jsonmodel_type'] = doc['primary_type']
        doc
      }

      result['facets'] = json['facet_counts']

      if json['highlighting']
        result['highlighting'] = json['highlighting']
      end

      return result
    else
      raise "Solr search failed: #{solr_response.body}"
    end
  end
end

.search_hooksObject



84
85
86
# File 'backend/app/model/solr.rb', line 84

def self.search_hooks
  @@search_hooks
end

.verify_checksum!(config) ⇒ Object



93
94
95
96
97
# File 'backend/app/model/solr.rb', line 93

def self.verify_checksum!(config)
  return true if config.checksum_valid?

  raise ChecksumMismatchError.new "Solr checksum verification failed (#{config.name}): expected [#{config.internal_checksum}] got [#{config.external_checksum}]"
end

.verify_checksums!Object



88
89
90
91
# File 'backend/app/model/solr.rb', line 88

def self.verify_checksums!
  verify_checksum!(Solr::Schema.new(AppConfig[:solr_url]))
  verify_checksum!(Solr::Solrconfig.new(AppConfig[:solr_url]))
end