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



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

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



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

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

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



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

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

#query(&block) ⇒ Object



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

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

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



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

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

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



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

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
23
24
25
26
# 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 = if field.is_a?(Enumerable) and not field.is_a?(String)
    { :terms => { :fields => field }.update({ :size => size, :all_terms => all_terms }).update(options) }
  else
    { :terms => { :field => field  }.update({ :size => size, :all_terms => all_terms }).update(options) }
  end
  self
end

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



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

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



67
68
69
70
# File 'lib/tire/search/facet.rb', line 67

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

#to_jsonObject



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

def to_json
  to_hash.to_json
end