Class: Solr

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

Defined Under Namespace

Classes: Query

Class Method Summary (collapse)

Class Method Details

+ (Object) add_search_hook(&block)



10
11
12
# File 'backend/app/model/solr.rb', line 10

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

+ (Object) search(query)



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'backend/app/model/solr.rb', line 289

def self.search(query)

  url = query.to_solr_url

  req = Net::HTTP::Get.new(url.request_uri)

  Net::HTTP.start(url.host, url.port) do |http|
    solr_response = http.request(req)

    if solr_response.code == '200'
      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

+ (Object) search_hooks



14
15
16
# File 'backend/app/model/solr.rb', line 14

def self.search_hooks
  @@search_hooks
end