Class: Tire::Search::Facet

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/search/facet.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Facet

Returns a new instance of Facet.



6
7
8
9
10
11
# 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

Instance Method Details

#date(field, options = {}) ⇒ Object



24
25
26
27
28
29
# 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) ⇒ Object



65
66
67
68
# 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 = {}) ⇒ Object



60
61
62
63
# 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 = {}) ⇒ Object



46
47
48
49
# 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 = {}) ⇒ Object



36
37
38
39
# 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) ⇒ Object



56
57
58
# File 'lib/tire/search/facet.rb', line 56

def query(&block)
  @value[:query] = Query.new(&block).to_hash
end

#range(field, ranges = [], options = {}) ⇒ Object



31
32
33
34
# 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 = {}) ⇒ Object



41
42
43
44
# 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 = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# 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 = {}) ⇒ Object



51
52
53
54
# 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_hashObject



74
75
76
77
# File 'lib/tire/search/facet.rb', line 74

def to_hash
  @value.update @options
  { @name => @value }
end

#to_json(options = {}) ⇒ Object



70
71
72
# File 'lib/tire/search/facet.rb', line 70

def to_json(options={})
  to_hash.to_json
end