Class: ElasticRecord::Relation

Inherits:
Object
  • Object
show all
Includes:
Batches, Calculations, Delegation, FinderMethods, Hits, Merging, SearchMethods
Defined in:
lib/elastic_record/relation.rb,
lib/elastic_record/relation/hits.rb,
lib/elastic_record/relation/none.rb,
lib/elastic_record/relation/batches.rb,
lib/elastic_record/relation/merging.rb,
lib/elastic_record/relation/delegation.rb,
lib/elastic_record/relation/calculations.rb,
lib/elastic_record/relation/value_methods.rb,
lib/elastic_record/relation/finder_methods.rb,
lib/elastic_record/relation/search_methods.rb

Defined Under Namespace

Modules: Batches, Calculations, Delegation, FinderMethods, Hits, Merging, None, SearchMethods

Constant Summary collapse

MULTI_VALUE_METHODS =
[:extending, :filter, :order, :aggregation]
SINGLE_VALUE_METHODS =
[:query, :limit, :offset, :search_options, :search_type, :reverse_order]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Batches

#build_scroll_enumerator, #find_each, #find_hits_in_batches, #find_ids_in_batches, #find_in_batches, #reindex

Methods included from Calculations

#calculate

Methods included from Delegation

#include?, #to_ary

Methods included from FinderMethods

#all, #find, #find_by, #find_by!, #first, #first!, #last

Methods included from Hits

#search_hits, #search_results, #to_ids

Methods included from Merging

#merge, #merge!

Methods included from SearchMethods

#aggregate, #aggregate!, #as_elastic, #extending, #extending!, #filter, #filter!, #limit, #limit!, #none, #offset, #offset!, #order, #order!, #query, #query!, #reverse_order, #reverse_order!, #search_options, #search_options!, #search_type, #search_type!

Constructor Details

#initialize(klass, values: {}) ⇒ Relation

Returns a new instance of Relation.



17
18
19
20
# File 'lib/elastic_record/relation.rb', line 17

def initialize(klass, values: {})
  @klass = klass
  @values = values
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ElasticRecord::Relation::Delegation

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



15
16
17
# File 'lib/elastic_record/relation.rb', line 15

def klass
  @klass
end

#valuesObject (readonly)

Returns the value of attribute values.



15
16
17
# File 'lib/elastic_record/relation.rb', line 15

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
# File 'lib/elastic_record/relation.rb', line 66

def ==(other)
  to_a == other
end

#aggregationsObject



38
39
40
41
42
43
# File 'lib/elastic_record/relation.rb', line 38

def aggregations
  @aggregations ||= begin
    results = search_results['aggregations']
    ElasticRecord::AggregationResponse::Builder.extract(results)
  end
end

#becomes(klass) ⇒ Object



27
28
29
30
31
32
# File 'lib/elastic_record/relation.rb', line 27

def becomes(klass)
  became = klass.allocate
  became.instance_variable_set(:@klass, @klass)
  became.instance_variable_set(:@values, @values.dup)
  became
end

#countObject



34
35
36
# File 'lib/elastic_record/relation.rb', line 34

def count
  search_hits.total
end

#delete_allObject



61
62
63
64
# File 'lib/elastic_record/relation.rb', line 61

def delete_all
  find_ids_in_batches { |ids| klass.delete(ids) }
  klass.elastic_index.delete_by_query(as_elastic)
end

#explain(id) ⇒ Object



45
46
47
# File 'lib/elastic_record/relation.rb', line 45

def explain(id)
  klass.elastic_index.explain(id, as_elastic)
end

#find_hits(search_hits) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/elastic_record/relation.rb', line 53

def find_hits(search_hits)
  if klass.elastic_index.load_from_source
    search_hits.hits.map { |hit| klass.from_search_hit(hit) }
  else
    klass.find search_hits.to_ids
  end
end

#initialize_copy(other) ⇒ Object



22
23
24
25
# File 'lib/elastic_record/relation.rb', line 22

def initialize_copy(other)
  @values = @values.dup
  reset
end

#inspectObject



70
71
72
# File 'lib/elastic_record/relation.rb', line 70

def inspect
  to_a.inspect
end

#scopingObject



74
75
76
77
78
79
# File 'lib/elastic_record/relation.rb', line 74

def scoping
  previous, klass.current_elastic_search = klass.current_elastic_search, self
  yield
ensure
  klass.current_elastic_search = previous
end

#to_aObject



49
50
51
# File 'lib/elastic_record/relation.rb', line 49

def to_a
  @records ||= find_hits(search_hits)
end