stretchy-model 0.6.0 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -1
  3. data/README.md +28 -10
  4. data/Rakefile +56 -0
  5. data/containers/Dockerfile.opensearch +4 -3
  6. data/docker-compose.yml +32 -19
  7. data/docs/.nojekyll +0 -0
  8. data/docs/README.md +147 -0
  9. data/docs/_coverpage.md +14 -0
  10. data/docs/_sidebar.md +14 -0
  11. data/docs/examples/_sidebar.md +15 -0
  12. data/docs/examples/data_analysis.md +216 -0
  13. data/docs/examples/semantic_search_with_llm.md +83 -0
  14. data/docs/examples/simple-ingest-pipeline.md +326 -0
  15. data/docs/guides/_sidebar.md +14 -0
  16. data/docs/guides/aggregations.md +142 -0
  17. data/docs/guides/machine-learning.md +154 -0
  18. data/docs/guides/models.md +372 -0
  19. data/docs/guides/pipelines.md +151 -0
  20. data/docs/guides/querying.md +361 -0
  21. data/docs/guides/quick-start.md +72 -0
  22. data/docs/guides/scopes.md +125 -0
  23. data/docs/index.html +113 -0
  24. data/docs/stretchy.cover.png +0 -0
  25. data/docs/stretchy.logo.png +0 -0
  26. data/docs/styles.css +90 -0
  27. data/lib/elasticsearch/api/actions/machine_learning/models/delete_model.rb +33 -0
  28. data/lib/elasticsearch/api/actions/machine_learning/models/deploy.rb +31 -0
  29. data/lib/elasticsearch/api/actions/machine_learning/models/get_model.rb +43 -0
  30. data/lib/elasticsearch/api/actions/machine_learning/models/get_status.rb +31 -0
  31. data/lib/elasticsearch/api/actions/machine_learning/models/params_registry.rb +45 -0
  32. data/lib/elasticsearch/api/actions/machine_learning/models/register.rb +45 -0
  33. data/lib/elasticsearch/api/actions/machine_learning/models/undeploy.rb +32 -0
  34. data/lib/elasticsearch/api/actions/machine_learning/models/update_model.rb +39 -0
  35. data/lib/elasticsearch/api/namespace/machine_learning/model.rb +27 -0
  36. data/lib/opensearch/api/actions/machine_learning/models/delete_model.rb +33 -0
  37. data/lib/opensearch/api/actions/machine_learning/models/deploy.rb +31 -0
  38. data/lib/opensearch/api/actions/machine_learning/models/get_model.rb +44 -0
  39. data/lib/opensearch/api/actions/machine_learning/models/get_status.rb +31 -0
  40. data/lib/opensearch/api/actions/machine_learning/models/params_registry.rb +45 -0
  41. data/lib/opensearch/api/actions/machine_learning/models/register.rb +45 -0
  42. data/lib/opensearch/api/actions/machine_learning/models/undeploy.rb +31 -0
  43. data/lib/opensearch/api/actions/machine_learning/models/update_model.rb +39 -0
  44. data/lib/opensearch/api/namespace/machine_learning/model.rb +27 -0
  45. data/lib/stretchy/attributes/transformers/keyword_transformer.rb +41 -35
  46. data/lib/stretchy/attributes/type/array.rb +24 -1
  47. data/lib/stretchy/attributes/type/base.rb +6 -2
  48. data/lib/stretchy/attributes/type/binary.rb +24 -17
  49. data/lib/stretchy/attributes/type/boolean.rb +29 -22
  50. data/lib/stretchy/attributes/type/completion.rb +18 -10
  51. data/lib/stretchy/attributes/type/constant_keyword.rb +35 -26
  52. data/lib/stretchy/attributes/type/date_time.rb +81 -20
  53. data/lib/stretchy/attributes/type/dense_vector.rb +46 -49
  54. data/lib/stretchy/attributes/type/flattened.rb +28 -19
  55. data/lib/stretchy/attributes/type/geo_point.rb +21 -12
  56. data/lib/stretchy/attributes/type/geo_shape.rb +21 -12
  57. data/lib/stretchy/attributes/type/hash.rb +24 -10
  58. data/lib/stretchy/attributes/type/histogram.rb +25 -0
  59. data/lib/stretchy/attributes/type/ip.rb +26 -17
  60. data/lib/stretchy/attributes/type/join.rb +16 -7
  61. data/lib/stretchy/attributes/type/keyword.rb +21 -26
  62. data/lib/stretchy/attributes/type/knn_vector.rb +47 -0
  63. data/lib/stretchy/attributes/type/match_only_text.rb +22 -1
  64. data/lib/stretchy/attributes/type/nested.rb +16 -11
  65. data/lib/stretchy/attributes/type/numeric/base.rb +30 -22
  66. data/lib/stretchy/attributes/type/numeric/byte.rb +20 -0
  67. data/lib/stretchy/attributes/type/numeric/double.rb +20 -0
  68. data/lib/stretchy/attributes/type/numeric/float.rb +20 -0
  69. data/lib/stretchy/attributes/type/numeric/half_float.rb +20 -0
  70. data/lib/stretchy/attributes/type/numeric/integer.rb +21 -1
  71. data/lib/stretchy/attributes/type/numeric/long.rb +20 -0
  72. data/lib/stretchy/attributes/type/numeric/scaled_float.rb +16 -7
  73. data/lib/stretchy/attributes/type/numeric/short.rb +20 -0
  74. data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +21 -1
  75. data/lib/stretchy/attributes/type/percolator.rb +16 -4
  76. data/lib/stretchy/attributes/type/point.rb +19 -9
  77. data/lib/stretchy/attributes/type/range/base.rb +24 -1
  78. data/lib/stretchy/attributes/type/range/date_range.rb +21 -5
  79. data/lib/stretchy/attributes/type/range/double_range.rb +20 -4
  80. data/lib/stretchy/attributes/type/range/float_range.rb +21 -5
  81. data/lib/stretchy/attributes/type/range/integer_range.rb +20 -4
  82. data/lib/stretchy/attributes/type/range/ip_range.rb +20 -4
  83. data/lib/stretchy/attributes/type/range/long_range.rb +20 -4
  84. data/lib/stretchy/attributes/type/rank_feature.rb +16 -6
  85. data/lib/stretchy/attributes/type/rank_features.rb +27 -10
  86. data/lib/stretchy/attributes/type/search_as_you_type.rb +28 -18
  87. data/lib/stretchy/attributes/type/shape.rb +19 -9
  88. data/lib/stretchy/attributes/type/sparse_vector.rb +25 -21
  89. data/lib/stretchy/attributes/type/string.rb +42 -1
  90. data/lib/stretchy/attributes/type/text.rb +53 -28
  91. data/lib/stretchy/attributes/type/token_count.rb +21 -11
  92. data/lib/stretchy/attributes/type/version.rb +16 -6
  93. data/lib/stretchy/attributes/type/wildcard.rb +36 -25
  94. data/lib/stretchy/attributes.rb +30 -0
  95. data/lib/stretchy/delegation/gateway_delegation.rb +86 -2
  96. data/lib/stretchy/index_setting.rb +94 -0
  97. data/lib/stretchy/indexing/bulk.rb +75 -3
  98. data/lib/stretchy/machine_learning/model.rb +192 -0
  99. data/lib/stretchy/model/callbacks.rb +1 -0
  100. data/lib/stretchy/model/common.rb +157 -0
  101. data/lib/stretchy/model/persistence.rb +144 -0
  102. data/lib/stretchy/model/refreshable.rb +26 -0
  103. data/lib/stretchy/open_search_compatibility.rb +4 -0
  104. data/lib/stretchy/pipeline.rb +124 -0
  105. data/lib/stretchy/pipelines/processor.rb +57 -0
  106. data/lib/stretchy/querying.rb +7 -7
  107. data/lib/stretchy/rails/instrumentation/publishers.rb +31 -0
  108. data/lib/{rails → stretchy/rails}/instrumentation/railtie.rb +11 -6
  109. data/lib/stretchy/record.rb +5 -4
  110. data/lib/stretchy/relation.rb +230 -28
  111. data/lib/stretchy/relations/aggregation_methods/aggregation.rb +59 -0
  112. data/lib/stretchy/relations/aggregation_methods/avg.rb +45 -0
  113. data/lib/stretchy/relations/aggregation_methods/bucket_script.rb +47 -0
  114. data/lib/stretchy/relations/aggregation_methods/bucket_selector.rb +47 -0
  115. data/lib/stretchy/relations/aggregation_methods/bucket_sort.rb +47 -0
  116. data/lib/stretchy/relations/aggregation_methods/cardinality.rb +47 -0
  117. data/lib/stretchy/relations/aggregation_methods/children.rb +47 -0
  118. data/lib/stretchy/relations/aggregation_methods/composite.rb +41 -0
  119. data/lib/stretchy/relations/aggregation_methods/date_histogram.rb +53 -0
  120. data/lib/stretchy/relations/aggregation_methods/date_range.rb +53 -0
  121. data/lib/stretchy/relations/aggregation_methods/extended_stats.rb +48 -0
  122. data/lib/stretchy/relations/aggregation_methods/filter.rb +47 -0
  123. data/lib/stretchy/relations/aggregation_methods/filters.rb +47 -0
  124. data/lib/stretchy/relations/aggregation_methods/geo_bounds.rb +40 -0
  125. data/lib/stretchy/relations/aggregation_methods/geo_centroid.rb +40 -0
  126. data/lib/stretchy/relations/aggregation_methods/global.rb +39 -0
  127. data/lib/stretchy/relations/aggregation_methods/histogram.rb +43 -0
  128. data/lib/stretchy/relations/aggregation_methods/ip_range.rb +41 -0
  129. data/lib/stretchy/relations/aggregation_methods/max.rb +40 -0
  130. data/lib/stretchy/relations/aggregation_methods/min.rb +41 -0
  131. data/lib/stretchy/relations/aggregation_methods/missing.rb +40 -0
  132. data/lib/stretchy/relations/aggregation_methods/nested.rb +40 -0
  133. data/lib/stretchy/relations/aggregation_methods/percentile_ranks.rb +45 -0
  134. data/lib/stretchy/relations/aggregation_methods/percentiles.rb +45 -0
  135. data/lib/stretchy/relations/aggregation_methods/range.rb +42 -0
  136. data/lib/stretchy/relations/aggregation_methods/reverse_nested.rb +40 -0
  137. data/lib/stretchy/relations/aggregation_methods/sampler.rb +40 -0
  138. data/lib/stretchy/relations/aggregation_methods/scripted_metric.rb +43 -0
  139. data/lib/stretchy/relations/aggregation_methods/significant_terms.rb +45 -0
  140. data/lib/stretchy/relations/aggregation_methods/stats.rb +42 -0
  141. data/lib/stretchy/relations/aggregation_methods/sum.rb +42 -0
  142. data/lib/stretchy/relations/aggregation_methods/terms.rb +46 -0
  143. data/lib/stretchy/relations/aggregation_methods/top_hits.rb +42 -0
  144. data/lib/stretchy/relations/aggregation_methods/top_metrics.rb +44 -0
  145. data/lib/stretchy/relations/aggregation_methods/value_count.rb +41 -0
  146. data/lib/stretchy/relations/aggregation_methods/weighted_avg.rb +42 -0
  147. data/lib/stretchy/relations/aggregation_methods.rb +20 -749
  148. data/lib/stretchy/relations/finder_methods.rb +2 -18
  149. data/lib/stretchy/relations/null_relation.rb +55 -0
  150. data/lib/stretchy/relations/query_builder.rb +139 -23
  151. data/lib/stretchy/relations/query_methods/bind.rb +19 -0
  152. data/lib/stretchy/relations/query_methods/extending.rb +29 -0
  153. data/lib/stretchy/relations/query_methods/fields.rb +70 -0
  154. data/lib/stretchy/relations/query_methods/filter_query.rb +53 -0
  155. data/lib/stretchy/relations/query_methods/has_field.rb +40 -0
  156. data/lib/stretchy/relations/query_methods/highlight.rb +75 -0
  157. data/lib/stretchy/relations/query_methods/hybrid.rb +60 -0
  158. data/lib/stretchy/relations/query_methods/ids.rb +40 -0
  159. data/lib/stretchy/relations/query_methods/match.rb +52 -0
  160. data/lib/stretchy/relations/query_methods/must_not.rb +54 -0
  161. data/lib/stretchy/relations/query_methods/neural.rb +58 -0
  162. data/lib/stretchy/relations/query_methods/neural_sparse.rb +43 -0
  163. data/lib/stretchy/relations/query_methods/none.rb +21 -0
  164. data/lib/stretchy/relations/query_methods/or_filter.rb +21 -0
  165. data/lib/stretchy/relations/query_methods/order.rb +63 -0
  166. data/lib/stretchy/relations/query_methods/query_string.rb +44 -0
  167. data/lib/stretchy/relations/query_methods/regexp.rb +61 -0
  168. data/lib/stretchy/relations/query_methods/should.rb +51 -0
  169. data/lib/stretchy/relations/query_methods/size.rb +44 -0
  170. data/lib/stretchy/relations/query_methods/skip_callbacks.rb +47 -0
  171. data/lib/stretchy/relations/query_methods/source.rb +59 -0
  172. data/lib/stretchy/relations/query_methods/where.rb +113 -0
  173. data/lib/stretchy/relations/query_methods.rb +51 -540
  174. data/lib/stretchy/relations/scoping/default.rb +136 -0
  175. data/lib/stretchy/relations/scoping/named.rb +70 -0
  176. data/lib/stretchy/relations/scoping/scope_registry.rb +36 -0
  177. data/lib/stretchy/relations/scoping.rb +30 -0
  178. data/lib/stretchy/relations/search_option_methods.rb +2 -0
  179. data/lib/stretchy/shared_scopes.rb +6 -1
  180. data/lib/stretchy/version.rb +1 -1
  181. data/lib/stretchy.rb +23 -11
  182. metadata +147 -18
  183. data/lib/rails/instrumentation/publishers.rb +0 -29
  184. data/lib/stretchy/common.rb +0 -33
  185. data/lib/stretchy/null_relation.rb +0 -53
  186. data/lib/stretchy/persistence.rb +0 -43
  187. data/lib/stretchy/refreshable.rb +0 -15
  188. data/lib/stretchy/scoping/default.rb +0 -134
  189. data/lib/stretchy/scoping/named.rb +0 -68
  190. data/lib/stretchy/scoping/scope_registry.rb +0 -34
  191. data/lib/stretchy/scoping.rb +0 -28
@@ -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
- # Constructor.
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
- # Builds a new Elasticsearch document.
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
- # @return [Array] The results of the relation.
47
- def to_a
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
- # @param options [Hash] The options to pass to the `as_json` method.
60
- # @return [Hash] The results of the relation as a JSON object.
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
- # @return [Hash] The Elasticsearch query for the relation.
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
- # @param args [Array] The arguments to pass to the document constructor.
75
- # @param block [Proc] The block to pass to the document constructor.
76
- # @return [Object] The new document.
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
- # @yield The block of code to execute.
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
- # @return [Relation] The relation object.
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
- # Deletes Elasticsearch documents.
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
- # @return [Array] The results of the query.
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
- # @return [Hash] The values of the relation.
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
- # Returns a string representation of the relation.
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
- # @return [String] The string representation of the relation.
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,8 @@ 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
336
+ raise e
150
337
  end
151
338
  end
152
339
 
@@ -154,7 +341,22 @@ module Stretchy
154
341
 
155
342
  # Returns the query builder for the relation.
156
343
  #
157
- # @return [QueryBuilder] The query builder for the relation.
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
+ #
158
360
  def query_builder
159
361
  Relations::QueryBuilder.new(values, klass.attribute_types)
160
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