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

.add_search_hook(&block) ⇒ Object



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

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

.search(query) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'backend/app/model/solr.rb', line 314

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



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

def self.search_hooks
  @@search_hooks
end