module Birds::App::Helpers::Controller

Public Instance Methods

explain_result(result = @result) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 103
def explain_result(result = @result)
  result.debug_explain if result && result.debug?
end
facet_counts(result = @result) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 107
def facet_counts(result = @result)
  return {} unless result && result.to_i > 1 && result.facet_counts?

  exclude, prepare = [@query, *@filter], lambda { |facet_hash, &block|
    facet_hash.delete_if { |key, hash|
      gap = block[hash] if block

      hash.delete_if { |term,|
        exclude.include?(facet_query(key, term, *gap)) }.size < 2
    }
  }

  prepare.(result.facet_fields.to_h).merge(
  prepare.(result.facet_ranges.to_h) { |hash|
    counts, before, start, gap = hash
      .values_at(*%w[counts before start gap])

    hash.clear
    hash[-start] = before if before && before > 1
    counts.each { |value, count| hash[value.to_i] = count }

    hash.singleton_class.send(:define_method, :gap) { gap }
    gap
  })
end
facet_params(params = {}) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 71
def facet_params(params = {})
  params.merge(f: f = params[:f] || {}, facet: {
    field: fields = [], range: ranges = [], mincount: 1
  }).tap { settings.facet_fields.each { |facet, (_, options)|
    options.nil? ? fields << facet : begin ranges << facet
      ((f[facet] ||= {})[:facet] ||= {})[:range] = options
    end
  } }
end
highlighting(result = @result) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 133
def highlighting(result = @result)
  result.highlighting if result && result.highlighting?
end
highlighting_params(params = {}) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 81
def highlighting_params(params = {})
  params.merge(hl: {
    fl:              settings.highlighting_fields,
    snippets:        settings.highlighting_snippets,
    fragsize:        settings.highlighting_fragsize,
    mergeContiguous: true,
    preserveMulti:   true,
    simple: {
      pre:  settings.highlighting_prefix,
      post: settings.highlighting_postfix
    }
  })
end
paginate_query(query, what, query_params = {}) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 53
def paginate_query(query, what, query_params = {})
  return unless query

  page, per_page = clip(params[:page].to_i),
    clip(Integer(params[:per_page] || settings.default_per_page))

  @prev_page, @next_page = page - 1, page + 1

  @result = search_query(query, query_params.merge(
    rows: per_page, start: @offset = @prev_page * per_page))

  @page_title_extra = '%d %s, page %d of %d' % [@result, what,
    page, @total_pages = clip(@result.to_i.fdiv(per_page).ceil)]

  @prev_page = nil if @prev_page < 1
  @next_page = nil if @next_page > @total_pages
end
search_document(id) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 49
def search_document(id)
  search(q: { id: id }).first
end
search_query(query, params = {}) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 45
def search_query(query, params = {})
  search(params.merge(q: query, fl: '*,score', defType: 'edismax'))
end
solr_query(path, params = {}, options = {}, &block) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 33
def solr_query(path, params = {}, options = {}, &block)
  settings.solr.json_query(params, options, path, &block)
end
spell_query(query, params = {}) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 41
def spell_query(query, params = {})
  solr_query(settings.solr_spell_path, params.merge(q: query))
end
spellcheck_collations(result = @result, limit = nil) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 137
def spellcheck_collations(result = @result, limit = nil)
  result.spellcheck_collations(limit) if result && result.spellcheck?
end
spellcheck_params(params = {}) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 95
def spellcheck_params(params = {})
  params.merge(spellcheck: { collate: true })
end
terms(f = @field) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 99
def terms(f = @field)
  settings.solr.json('terms', terms: { fl: f, limit: -1 }).to_h[f].sort
end

Private Instance Methods

clip(num, min = 1) click to toggle source
# File lib/birds/app/helpers/controller.rb, line 143
def clip(num, min = 1)
  num < min ? min : num
end