Class: MmEsSearch::Api::Query::NestedQuery

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

Direct Known Subclasses

NestedFilter

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mm_es_search/api/query/nested_query.rb', line 25

def to_es_query
  
  mod_path, array_index = path_and_index
  mod_query = if array_index.nil?
    query
  else
    query_array = [
      query,
      TermQuery.new(:path => mod_path, :field => array_index_name || '_array_index', :value => array_index)]
    anded_query(query_array)
  end

  nested_params = {
    es_api_keyword => mod_query.to_es_query,
    :path => mod_path
  }
  nested_params.merge!({:score_mode => score_mode}) unless score_mode.nil?
  nested_params.merge!({:_scope => _scope}) unless _scope.nil?
      
  return {:nested => nested_params}
      
end

#to_mongo_query(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mm_es_search/api/query/nested_query.rb', line 13

def to_mongo_query(options = {})
  
  mod_path, array_index = path_and_index
  if array_index.nil?
    #NOTE: we assume here that earlier terms in a dot-joined path has been used in an enclosing nested query
    {path.split('.').last => {'$elemMatch' => query.to_mongo_query(options)}}
  else
    query.to_mongo_query(options) #don't need elemMatch as index means we're addressing one document only
  end
  
end