Class: ElasticRecord::Relation

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

Direct Known Subclasses

SearchesMany::CollectionProxy

Defined Under Namespace

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

Constant Summary collapse

MULTI_VALUE_METHODS =
[:extending, :facet, :filter, :order, :select, :eager_load]
SINGLE_VALUE_METHODS =
[:query, :limit, :offset, :reverse_order]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Admin

#create_percolator, #create_warmer

Methods included from Batches

#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

#as_elastic, #eager_load, #eager_load!, #extending, #extending!, #facet, #facet!, #filter, #filter!, #find_by, #find_by!, #limit, #limit!, #none, #offset, #offset!, #order, #order!, #query, #query!, #reverse_order, #reverse_order!, #select, #select!

Constructor Details

#initialize(klass) ⇒ Relation

Returns a new instance of Relation.



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

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.



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

def klass
  @klass
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  to_a == other
end

#countObject



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

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

#eager_loading?Boolean

Returns:

  • (Boolean)


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

def eager_loading?
  @should_eager_load ||= eager_load_values.any?
end

#explain(id) ⇒ Object



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

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

#facetsObject



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

def facets
  search_results['facets']
end

#initialize_copy(other) ⇒ Object



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

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

#inspectObject



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

def inspect
  to_a.inspect
end

#scopingObject



62
63
64
65
66
67
# File 'lib/elastic_record/relation.rb', line 62

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

#to_aObject



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

def to_a
  @records ||= begin
    records = load_hits(to_ids)
    eager_load_associations(records) if eager_loading?
    records
  end
end

#to_idsObject



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

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