Class: Elastictastic::Scope
- Inherits:
-
BasicObject
- Defined in:
- lib/elastictastic/scope.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(index, clazz, search = Search.new, parent_collection = nil) ⇒ Scope
Returns a new instance of Scope.
7
8
9
10
|
# File 'lib/elastictastic/scope.rb', line 7
def initialize(index, clazz, search = Search.new, parent_collection = nil)
@index, @clazz, @search, @parent_collection =
index, clazz, search, parent_collection
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/elastictastic/scope.rb', line 131
def method_missing(method, *args, &block)
if ::Enumerable.method_defined?(method)
each.__send__(method, *args, &block)
elsif @clazz.respond_to?(method)
@clazz.with_scope(self) do
@clazz.__send__(method, *args, &block)
end
else
super
end
end
|
Instance Attribute Details
#clazz ⇒ Object
Returns the value of attribute clazz.
5
6
7
|
# File 'lib/elastictastic/scope.rb', line 5
def clazz
@clazz
end
|
#index ⇒ Object
Returns the value of attribute index.
5
6
7
|
# File 'lib/elastictastic/scope.rb', line 5
def index
@index
end
|
Instance Method Details
#all ⇒ Object
72
73
74
|
# File 'lib/elastictastic/scope.rb', line 72
def all
scoped({})
end
|
#all_facets ⇒ Object
76
77
78
79
80
|
# File 'lib/elastictastic/scope.rb', line 76
def all_facets
return @all_facets if defined? @all_facets
populate_counts
@all_facets ||= nil
end
|
#any?(&block) ⇒ Boolean
58
59
60
|
# File 'lib/elastictastic/scope.rb', line 58
def any?(&block)
block ? each.any?(&block) : !empty?
end
|
#count ⇒ Object
48
49
50
51
52
|
# File 'lib/elastictastic/scope.rb', line 48
def count
return @count if defined? @count
populate_counts
@count
end
|
#destroy_all ⇒ Object
91
92
93
94
|
# File 'lib/elastictastic/scope.rb', line 91
def destroy_all
::Elastictastic.client.delete(@index, @clazz.type)
end
|
#each ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/elastictastic/scope.rb', line 21
def each
if ::Kernel.block_given?
find_each { |result, hit| yield result }
else
::Enumerator.new(self, :each)
end
end
|
#empty? ⇒ Boolean
54
55
56
|
# File 'lib/elastictastic/scope.rb', line 54
def empty?
count == 0
end
|
#find(*ids) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/elastictastic/scope.rb', line 101
def find(*ids)
force_array = ::Array === ids.first
ids = ids.flatten
if ::Hash === ids.first
find_many_in_many_indices(*ids)
elsif ids.length == 1
instance = find_one(ids.first)
force_array ? [instance] : instance
else
find_many(ids)
end
end
|
#find_each(batch_options = {}, &block) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/elastictastic/scope.rb', line 29
def find_each(batch_options = {}, &block)
if block
find_in_batches(batch_options) { |batch| batch.each(&block) }
else
::Enumerator.new(self, :find_each, batch_options)
end
end
|
#find_in_batches(batch_options = {}, &block) ⇒ Object
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/elastictastic/scope.rb', line 37
def find_in_batches(batch_options = {}, &block)
return ::Enumerator.new(self, :find_in_batches, batch_options) unless block
if params.key?('size') || params.key?('from')
yield search_all
elsif params.key?('sort') || params.key('facets')
search_in_batches(&block)
else
scan_in_batches(batch_options, &block)
end
end
|
#first ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/elastictastic/scope.rb', line 62
def first
params = from(0).size(1).params
hit = ::Elastictastic.client.search(
@index,
@clazz.type,
params
)['hits']['hits'].first
materialize_hit(hit) if hit
end
|
#initialize_instance(instance) ⇒ Object
12
13
14
15
|
# File 'lib/elastictastic/scope.rb', line 12
def initialize_instance(instance)
index = @index
instance.instance_eval { @index = index }
end
|
#inspect ⇒ Object
143
144
145
146
147
|
# File 'lib/elastictastic/scope.rb', line 143
def inspect
inspected = "#{@clazz.name}:#{@index.name}"
inspected << @search.params.to_json unless @search.params.empty?
inspected
end
|
#params ⇒ Object
17
18
19
|
# File 'lib/elastictastic/scope.rb', line 17
def params
@search.params
end
|
#scoped(params, index = @index) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/elastictastic/scope.rb', line 82
def scoped(params, index = @index)
::Elastictastic::Scope.new(
@index,
@clazz,
@search.merge(Search.new(params)),
@parent_collection
)
end
|
#sync_mapping ⇒ Object
96
97
98
99
|
# File 'lib/elastictastic/scope.rb', line 96
def sync_mapping
::Elastictastic.client.put_mapping(index, type, @clazz.mapping)
end
|