class Tire::Search::Facet

Public Class Methods

new(name, options={}, &block) click to toggle source
# File lib/tire/search/facet.rb, line 6
def initialize(name, options={}, &block)
  @name    = name
  @options = options
  @value   = {}
  block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

Public Instance Methods

date(field, options={}) click to toggle source
# File lib/tire/search/facet.rb, line 24
def date(field, options={})
  interval = { :interval => options.delete(:interval) || 'day' }
  fields   = options[:value_field] || options[:value_script] ? { :key_field => field } : { :field => field }
  @value[:date_histogram] = {}.update(fields).update(interval).update(options)
  self
end
facet_filter(type, *options) click to toggle source
# File lib/tire/search/facet.rb, line 65
def facet_filter(type, *options)
  @value[:facet_filter] = Filter.new(type, *options).to_hash
  self
end
filter(type, options={}) click to toggle source
# File lib/tire/search/facet.rb, line 60
def filter(type, options={})
  @value[:filter] = Filter.new(type, options)
  self
end
geo_distance(field, point, ranges=[], options={}) click to toggle source
# File lib/tire/search/facet.rb, line 46
def geo_distance(field, point, ranges=[], options={})
  @value[:geo_distance] = { field => point, :ranges => ranges }.update(options)
  self
end
histogram(field, options={}) click to toggle source
# File lib/tire/search/facet.rb, line 36
def histogram(field, options={})
  @value[:histogram] = (options.delete(:histogram) || {:field => field}.update(options))
  self
end
query(&block) click to toggle source
# File lib/tire/search/facet.rb, line 56
def query(&block)
  @value[:query] = Query.new(&block).to_hash
end
range(field, ranges=[], options={}) click to toggle source
# File lib/tire/search/facet.rb, line 31
def range(field, ranges=[], options={})
  @value[:range] = { :field => field, :ranges => ranges }.update(options)
  self
end
statistical(field, options={}) click to toggle source
# File lib/tire/search/facet.rb, line 41
def statistical(field, options={})
  @value[:statistical] = (options.delete(:statistical) || {:field => field}.update(options))
  self
end
terms(field, options={}) click to toggle source
# File lib/tire/search/facet.rb, line 13
def terms(field, options={})
  size      = options.delete(:size) || 10
  all_terms = options.delete(:all_terms) || false
  @value[:terms] = if field.is_a?(Enumerable) and not field.is_a?(String)
    { :fields => field }.update({ :size => size, :all_terms => all_terms }).update(options)
  else
    { :field => field  }.update({ :size => size, :all_terms => all_terms }).update(options)
  end
  self
end
terms_stats(key_field, value_field, options={}) click to toggle source
# File lib/tire/search/facet.rb, line 51
def terms_stats(key_field, value_field, options={})
  @value[:terms_stats] = {:key_field => key_field, :value_field => value_field}.update(options)
  self
end
to_hash() click to toggle source
# File lib/tire/search/facet.rb, line 74
def to_hash
  @value.update @options
  { @name => @value }
end
to_json(options={}) click to toggle source
# File lib/tire/search/facet.rb, line 70
def to_json(options={})
  to_hash.to_json
end