Class: ElasticRecord::Relation

Inherits:
Object
  • Object
show all
Includes:
Batches, Delegation, FinderMethods, Merging, SearchMethods
Defined in:
lib/elastic_record/relation.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/value_methods.rb,
lib/elastic_record/relation/finder_methods.rb,
lib/elastic_record/relation/search_methods.rb

Defined Under Namespace

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

Constant Summary collapse

MULTI_VALUE_METHODS =
[:extending, :filter, :order, :select, :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_ids_in_batches, #find_in_batches, #reindex

Methods included from Delegation

#include?, #to_ary

Methods included from FinderMethods

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

Methods included from Merging

#merge, #merge!

Methods included from SearchMethods

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

Constructor Details

#initialize(klass) ⇒ Relation

Returns a new instance of Relation.



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

def initialize(klass)
  @klass = klass
  @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.



13
14
15
# File 'lib/elastic_record/relation.rb', line 13

def klass
  @klass
end

#valuesObject (readonly)

Returns the value of attribute values.



13
14
15
# File 'lib/elastic_record/relation.rb', line 13

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  to_a == other
end

#aggregationsObject



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

def aggregations
  search_results['aggregations']
end

#countObject



20
21
22
# File 'lib/elastic_record/relation.rb', line 20

def count
  search_results['hits']['total']
end

#delete_allObject



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

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

#explain(id) ⇒ Object



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

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

#initialize_copy(other) ⇒ Object



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

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

#inspectObject



54
55
56
# File 'lib/elastic_record/relation.rb', line 54

def inspect
  to_a.inspect
end

#scopingObject



58
59
60
61
62
63
# File 'lib/elastic_record/relation.rb', line 58

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

#to_aObject



37
38
39
# File 'lib/elastic_record/relation.rb', line 37

def to_a
  @records ||= load_hits
end

#to_idsObject



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

def to_ids
  search_hits.map { |hit| hit['_id'] }
end