module Elasticsearch::FacetedSearch::FacetBase

Attributes

params[RW]

Public Class Methods

new(p) click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 16
def initialize(p)
  self.params = p
end

Public Instance Methods

class_facets() click to toggle source

Instance

# File lib/elasticsearch/faceted_search/facet_base.rb, line 76
def class_facets
  self.class.facets.dup
end
facet_query() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 102
def facet_query
  @facet_query ||= build_facets
end
facets() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 24
def facets
  @facets ||= search['facets'].map{|key, values| FacetGroup.new(self, key, values) }
end
filter_query() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 106
def filter_query
  @filter_query ||= build_filters
end
query() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 80
def query
  q = {
    size: limit,
    from: ([current_page.to_i, 1].max - 1) * limit,
    sort: current_sort_for_search
  }

  # Filters
  q.merge!({
    :filter => {
      :and => filter_query
    }
  }) unless filter_query.blank?

  # Facets
  q.merge!({
    :facets => facet_query
  }) unless facet_query.blank?

  q.reject{|k,v| v.blank? }
end
results() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 20
def results
  @results ||= search['hits']['hits'].map{|x| Hashie::Mash.new(x['_source'].merge('_type' => x['_type'])) }
end
search_params() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 110
def search_params
  params
end

Protected Instance Methods

execution_type(type) click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 128
def execution_type(type)
  case type
    when 'multivalue'
      # TODO: Based off params
    when 'multivalue_or'
      :or
    when 'multivalue_and'
      :and
    when 'exclusive_or'
      nil
  end
end
facet_size_allowed() click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 141
def facet_size_allowed
  70
end
operator_for(key) click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 120
def operator_for(key)
  operator_mappings(
    execution_type(
      class_facets[key][:type]
    )
  )
end
operator_mappings(i=nil) click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 116
def operator_mappings(i=nil)
  FacetGroup::OPERATOR_MAPPING.fetch(i, nil)
end

Private Instance Methods

build_facets(filter_counts=true) click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 147
def build_facets(filter_counts=true)
  h = {}
  filtered_facets = filter_counts && filter_query.present? ? {facet_filter: { :and => filter_query }} : {}

  class_facets.each do |k,v|
    h.merge!({
      k => {
        terms: {
          field: v[:field],
          size: facet_size_allowed
        }
      }.merge(filtered_facets).reject{|k,v| v.blank?}
    })
  end

  h
end
build_filters() click to toggle source

Whitelist and filter

# File lib/elasticsearch/faceted_search/facet_base.rb, line 166
def build_filters
  class_facets.map do |type, info|
    if respond_to?(:"filter_#{type}?") and public_send("filter_#{type}?")
      {
        terms: {
          info[:field] => values_for(type),
          :execution => execution_type(info[:type])
        }.reject{|k,v| v.blank?}
      }
    end
  end.compact
end
values_for(facet_type) click to toggle source
# File lib/elasticsearch/faceted_search/facet_base.rb, line 179
def values_for(facet_type)
  Array(public_send("#{facet_type}_value"))
end