stretchy-model 0.6.5 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +2 -1
- data/README.md +28 -10
- data/Rakefile +56 -0
- data/docs/.nojekyll +0 -0
- data/docs/README.md +147 -0
- data/docs/_coverpage.md +14 -0
- data/docs/_sidebar.md +14 -0
- data/docs/examples/_sidebar.md +15 -0
- data/docs/examples/data_analysis.md +216 -0
- data/docs/examples/semantic_search_with_llm.md +83 -0
- data/docs/examples/simple-ingest-pipeline.md +326 -0
- data/docs/guides/_sidebar.md +14 -0
- data/docs/guides/aggregations.md +142 -0
- data/docs/guides/machine-learning.md +154 -0
- data/docs/guides/models.md +372 -0
- data/docs/guides/pipelines.md +151 -0
- data/docs/guides/querying.md +361 -0
- data/docs/guides/quick-start.md +72 -0
- data/docs/guides/scopes.md +125 -0
- data/docs/index.html +113 -0
- data/docs/stretchy.cover.png +0 -0
- data/docs/stretchy.logo.png +0 -0
- data/docs/styles.css +90 -0
- data/lib/stretchy/attributes/transformers/keyword_transformer.rb +41 -35
- data/lib/stretchy/attributes/type/array.rb +24 -1
- data/lib/stretchy/attributes/type/base.rb +6 -2
- data/lib/stretchy/attributes/type/binary.rb +24 -17
- data/lib/stretchy/attributes/type/boolean.rb +29 -22
- data/lib/stretchy/attributes/type/completion.rb +18 -10
- data/lib/stretchy/attributes/type/constant_keyword.rb +35 -26
- data/lib/stretchy/attributes/type/date_time.rb +28 -17
- data/lib/stretchy/attributes/type/dense_vector.rb +46 -49
- data/lib/stretchy/attributes/type/flattened.rb +28 -19
- data/lib/stretchy/attributes/type/geo_point.rb +21 -12
- data/lib/stretchy/attributes/type/geo_shape.rb +21 -12
- data/lib/stretchy/attributes/type/hash.rb +24 -10
- data/lib/stretchy/attributes/type/histogram.rb +25 -0
- data/lib/stretchy/attributes/type/ip.rb +26 -17
- data/lib/stretchy/attributes/type/join.rb +16 -7
- data/lib/stretchy/attributes/type/keyword.rb +21 -26
- data/lib/stretchy/attributes/type/knn_vector.rb +47 -0
- data/lib/stretchy/attributes/type/match_only_text.rb +22 -1
- data/lib/stretchy/attributes/type/nested.rb +16 -11
- data/lib/stretchy/attributes/type/numeric/base.rb +30 -22
- data/lib/stretchy/attributes/type/numeric/byte.rb +20 -0
- data/lib/stretchy/attributes/type/numeric/double.rb +20 -0
- data/lib/stretchy/attributes/type/numeric/float.rb +20 -0
- data/lib/stretchy/attributes/type/numeric/half_float.rb +20 -0
- data/lib/stretchy/attributes/type/numeric/integer.rb +21 -1
- data/lib/stretchy/attributes/type/numeric/long.rb +20 -0
- data/lib/stretchy/attributes/type/numeric/scaled_float.rb +16 -7
- data/lib/stretchy/attributes/type/numeric/short.rb +20 -0
- data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +21 -1
- data/lib/stretchy/attributes/type/percolator.rb +16 -4
- data/lib/stretchy/attributes/type/point.rb +19 -9
- data/lib/stretchy/attributes/type/range/base.rb +24 -1
- data/lib/stretchy/attributes/type/range/date_range.rb +21 -5
- data/lib/stretchy/attributes/type/range/double_range.rb +20 -4
- data/lib/stretchy/attributes/type/range/float_range.rb +21 -5
- data/lib/stretchy/attributes/type/range/integer_range.rb +20 -4
- data/lib/stretchy/attributes/type/range/ip_range.rb +20 -4
- data/lib/stretchy/attributes/type/range/long_range.rb +20 -4
- data/lib/stretchy/attributes/type/rank_feature.rb +16 -6
- data/lib/stretchy/attributes/type/rank_features.rb +16 -9
- data/lib/stretchy/attributes/type/search_as_you_type.rb +28 -18
- data/lib/stretchy/attributes/type/shape.rb +19 -9
- data/lib/stretchy/attributes/type/sparse_vector.rb +25 -21
- data/lib/stretchy/attributes/type/string.rb +42 -1
- data/lib/stretchy/attributes/type/text.rb +53 -28
- data/lib/stretchy/attributes/type/token_count.rb +21 -11
- data/lib/stretchy/attributes/type/version.rb +16 -6
- data/lib/stretchy/attributes/type/wildcard.rb +36 -25
- data/lib/stretchy/attributes.rb +29 -0
- data/lib/stretchy/delegation/gateway_delegation.rb +78 -0
- data/lib/stretchy/index_setting.rb +94 -0
- data/lib/stretchy/indexing/bulk.rb +75 -3
- data/lib/stretchy/model/callbacks.rb +1 -0
- data/lib/stretchy/model/common.rb +157 -0
- data/lib/stretchy/model/persistence.rb +144 -0
- data/lib/stretchy/model/refreshable.rb +26 -0
- data/lib/stretchy/pipeline.rb +2 -1
- data/lib/stretchy/pipelines/processor.rb +38 -36
- data/lib/stretchy/querying.rb +7 -8
- data/lib/stretchy/record.rb +5 -4
- data/lib/stretchy/relation.rb +229 -28
- data/lib/stretchy/relations/aggregation_methods/aggregation.rb +59 -0
- data/lib/stretchy/relations/aggregation_methods/avg.rb +45 -0
- data/lib/stretchy/relations/aggregation_methods/bucket_script.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/bucket_selector.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/bucket_sort.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/cardinality.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/children.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/composite.rb +41 -0
- data/lib/stretchy/relations/aggregation_methods/date_histogram.rb +53 -0
- data/lib/stretchy/relations/aggregation_methods/date_range.rb +53 -0
- data/lib/stretchy/relations/aggregation_methods/extended_stats.rb +48 -0
- data/lib/stretchy/relations/aggregation_methods/filter.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/filters.rb +47 -0
- data/lib/stretchy/relations/aggregation_methods/geo_bounds.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/geo_centroid.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/global.rb +39 -0
- data/lib/stretchy/relations/aggregation_methods/histogram.rb +43 -0
- data/lib/stretchy/relations/aggregation_methods/ip_range.rb +41 -0
- data/lib/stretchy/relations/aggregation_methods/max.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/min.rb +41 -0
- data/lib/stretchy/relations/aggregation_methods/missing.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/nested.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/percentile_ranks.rb +45 -0
- data/lib/stretchy/relations/aggregation_methods/percentiles.rb +45 -0
- data/lib/stretchy/relations/aggregation_methods/range.rb +42 -0
- data/lib/stretchy/relations/aggregation_methods/reverse_nested.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/sampler.rb +40 -0
- data/lib/stretchy/relations/aggregation_methods/scripted_metric.rb +43 -0
- data/lib/stretchy/relations/aggregation_methods/significant_terms.rb +45 -0
- data/lib/stretchy/relations/aggregation_methods/stats.rb +42 -0
- data/lib/stretchy/relations/aggregation_methods/sum.rb +42 -0
- data/lib/stretchy/relations/aggregation_methods/terms.rb +46 -0
- data/lib/stretchy/relations/aggregation_methods/top_hits.rb +42 -0
- data/lib/stretchy/relations/aggregation_methods/top_metrics.rb +44 -0
- data/lib/stretchy/relations/aggregation_methods/value_count.rb +41 -0
- data/lib/stretchy/relations/aggregation_methods/weighted_avg.rb +42 -0
- data/lib/stretchy/relations/aggregation_methods.rb +20 -749
- data/lib/stretchy/relations/finder_methods.rb +2 -18
- data/lib/stretchy/relations/null_relation.rb +55 -0
- data/lib/stretchy/relations/query_builder.rb +82 -36
- data/lib/stretchy/relations/query_methods/bind.rb +19 -0
- data/lib/stretchy/relations/query_methods/extending.rb +29 -0
- data/lib/stretchy/relations/query_methods/fields.rb +70 -0
- data/lib/stretchy/relations/query_methods/filter_query.rb +53 -0
- data/lib/stretchy/relations/query_methods/has_field.rb +40 -0
- data/lib/stretchy/relations/query_methods/highlight.rb +75 -0
- data/lib/stretchy/relations/query_methods/hybrid.rb +60 -0
- data/lib/stretchy/relations/query_methods/ids.rb +40 -0
- data/lib/stretchy/relations/query_methods/match.rb +52 -0
- data/lib/stretchy/relations/query_methods/must_not.rb +54 -0
- data/lib/stretchy/relations/query_methods/neural.rb +58 -0
- data/lib/stretchy/relations/query_methods/neural_sparse.rb +43 -0
- data/lib/stretchy/relations/query_methods/none.rb +21 -0
- data/lib/stretchy/relations/query_methods/or_filter.rb +21 -0
- data/lib/stretchy/relations/query_methods/order.rb +63 -0
- data/lib/stretchy/relations/query_methods/query_string.rb +44 -0
- data/lib/stretchy/relations/query_methods/regexp.rb +61 -0
- data/lib/stretchy/relations/query_methods/should.rb +51 -0
- data/lib/stretchy/relations/query_methods/size.rb +44 -0
- data/lib/stretchy/relations/query_methods/skip_callbacks.rb +47 -0
- data/lib/stretchy/relations/query_methods/source.rb +59 -0
- data/lib/stretchy/relations/query_methods/where.rb +113 -0
- data/lib/stretchy/relations/query_methods.rb +48 -569
- data/lib/stretchy/relations/scoping/default.rb +136 -0
- data/lib/stretchy/relations/scoping/named.rb +70 -0
- data/lib/stretchy/relations/scoping/scope_registry.rb +36 -0
- data/lib/stretchy/relations/scoping.rb +30 -0
- data/lib/stretchy/relations/search_option_methods.rb +2 -0
- data/lib/stretchy/version.rb +1 -1
- data/lib/stretchy.rb +17 -10
- metadata +111 -17
- data/lib/stretchy/common.rb +0 -38
- data/lib/stretchy/null_relation.rb +0 -53
- data/lib/stretchy/persistence.rb +0 -43
- data/lib/stretchy/refreshable.rb +0 -15
- data/lib/stretchy/scoping/default.rb +0 -134
- data/lib/stretchy/scoping/named.rb +0 -68
- data/lib/stretchy/scoping/scope_registry.rb +0 -34
- data/lib/stretchy/scoping.rb +0 -28
data/lib/stretchy/relation.rb
CHANGED
@@ -3,6 +3,7 @@ module Stretchy
|
|
3
3
|
# It provides methods for querying and manipulating the documents.
|
4
4
|
class Relation
|
5
5
|
|
6
|
+
|
6
7
|
# These methods cannot be used with the `delete_all` method.
|
7
8
|
INVALID_METHODS_FOR_DELETE_ALL = [:limit, :offset]
|
8
9
|
|
@@ -14,7 +15,6 @@ module Stretchy
|
|
14
15
|
Relations::SearchOptionMethods,
|
15
16
|
Delegation
|
16
17
|
|
17
|
-
# Getters.
|
18
18
|
attr_reader :klass, :loaded
|
19
19
|
alias :model :klass
|
20
20
|
alias :loaded? :loaded
|
@@ -22,10 +22,28 @@ module Stretchy
|
|
22
22
|
# Delegates to the results array.
|
23
23
|
delegate :blank?, :empty?, :any?, :many?, to: :results
|
24
24
|
|
25
|
-
#
|
25
|
+
# Initialize a new instance of the relation.
|
26
|
+
#
|
27
|
+
# This constructor method is used to create a new instance of the relation. It accepts a class representing the Elasticsearch documents and a hash of initial values for the relation.
|
28
|
+
#
|
29
|
+
# ### Parameters
|
30
|
+
#
|
31
|
+
# - `klass:` The Class representing the Elasticsearch documents.
|
32
|
+
# - `values:` The Hash representing the initial values for the relation (optional).
|
33
|
+
#
|
34
|
+
# ### Returns
|
35
|
+
# Returns a new instance of the relation.
|
36
|
+
#
|
37
|
+
# ---
|
38
|
+
#
|
39
|
+
# ### Examples
|
40
|
+
#
|
41
|
+
# #### Initialize a new relation
|
42
|
+
#
|
43
|
+
# ```ruby
|
44
|
+
# relation = Relation.new(Model, {name: 'John Doe', age: 30})
|
45
|
+
# ```
|
26
46
|
#
|
27
|
-
# @param klass [Class] The class of the Elasticsearch documents.
|
28
|
-
# @param values [Hash] The initial values for the relation.
|
29
47
|
def initialize(klass, values={})
|
30
48
|
@klass = klass
|
31
49
|
@values = values.merge(default_size: klass.default_size)
|
@@ -33,54 +51,173 @@ module Stretchy
|
|
33
51
|
@loaded = false
|
34
52
|
end
|
35
53
|
|
36
|
-
#
|
54
|
+
# Initialize a new instance of the relation.
|
55
|
+
#
|
56
|
+
# This constructor method is used to create a new instance of the relation. It accepts a class representing the Elasticsearch documents and a hash of initial values for the relation.
|
57
|
+
#
|
58
|
+
# ### Parameters
|
59
|
+
#
|
60
|
+
# - `klass:` The Class representing the Elasticsearch documents.
|
61
|
+
# - `values:` The Hash representing the initial values for the relation (optional).
|
62
|
+
#
|
63
|
+
# ### Returns
|
64
|
+
# Returns a new instance of the relation.
|
65
|
+
#
|
66
|
+
# ---
|
67
|
+
#
|
68
|
+
# ### Examples
|
69
|
+
#
|
70
|
+
# #### Initialize a new relation
|
71
|
+
#
|
72
|
+
# ```ruby
|
73
|
+
# relation = Relation.new(Model, {name: 'John Doe', age: 30})
|
74
|
+
# ```
|
37
75
|
#
|
38
|
-
# @param args [Array] The arguments to pass to the document constructor.
|
39
|
-
# @return [Object] The new document.
|
40
76
|
def build(*args)
|
41
77
|
@klass.new *args
|
42
78
|
end
|
43
79
|
|
44
80
|
# Returns the results of the relation as an array.
|
45
81
|
#
|
46
|
-
#
|
47
|
-
|
82
|
+
# This instance method is used to load the results of the relation and return them as an array. It calls the `load` method to load the results and then returns the `@records` instance variable.
|
83
|
+
#
|
84
|
+
# ### Returns
|
85
|
+
# Returns an Array representing the results of the relation.
|
86
|
+
#
|
87
|
+
# ---
|
88
|
+
#
|
89
|
+
# ### Examples
|
90
|
+
#
|
91
|
+
# #### Get the results of a relation as an array
|
92
|
+
#
|
93
|
+
# ```ruby
|
94
|
+
# relation = Relation.new(Model, {name: 'John Doe', age: 30})
|
95
|
+
# results = relation.results
|
96
|
+
# ```
|
97
|
+
#
|
98
|
+
def results
|
48
99
|
load
|
49
100
|
@records
|
50
101
|
end
|
51
|
-
alias :results :to_a
|
52
102
|
|
103
|
+
# _alias for `results`_
|
104
|
+
alias :to_a :results
|
105
|
+
|
106
|
+
# Returns the raw Elasticsearch response for the relation.
|
107
|
+
#
|
108
|
+
# This instance method is used to get the raw Elasticsearch response for the relation. It calls the `results` method to load the results and then returns the `response` property of the results.
|
109
|
+
#
|
110
|
+
# ### Returns
|
111
|
+
# Returns a Hash representing the raw Elasticsearch response for the relation.
|
112
|
+
#
|
113
|
+
# ---
|
114
|
+
#
|
115
|
+
# ### Examples
|
116
|
+
#
|
117
|
+
# #### Get the raw Elasticsearch response for a relation
|
118
|
+
#
|
119
|
+
# ```ruby
|
120
|
+
# relation = Relation.new(Model, {name: 'John Doe', age: 30})
|
121
|
+
# raw_response = relation.response
|
122
|
+
# ```
|
123
|
+
#
|
124
|
+
# #### With a model
|
125
|
+
# ```ruby
|
126
|
+
# relation = Model.where(name: 'John Doe')
|
127
|
+
# raw_response = relation.response
|
128
|
+
# #=> {"took"=>3, "timed_out"=>false, "_shards"=>{"total"=>1, "successful"=>1, "skipped"=>0, "failed"=>0}, "hits"=>{"total"=>{"value"=>0, "relation"=>"eq"}, "max_score"=>nil, "hits"=>[]}}
|
129
|
+
# ```
|
130
|
+
#
|
53
131
|
def response
|
54
132
|
results.response
|
55
133
|
end
|
56
134
|
|
57
135
|
# Returns the results of the relation as a JSON object.
|
58
136
|
#
|
59
|
-
#
|
60
|
-
#
|
137
|
+
# This instance method is used to get the results of the relation as a JSON object. It calls the `as_json` method on the results of the relation and passes any provided options to it.
|
138
|
+
#
|
139
|
+
# ### Parameters
|
140
|
+
#
|
141
|
+
# - `options:` The Hash representing the options to pass to the `as_json` method (optional).
|
142
|
+
#
|
143
|
+
# ### Returns
|
144
|
+
# Returns a Hash representing the results of the relation as a JSON object.
|
145
|
+
#
|
146
|
+
# ---
|
147
|
+
#
|
148
|
+
# ### Examples
|
149
|
+
#
|
150
|
+
# #### Get the results of a relation as a JSON object
|
151
|
+
#
|
152
|
+
# ```ruby
|
153
|
+
# relation = Relation.new(Model, {name: 'John Doe', age: 30})
|
154
|
+
# json_results = relation.as_json
|
155
|
+
# ```
|
156
|
+
#
|
61
157
|
def as_json(options = nil)
|
62
158
|
results.as_json(options)
|
63
159
|
end
|
64
160
|
|
65
161
|
# Returns the Elasticsearch query for the relation.
|
66
162
|
#
|
67
|
-
#
|
163
|
+
# This instance method is used to get the Elasticsearch query for the relation. It calls the `to_elastic` method on the `query_builder` of the relation.
|
164
|
+
#
|
165
|
+
# ### Returns
|
166
|
+
# Returns a Hash representing the Elasticsearch query for the relation.
|
167
|
+
#
|
168
|
+
# ---
|
169
|
+
#
|
170
|
+
# ### Examples
|
171
|
+
#
|
172
|
+
# #### Get the Elasticsearch query for a relation
|
173
|
+
#
|
174
|
+
# ```ruby
|
175
|
+
# Model.where(flight: 'goat').to_elastic
|
176
|
+
# #=> {"query"=>{"bool"=>{"must"=>{"term"=>{"flight.keyword"=>"goat"}}}}}
|
177
|
+
# ```
|
178
|
+
#
|
68
179
|
def to_elastic
|
69
180
|
query_builder.to_elastic
|
70
181
|
end
|
71
182
|
|
72
183
|
# Creates a new Elasticsearch document.
|
73
184
|
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
185
|
+
# This instance method is used to create a new Elasticsearch document within the scope of the relation. It accepts a list of arguments and an optional block to pass to the document constructor.
|
186
|
+
#
|
187
|
+
# ### Parameters
|
188
|
+
#
|
189
|
+
# - `*args:` A list of arguments to pass to the document constructor.
|
190
|
+
# - `&block:` An optional block to pass to the document constructor.
|
191
|
+
#
|
192
|
+
# ### Returns
|
193
|
+
# Returns the newly created document.
|
194
|
+
#
|
195
|
+
# ---
|
196
|
+
#
|
197
|
+
# ### Examples
|
198
|
+
#
|
199
|
+
# #### Create a new document within the scope of a relation
|
200
|
+
#
|
201
|
+
# ```ruby
|
202
|
+
# document = relation.create(name: 'John Doe', age: 30)
|
203
|
+
# ```
|
204
|
+
#
|
77
205
|
def create(*args, &block)
|
78
206
|
scoping { @klass.create!(*args, &block) }
|
79
207
|
end
|
80
208
|
|
81
209
|
# Executes a block of code within the scope of the relation.
|
82
210
|
#
|
83
|
-
#
|
211
|
+
# This instance method is used to execute a block of code within the scope of the relation. It temporarily sets the current scope of the class to this relation, executes the block, and then resets the current scope to its previous value.
|
212
|
+
#
|
213
|
+
# ### Parameters
|
214
|
+
#
|
215
|
+
# - `&block:` The block of code to execute within the scope of the relation.
|
216
|
+
#
|
217
|
+
# ### Returns
|
218
|
+
# Returns the result of the block execution.
|
219
|
+
#
|
220
|
+
#
|
84
221
|
def scoping
|
85
222
|
previous, klass.current_scope = klass.current_scope, self
|
86
223
|
yield
|
@@ -90,22 +227,41 @@ module Stretchy
|
|
90
227
|
|
91
228
|
# Loads the results of the relation.
|
92
229
|
#
|
93
|
-
#
|
230
|
+
# This instance method is used to load the results of the relation. It calls the `exec_queries` method to execute the queries unless the results have already been loaded.
|
231
|
+
#
|
232
|
+
# ### Returns
|
233
|
+
# Returns the relation object itself.
|
234
|
+
#
|
235
|
+
# ---
|
236
|
+
#
|
237
|
+
# ### Examples
|
238
|
+
#
|
239
|
+
# #### Load the results of a relation
|
240
|
+
#
|
241
|
+
# ```ruby
|
242
|
+
# relation = Model.where(flight: 'goat')
|
243
|
+
# relation.load
|
244
|
+
# ```
|
245
|
+
#
|
94
246
|
def load
|
95
247
|
exec_queries unless loaded?
|
96
248
|
self
|
97
249
|
end
|
250
|
+
|
251
|
+
# _alias for `load`_
|
98
252
|
alias :fetch :load
|
99
253
|
|
100
|
-
|
101
|
-
#
|
102
|
-
# @param opts [Hash] The options for the delete operation.
|
103
|
-
def delete(opts=nil)
|
254
|
+
def delete(opts=nil) #:nodoc:
|
104
255
|
end
|
105
256
|
|
106
257
|
# Executes the Elasticsearch query for the relation.
|
107
258
|
#
|
108
|
-
#
|
259
|
+
# This instance method is used to execute the Elasticsearch query for the relation. It calls the `execute` method on the `query_builder` of the relation and stores the results in the `@records` instance variable.
|
260
|
+
#
|
261
|
+
# ### Returns
|
262
|
+
# Returns an Array representing the results of the query.
|
263
|
+
#
|
264
|
+
#
|
109
265
|
def exec_queries
|
110
266
|
# Run safety callback
|
111
267
|
klass._circuit_breaker_callbacks.each do |cb|
|
@@ -128,14 +284,44 @@ module Stretchy
|
|
128
284
|
|
129
285
|
# Returns the values of the relation as a hash.
|
130
286
|
#
|
131
|
-
#
|
287
|
+
# This instance method is used to get the values of the relation as a hash. It converts the `@values` instance variable to a hash and returns it.
|
288
|
+
#
|
289
|
+
# ### Returns
|
290
|
+
# Returns a Hash representing the values of the relation.
|
291
|
+
#
|
292
|
+
# ---
|
293
|
+
#
|
294
|
+
# ### Examples
|
295
|
+
#
|
296
|
+
# #### Get the values of a relation
|
297
|
+
#
|
298
|
+
# ```ruby
|
299
|
+
# relation = Model.where(flight: 'goat')
|
300
|
+
# values = relation.values
|
301
|
+
# ```
|
302
|
+
#
|
132
303
|
def values
|
133
304
|
Hash[@values]
|
134
305
|
end
|
135
306
|
|
136
|
-
#
|
307
|
+
# Returns a string representation of the relation.
|
308
|
+
#
|
309
|
+
# This instance method is used to get a string representation of the relation. It includes information about the total number of results, the maximum number of results, and any aggregations present in the response.
|
137
310
|
#
|
138
|
-
#
|
311
|
+
# ### Returns
|
312
|
+
# Returns a String representing the relation.
|
313
|
+
#
|
314
|
+
# ---
|
315
|
+
#
|
316
|
+
# ### Examples
|
317
|
+
#
|
318
|
+
# #### Get a string representation of a relation
|
319
|
+
#
|
320
|
+
# ```ruby
|
321
|
+
# Model.where(flight: 'goat').terms(:flights, field: :flight).inspect
|
322
|
+
# #=> #<Stretchy::Relation total: 0, max: 0, aggregations: ["flights"]>
|
323
|
+
# ```
|
324
|
+
#
|
139
325
|
def inspect
|
140
326
|
begin
|
141
327
|
entries = to_a.results.take([size_value.to_i + 1, 11].compact.min).map!(&:inspect)
|
@@ -146,7 +332,7 @@ module Stretchy
|
|
146
332
|
message.unshift entries.join(', ') unless entries.size.zero?
|
147
333
|
"#<#{self.class.name} #{message.join(', ')}>"
|
148
334
|
rescue StandardError => e
|
149
|
-
e
|
335
|
+
Stretchy.logger.error e.message
|
150
336
|
raise e
|
151
337
|
end
|
152
338
|
end
|
@@ -155,7 +341,22 @@ module Stretchy
|
|
155
341
|
|
156
342
|
# Returns the query builder for the relation.
|
157
343
|
#
|
158
|
-
#
|
344
|
+
# This private instance method is used to get the query builder for the relation. It creates a new instance of `Relations::QueryBuilder` with the values of the relation and the attribute types of the class.
|
345
|
+
#
|
346
|
+
# ### Returns
|
347
|
+
# Returns a `Relations::QueryBuilder` representing the query builder for the relation.
|
348
|
+
#
|
349
|
+
# ---
|
350
|
+
#
|
351
|
+
# ### Examples
|
352
|
+
#
|
353
|
+
# #### Get the query builder for a relation
|
354
|
+
#
|
355
|
+
# ```ruby
|
356
|
+
# relation = Model.where(flight: 'goat')
|
357
|
+
# query_builder = relation.send(:query_builder)
|
358
|
+
# ```
|
359
|
+
#
|
159
360
|
def query_builder
|
160
361
|
Relations::QueryBuilder.new(values, klass.attribute_types)
|
161
362
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Relations
|
3
|
+
module AggregationMethods
|
4
|
+
module Aggregation
|
5
|
+
# Adds an aggregation to the query.
|
6
|
+
#
|
7
|
+
# This method is used to add an aggregation to the query. It accepts a name for the aggregation, a hash of options for the aggregation, and an optional block to further configure the aggregation.
|
8
|
+
#
|
9
|
+
# ### Parameters
|
10
|
+
#
|
11
|
+
# - `name:` The Symbol or String representing the name of the aggregation.
|
12
|
+
# - `options:` The Hash representing the options for the aggregation (default: {}).
|
13
|
+
# - `:field:` The Symbol or String representing the field to aggregate on.
|
14
|
+
# - `:ranges:` The Array of Hashes representing the ranges for a range aggregation.
|
15
|
+
# - `block:` The Proc representing an optional block to further configure the aggregation.
|
16
|
+
#
|
17
|
+
# ### Returns
|
18
|
+
# Returns a new Stretchy::Relation with the specified aggregation.
|
19
|
+
#
|
20
|
+
# ---
|
21
|
+
#
|
22
|
+
# ### Examples
|
23
|
+
#
|
24
|
+
# #### Single aggregation
|
25
|
+
#
|
26
|
+
# ```ruby
|
27
|
+
# Model.aggregation(:avg_price, field: :price)
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# #### Aggregation with ranges
|
31
|
+
#
|
32
|
+
# ```ruby
|
33
|
+
# Model.aggregation(:price_ranges) do
|
34
|
+
# range field: :price, ranges: [{to: 100}, {from: 100, to: 200}, {from: 200}]
|
35
|
+
# end
|
36
|
+
# ```
|
37
|
+
#
|
38
|
+
# Aggregation results are available in the `aggregations` method of the results under the name provided in the aggregation.
|
39
|
+
#
|
40
|
+
# ```ruby
|
41
|
+
# results = Model.where(color: :blue).aggregation(:avg_price, field: :price)
|
42
|
+
# results.aggregations.avg_price
|
43
|
+
# ```
|
44
|
+
#
|
45
|
+
def aggregation(name, options = {}, &block)
|
46
|
+
spawn.aggregation!(name, options, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def aggregation!(name, options = {}, &block) # :nodoc:
|
50
|
+
self.aggregation_values += [{name: name, args: options}]
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
AggregationMethods.register!(:aggregation)
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Relations
|
3
|
+
module AggregationMethods
|
4
|
+
module Avg
|
5
|
+
# Perform an average aggregation.
|
6
|
+
#
|
7
|
+
# This method is used to calculate the average of a numeric field. It accepts a name for the aggregation and a hash of options for the aggregation.
|
8
|
+
#
|
9
|
+
# ### Parameters
|
10
|
+
#
|
11
|
+
# - `name:` The Symbol or String representing the name of the aggregation.
|
12
|
+
# - `options:` The Hash representing the options for the aggregation (default: {}).
|
13
|
+
# - `:field:` The Symbol or String representing the field to calculate the average on.
|
14
|
+
# - `aggs:` The Array of Hashes representing nested aggregations (optional).
|
15
|
+
#
|
16
|
+
# ### Returns
|
17
|
+
# Returns a new Stretchy::Relation with the specified average aggregation.
|
18
|
+
#
|
19
|
+
# ---
|
20
|
+
#
|
21
|
+
# ### Examples
|
22
|
+
#
|
23
|
+
# #### Average aggregation
|
24
|
+
#
|
25
|
+
# ```ruby
|
26
|
+
# Model.avg(:average_price, field: :price)
|
27
|
+
# ```
|
28
|
+
#
|
29
|
+
# Aggregation results are available in the `aggregations` method of the results under the name provided in the aggregation.
|
30
|
+
#
|
31
|
+
# ```ruby
|
32
|
+
# results = Model.where(color: :blue).avg(:average_price, field: :price)
|
33
|
+
# results.aggregations.average_price
|
34
|
+
# ```
|
35
|
+
#
|
36
|
+
def avg(name, options = {}, *aggs)
|
37
|
+
options = {avg: options}.merge(*aggs)
|
38
|
+
aggregation(name, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
AggregationMethods.register!(:avg)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Relations
|
3
|
+
module AggregationMethods
|
4
|
+
module BucketScript
|
5
|
+
# Perform a bucket_script aggregation.
|
6
|
+
#
|
7
|
+
# This method is used to perform computations on the results of other aggregations. It accepts a name for the aggregation, a hash of options for the aggregation, and an optional array of nested aggregations.
|
8
|
+
#
|
9
|
+
# ### Parameters
|
10
|
+
#
|
11
|
+
# - `name:` The Symbol or String representing the name of the aggregation.
|
12
|
+
# - `options:` The Hash representing the options for the aggregation (default: {}).
|
13
|
+
# - `:buckets_path:` The Hash representing the paths to the buckets on which to perform computations.
|
14
|
+
# - `:script:` The String representing the script to execute.
|
15
|
+
# - `aggs:` The Array of Hashes representing nested aggregations (optional).
|
16
|
+
#
|
17
|
+
# ### Returns
|
18
|
+
# Returns a new Stretchy::Relation with the specified bucket_script aggregation.
|
19
|
+
#
|
20
|
+
# ---
|
21
|
+
#
|
22
|
+
# ### Examples
|
23
|
+
#
|
24
|
+
# #### Bucket_script aggregation
|
25
|
+
#
|
26
|
+
# ```ruby
|
27
|
+
# Model.bucket_script(:total_sales, script: "params.tShirtsSold * params.price", buckets_path: {tShirtsSold: "tShirtsSold", price: "price"})
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# Aggregation results are available in the `aggregations` method of the results under the name provided in the aggregation.
|
31
|
+
#
|
32
|
+
# ```ruby
|
33
|
+
# results = Model.where(color: :blue).bucket_script(:total_sales, script: "params.tShirtsSold * params.price", buckets_path: {tShirtsSold: "tShirtsSold", price: "price"})
|
34
|
+
# results.aggregations.total_sales
|
35
|
+
# ```
|
36
|
+
#
|
37
|
+
def bucket_script(name, options = {}, *aggs)
|
38
|
+
options = {bucket_script: options}.merge(*aggs)
|
39
|
+
aggregation(name, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
AggregationMethods.register!(:bucket_script)
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Relations
|
3
|
+
module AggregationMethods
|
4
|
+
module BucketSelector
|
5
|
+
# Perform a bucket_selector aggregation.
|
6
|
+
#
|
7
|
+
# This method is used to filter buckets of a parent multi-bucket aggregation. It accepts a name for the aggregation, a hash of options for the aggregation, and an optional array of nested aggregations.
|
8
|
+
#
|
9
|
+
# ### Parameters
|
10
|
+
#
|
11
|
+
# - `name:` The Symbol or String representing the name of the aggregation.
|
12
|
+
# - `options:` The Hash representing the options for the aggregation (default: {}).
|
13
|
+
# - `:script:` The String representing the script to determine whether the current bucket will be retained.
|
14
|
+
# - `:buckets_path:` The Hash representing the paths to the buckets on which to perform computations.
|
15
|
+
# - `aggs:` The Array of Hashes representing nested aggregations (optional).
|
16
|
+
#
|
17
|
+
# ### Returns
|
18
|
+
# Returns a new Stretchy::Relation with the specified bucket_selector aggregation.
|
19
|
+
#
|
20
|
+
# ---
|
21
|
+
#
|
22
|
+
# ### Examples
|
23
|
+
#
|
24
|
+
# #### Bucket_selector aggregation
|
25
|
+
#
|
26
|
+
# ```ruby
|
27
|
+
# Model.bucket_selector(:sales_bucket_filter, script: "params.totalSales > 200", buckets_path: {totalSales: "totalSales"})
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# Aggregation results are available in the `aggregations` method of the results under the name provided in the aggregation.
|
31
|
+
#
|
32
|
+
# ```ruby
|
33
|
+
# results = Model.where(color: :blue).bucket_selector(:sales_bucket_filter, script: "params.totalSales > 200", buckets_path: {totalSales: "totalSales"})
|
34
|
+
# results.aggregations.sales_bucket_filter
|
35
|
+
# ```
|
36
|
+
#
|
37
|
+
def bucket_selector(name, options = {}, *aggs)
|
38
|
+
options = {bucket_selector: options}.merge(*aggs)
|
39
|
+
aggregation(name, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
AggregationMethods.register!(:bucket_selector)
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Relations
|
3
|
+
module AggregationMethods
|
4
|
+
module BucketSort
|
5
|
+
# Perform a bucket_sort aggregation.
|
6
|
+
#
|
7
|
+
# This method is used to sort the buckets of a parent multi-bucket aggregation. It accepts a name for the aggregation, a hash of options for the aggregation, and an optional array of nested aggregations.
|
8
|
+
#
|
9
|
+
# ### Parameters
|
10
|
+
#
|
11
|
+
# - `name:` The Symbol or String representing the name of the aggregation.
|
12
|
+
# - `options:` The Hash representing the options for the aggregation (default: {}).
|
13
|
+
# - `:field:` The Symbol or String representing the field to sort on.
|
14
|
+
# - `aggs:` The Array of Hashes representing nested aggregations (optional).
|
15
|
+
#
|
16
|
+
# ### Returns
|
17
|
+
# Returns a new Stretchy::Relation with the specified bucket_sort aggregation.
|
18
|
+
#
|
19
|
+
# ---
|
20
|
+
#
|
21
|
+
# ### Examples
|
22
|
+
#
|
23
|
+
# #### Bucket_sort aggregation
|
24
|
+
#
|
25
|
+
# ```ruby
|
26
|
+
# Model.bucket_sort(:my_agg, {field: 'my_field'})
|
27
|
+
# Model.bucket_sort(:my_agg, {field: 'my_field'}, aggs: {...})
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# Aggregation results are available in the `aggregations` method of the results under the name provided in the aggregation.
|
31
|
+
#
|
32
|
+
# ```ruby
|
33
|
+
# results = Model.where(color: :blue).bucket_sort(:my_agg, {field: 'my_field'})
|
34
|
+
# results.aggregations.my_agg
|
35
|
+
# ```
|
36
|
+
#
|
37
|
+
def bucket_sort(name, options = {}, *aggs)
|
38
|
+
options = {bucket_sort: options}.merge(*aggs)
|
39
|
+
aggregation(name, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
AggregationMethods.register!(:bucket_sort)
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Stretchy
|
2
|
+
module Relations
|
3
|
+
module AggregationMethods
|
4
|
+
module Cardinality
|
5
|
+
# Perform a cardinality aggregation.
|
6
|
+
#
|
7
|
+
# This method is used to calculate the cardinality (unique count) of a field. It accepts a name for the aggregation, a hash of options for the aggregation, and an optional array of nested aggregations.
|
8
|
+
#
|
9
|
+
# ### Parameters
|
10
|
+
#
|
11
|
+
# - `name:` The Symbol or String representing the name of the aggregation.
|
12
|
+
# - `options:` The Hash representing the options for the aggregation (default: {}).
|
13
|
+
# - `:field:` The Symbol or String representing the field to calculate the cardinality on.
|
14
|
+
# - `aggs:` The Array of Hashes representing nested aggregations (optional).
|
15
|
+
#
|
16
|
+
# ### Returns
|
17
|
+
# Returns a new Stretchy::Relation with the specified cardinality aggregation.
|
18
|
+
#
|
19
|
+
# ---
|
20
|
+
#
|
21
|
+
# ### Examples
|
22
|
+
#
|
23
|
+
# #### Cardinality aggregation
|
24
|
+
#
|
25
|
+
# ```ruby
|
26
|
+
# Model.cardinality(:unique_names, {field: 'names'})
|
27
|
+
# Model.cardinality(:unique_names, {field: 'names'}, aggs: {...})
|
28
|
+
# ```
|
29
|
+
#
|
30
|
+
# Aggregation results are available in the `aggregations` method of the results under the name provided in the aggregation.
|
31
|
+
#
|
32
|
+
# ```ruby
|
33
|
+
# results = Model.where(color: :blue).cardinality(:unique_names, {field: 'names'})
|
34
|
+
# results.aggregations.unique_names
|
35
|
+
# ```
|
36
|
+
#
|
37
|
+
def cardinality(name, options = {}, *aggs)
|
38
|
+
options = {cardinality: options}.merge(*aggs)
|
39
|
+
aggregation(name, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
AggregationMethods.register!(:cardinality)
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|