Class: MmEsSearch::Api::Query::BoolQuery

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

Direct Known Subclasses

BoolFilter

Instance Method Summary collapse

Methods inherited from AbstractQuery

#es_abs_field, #mongo_abs_field, #path_and_index, #to_filter

Instance Method Details

#to_es_queryObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mm_es_search/api/query/bool_query.rb', line 34

def to_es_query
  
  validate
  
  # use more optimal and, or, not SingleBoolFilter if appropriate
  if self.is_a?(BoolFilter)
    if (shoulds + must_nots).empty? and not musts.empty? and boost.nil?
      return AndFilter.new(:filters => musts).to_es_query
    elsif (musts + must_nots).empty? and not shoulds.empty? and boost.nil? and minimum_number_should_match == 1
      return OrFilter.new(:filters => shoulds).to_es_query
    elsif (musts + shoulds).empty? and not must_nots.empty? and boost.nil?
      return NotFilter.new(:filters => must_nots).to_es_query
    end
  end
  
  must_array = musts.map {|query| query.to_es_query}
  should_array = shoulds.map {|query| query.to_es_query}
  must_not_array = must_nots.map {|query| query.to_es_query}
  
  bool_params = {}
  bool_params.merge!({:must => must_array}) unless must_array.empty?
  bool_params.merge!({:should => should_array}) unless should_array.empty?
  bool_params.merge!({:must_not => must_not_array}) unless must_not_array.empty?
  bool_params.merge!({:boost => boost}) unless boost.nil?
  bool_params.merge!({:minimum_number_should_match => minimum_number_should_match}) if minimum_number_should_match?
  return {:bool => bool_params}
  
end

#to_mongo_query(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mm_es_search/api/query/bool_query.rb', line 17

def to_mongo_query(options = {})
  
  validate
  
  negated_options = options.merge({:negated => !options[:negated]})
  
  and_array = musts.map {|query| query.to_mongo_query(options)} + must_nots.map {|query| query.to_mongo_query(negated_options)}
  or_array = shoulds.map {|query| query.to_mongo_query(options)}
  
  bool_params = {}
  bool_params.merge!({'$and' => and_array}) unless and_array.empty?
  bool_params.merge!({'$or' => or_array}) unless or_array.empty?
  
  return bool_params
  
end

#validateObject



13
14
15
# File 'lib/mm_es_search/api/query/bool_query.rb', line 13

def validate
  raise "cannot have a must_not by itself in a BoolQuery" if (musts.empty? and shoulds.empty? and not self.is_a?(BoolFilter))
end