Class: MmEsSearch::Api::Query::SingleBoolFilter

Inherits:
AbstractQuery show all
Defined in:
lib/mm_es_search/api/query/single_bool_filter.rb

Direct Known Subclasses

AndFilter, NotFilter, OrFilter

Instance Method Summary collapse

Methods inherited from AbstractQuery

#es_abs_field, #mongo_abs_field, #path_and_index, #to_filter

Instance Method Details

#operator_nameObject



12
13
14
# File 'lib/mm_es_search/api/query/single_bool_filter.rb', line 12

def operator_name
  self.class.to_s[0..-7].split('::').last.downcase #strip off "Filter" and downcase e.g. AndFilter => filter
end

#optimize_filtersObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mm_es_search/api/query/single_bool_filter.rb', line 16

def optimize_filters
  opt_filters = []
  filters.each do |child_filter|
    if child_filter.is_a?(self.class)
      opt_filters += child_filter.filters
    else
      opt_filters << child_filter
    end
  end
  opt_filters
end

#to_es_queryObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mm_es_search/api/query/single_bool_filter.rb', line 44

def to_es_query
  opt_filters = optimize_filters
  filter_array = opt_filters.map {|query| query.to_es_query}
  params = {}
  case self
  when NotFilter
    if filter_array.length == 1
      return {operator_name => {:filter => filter_array.first}}
    else
      return {operator_name => {:filter => {:or => filter_array}}}
    end
  else
    params = {operator_name => filter_array}
  end
  params.merge!({"_cache" => _cache}) unless _cache.nil?
  return params
end

#to_mongo_query(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mm_es_search/api/query/single_bool_filter.rb', line 28

def to_mongo_query(options = {})
  case self
  when NotFilter
    negated_options = options.merge({:negated => !options[:negated]})
    return AndFilter.new(:filters => filters).to_mongo_query(negated_options)
  else
    opt_filters = optimize_filters
    filter_array = opt_filters.map {|filter| filter.to_mongo_query(options)}
    if filter_array.length == 1
      return filter_array.first
    else
      return {"$#{operator_name}" => filter_array}
    end
  end
end