class Elasticsearch::FacetedSearch::FacetGroup

Constants

OPERATOR_MAPPING

Attributes

key[RW]
objects[RW]

Public Class Methods

new(search, key, objects) click to toggle source

delegate :class_facets, :execution_type, :search_params, to: :search

# File lib/elasticsearch/faceted_search/facet_group.rb, line 18
def initialize(search, key, objects)
  self.search = search
  self.key = key.to_sym
  self.objects = hit_count_mapping(objects['terms'])
end

Public Instance Methods

group_params() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 39
def group_params
  group_params_string.split(operator).dup
end
group_params_string() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 43
def group_params_string
  search_params.fetch(key, '')
end
items() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 24
def items
  @items ||= build_items
end
operator() click to toggle source

Returns the string value ‘,’ or ‘|’

# File lib/elasticsearch/faceted_search/facet_group.rb, line 55
def operator
  @operator ||= operator_mappings(operator_for)
end
operator_mappings(i=nil) click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 59
def operator_mappings(i=nil)
  OPERATOR_MAPPING.fetch(i, nil)
end
selected_values() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 34
def selected_values
  v = search_params.fetch(key, '')
  operator ? v.split(operator).map(&:downcase) : v.downcase
end
title() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 28
def title
  class_facets[key][:title]
rescue
  key.to_s.humanize
end
type() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 47
def type
  class_facets[key][:type]
rescue
  nil
end

Private Instance Methods

build_items() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 71
def build_items
  terms_collection.map do |x|
    FacetItem.new(self, x.merge(count: count(x[:id])))
  end
end
count(id) click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 92
def count(id)
  objects.fetch(id.to_s.downcase, 0)
end
hit_count_mapping(o) click to toggle source

Occationally ElasticSearch will return boolean facets with mix cased characters (T/t & F/f) This method cleans this result and combines the counts to be correct & valid

# File lib/elasticsearch/faceted_search/facet_group.rb, line 85
def hit_count_mapping(o)
  @hit_count_mapping ||= o.each_with_object(Hash.new(0)) do |result, hash|
    term = result['term'].to_s.downcase
    hash[term] += result['count']
  end
end
mapped_objects() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 96
def mapped_objects
  objects.map{|k,v| {id: k, term: k, count: v} }
end
operator_for() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 65
def operator_for
  execution_type(
    class_facets[key][:type]
  )
end
terms_collection() click to toggle source
# File lib/elasticsearch/faceted_search/facet_group.rb, line 77
def terms_collection
  return mapped_objects unless search.respond_to?(:"#{key}_collection")
  @terms_collection ||= search.public_send(:"#{key}_collection")
end