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/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, SearchMethods

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Batches

#find_each, #find_in_batches, #reindex

Methods included from Delegation

#include?, #to_ary

Methods included from FinderMethods

#all, #find, #first, #last

Methods included from Merging

#merge, #merge!

Methods included from SearchMethods

#as_elastic, #extending, #extending!, #facet, #facet!, #filter, #filter!, #limit, #limit!, #negate, #offset, #offset!, #order, #order!, #query, #query!, #select, #select!

Constructor Details

#initialize(klass, arelastic) ⇒ Relation

Returns a new instance of Relation.



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

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

Dynamic Method Handling

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

Instance Attribute Details

#arelasticObject (readonly)

Returns the value of attribute arelastic.



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

def arelastic
  @arelastic
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  case other
  when Relation
    other.as_elastic == as_elastic
  when Array
    to_a == other
  end
end

#countObject



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

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

#create_percolator(name) ⇒ Object



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

def create_percolator(name)
  klass.elastic_index.create_percolator(name, as_elastic)
end

#explain(id) ⇒ Object



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

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

#facetsObject



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

def facets
  search_results['facets']
end

#initialize_copy(other) ⇒ Object



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

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

#inspectObject



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

def inspect
  to_a.inspect
end

#scopingObject



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

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

#to_aObject



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

def to_a
  @records ||= begin
    scope = select_values.any? ? klass.select(select_values) : klass
    scope.find(to_ids)
  end
end

#to_idsObject



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

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