Module: DynamicSunspotSearch::Translator::Facet

Defined in:
lib/dynamic_sunspot_search/translator/facet.rb

Class Method Summary collapse

Class Method Details

.apply(query_object, options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dynamic_sunspot_search/translator/facet.rb', line 7

def self.apply(query_object, options)
  return unless options.present?
  query_object.tap do |search|
    case options
    when String, Symbol, Array
      search.facet *options
    when Hash
      fields = options.values_at(:field, :fields).flatten.compact
      exclude_filter = get_exclude_filter(search, options.delete(:exclude))
      search.facet(*fields, exclude: exclude_filter)
    else
      raise NotImplementedError
    end
  end
end

.get_exclude_filter(query_object, options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dynamic_sunspot_search/translator/facet.rb', line 23

def self.get_exclude_filter(query_object, options)
  case options
  when Array
    options.map do |option|
      get_exclude_filter(query_object, option)
    end.flatten
  when Hash
    [
      With.apply(query_object, options.delete(:with)),
      Without.apply(query_object, options.delete(:without)),
    ].flatten.compact
  else
    raise NotImplementedError
  end
end