Class: Elastics::InstanceProxy::ModelIndexer
Instance Attribute Summary collapse
Attributes inherited from ModelSyncer
#class_elastics, #instance
Instance Method Summary
collapse
Methods inherited from ModelSyncer
#initialize, #refresh_index, #sync, #uid
Instance Attribute Details
#index ⇒ Object
57
58
59
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 57
def index
@index ||= instance.respond_to?(:elastics_index) ? instance.elastics_index : class_elastics.index
end
|
#parent ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 84
def parent
@parent ||= case
when instance.respond_to?(:elastics_parent) then instance.elastics_parent
when is_child? then parent_instance.id.to_s
else nil
end
end
|
#routing ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 75
def routing
@routing ||= case
when instance.respond_to?(:elastics_routing) then instance.elastics_routing
when is_child? then parent_instance.elastics.routing
when is_parent? then id
end
end
|
#type ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 62
def type
@type ||= case
when instance.respond_to?(:elastics_type) then instance.elastics_type
when is_child? then class_elastics.parent_child_map[parent_instance.elastics.type]
else class_elastics.type
end
end
|
Instance Method Details
#each_parent ⇒ Object
helper that iterates through the parent record chain record.elastics.each_parent{|p| p.do_something }
49
50
51
52
53
54
55
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 49
def each_parent
pi = parent_instance
while pi do
yield pi
pi = pi.elastics.parent_instance
end
end
|
#full_get(*vars) ⇒ Object
like get, but it returns all the source fields after a refresh
36
37
38
39
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 36
def full_get(*vars)
return unless instance.elastics_indexable?
Elastics.search_by_id(metainfo, {:refresh => true, :params => {:_source => '*'}}, *vars)
end
|
#get(*vars) ⇒ Object
gets the document from ES
30
31
32
33
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 30
def get(*vars)
return unless instance.elastics_indexable?
Elastics.get(metainfo, *vars)
end
|
#id ⇒ Object
71
72
73
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 71
def id
@id ||= instance.respond_to?(:elastics_id) ? instance.elastics_id : instance.id.to_s
end
|
93
94
95
96
97
98
99
100
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 93
def metainfo
meta = Vars.new( :index => index, :type => type, :id => id )
params = {}
params[:routing] = routing if routing
params[:parent] = parent if parent
meta.merge!(:params => params) unless params.empty?
meta
end
|
#parent_instance ⇒ Object
41
42
43
44
45
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 41
def parent_instance
return unless is_child?
@parent_instance ||= instance.send(class_elastics.parent_association) ||
raise(MissingParentError, "missing parent instance for document #{instance.inspect}.")
end
|
#remove(*vars) ⇒ Object
removes the document from the index (called from after_destroy)
24
25
26
27
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 24
def remove(*vars)
return unless instance.elastics_indexable?
Elastics.remove(metainfo, *vars)
end
|
#store(*vars) ⇒ Object
indexes the document usually called from after_save, you can eventually call it explicitly for example from another callback or whenever the DB doesn’t get updated by the model you can also pass the :data=>elastics_source explicitly (useful for example to override the elastics_source in the model)
15
16
17
18
19
20
21
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 15
def store(*vars)
if instance.elastics_indexable?
Elastics.store(metainfo, {:data => instance.elastics_source}, *vars)
else
Elastics.remove(metainfo, *vars) if Elastics.get(metainfo, *vars, :raise => false)
end
end
|
#sync_self ⇒ Object
102
103
104
|
# File 'lib/elastics/instance_proxy/model_indexer.rb', line 102
def sync_self
instance.destroyed? ? remove : store
end
|