Class: Slingshot::Search::Facet

Inherits:
Object
  • Object
show all
Defined in:
lib/slingshot/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/slingshot/search/facet.rb', line 11

def initialize(name, options={}, &block)
  @name    = name
  @options = options
  self.instance_eval(&block) if block_given?
end

Instance Method Details

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



24
25
26
27
28
# File 'lib/slingshot/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

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



17
18
19
20
21
22
# File 'lib/slingshot/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

#to_hashObject



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

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

#to_jsonObject



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

def to_json
  to_hash.to_json
end