Class: Tire::Search::Facet

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

Overview

– TODO: Implement all elastic search facets (geo, histogram, range, etc) elasticsearch.org/guide/reference/api/search/facets/ ++

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Facet.



11
12
13
14
15
# File 'lib/tire/search/facet.rb', line 11

def initialize(name, options={}, &block)
  @name    = name
  @options = options
  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
# File 'lib/tire/search/facet.rb', line 24

def date(field, options={})
  interval = options.delete(:interval) || 'day'
  @value = { :date_histogram => { :field => field, :interval => interval }.update(options) }
  self
end

#filter(field, value, options = {}) ⇒ Object



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

def filter(field, value, options={})
  @value = { :filter => { :term => { field => value }}.update(options) }
  self
end

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



35
36
37
38
# File 'lib/tire/search/facet.rb', line 35

def histogram(field, options={})
  @value = { :histogram => (options.delete(:histogram) || {:field => field}.update(options)) }
  self
end

#query(&block) ⇒ Object



50
51
52
# File 'lib/tire/search/facet.rb', line 50

def query(&block)
  @value = { :query => Query.new(&block).to_hash }
end

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



30
31
32
33
# File 'lib/tire/search/facet.rb', line 30

def range(field, ranges=[], options={})
  @value = { :range => { :field => field, :ranges => ranges }.update(options) }
  self
end

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



40
41
42
43
# File 'lib/tire/search/facet.rb', line 40

def statistical(field, options={})
  @value = { :statistical => (options.delete(:statistical) || {:field => field}.update(options)) }
  self
end

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



17
18
19
20
21
22
# File 'lib/tire/search/facet.rb', line 17

def terms(field, options={})
  size      = options.delete(:size) || 10
  all_terms = options.delete(:all_terms) || false
  @value = { :terms => { :field => field, :size => size, :all_terms => all_terms }.update(options) }
  self
end

#terms_stats(key_field, value_field, options = {}) ⇒ Object



45
46
47
48
# File 'lib/tire/search/facet.rb', line 45

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



63
64
65
66
# File 'lib/tire/search/facet.rb', line 63

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

#to_jsonObject



59
60
61
# File 'lib/tire/search/facet.rb', line 59

def to_json
  to_hash.to_json
end