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.
Files changed (165) 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/docs/.nojekyll +0 -0
  6. data/docs/README.md +147 -0
  7. data/docs/_coverpage.md +14 -0
  8. data/docs/_sidebar.md +14 -0
  9. data/docs/examples/_sidebar.md +15 -0
  10. data/docs/examples/data_analysis.md +216 -0
  11. data/docs/examples/semantic_search_with_llm.md +83 -0
  12. data/docs/examples/simple-ingest-pipeline.md +326 -0
  13. data/docs/guides/_sidebar.md +14 -0
  14. data/docs/guides/aggregations.md +142 -0
  15. data/docs/guides/machine-learning.md +154 -0
  16. data/docs/guides/models.md +372 -0
  17. data/docs/guides/pipelines.md +151 -0
  18. data/docs/guides/querying.md +361 -0
  19. data/docs/guides/quick-start.md +72 -0
  20. data/docs/guides/scopes.md +125 -0
  21. data/docs/index.html +113 -0
  22. data/docs/stretchy.cover.png +0 -0
  23. data/docs/stretchy.logo.png +0 -0
  24. data/docs/styles.css +90 -0
  25. data/lib/stretchy/attributes/transformers/keyword_transformer.rb +41 -35
  26. data/lib/stretchy/attributes/type/array.rb +24 -1
  27. data/lib/stretchy/attributes/type/base.rb +6 -2
  28. data/lib/stretchy/attributes/type/binary.rb +24 -17
  29. data/lib/stretchy/attributes/type/boolean.rb +29 -22
  30. data/lib/stretchy/attributes/type/completion.rb +18 -10
  31. data/lib/stretchy/attributes/type/constant_keyword.rb +35 -26
  32. data/lib/stretchy/attributes/type/date_time.rb +28 -17
  33. data/lib/stretchy/attributes/type/dense_vector.rb +46 -49
  34. data/lib/stretchy/attributes/type/flattened.rb +28 -19
  35. data/lib/stretchy/attributes/type/geo_point.rb +21 -12
  36. data/lib/stretchy/attributes/type/geo_shape.rb +21 -12
  37. data/lib/stretchy/attributes/type/hash.rb +24 -10
  38. data/lib/stretchy/attributes/type/histogram.rb +25 -0
  39. data/lib/stretchy/attributes/type/ip.rb +26 -17
  40. data/lib/stretchy/attributes/type/join.rb +16 -7
  41. data/lib/stretchy/attributes/type/keyword.rb +21 -26
  42. data/lib/stretchy/attributes/type/knn_vector.rb +47 -0
  43. data/lib/stretchy/attributes/type/match_only_text.rb +22 -1
  44. data/lib/stretchy/attributes/type/nested.rb +16 -11
  45. data/lib/stretchy/attributes/type/numeric/base.rb +30 -22
  46. data/lib/stretchy/attributes/type/numeric/byte.rb +20 -0
  47. data/lib/stretchy/attributes/type/numeric/double.rb +20 -0
  48. data/lib/stretchy/attributes/type/numeric/float.rb +20 -0
  49. data/lib/stretchy/attributes/type/numeric/half_float.rb +20 -0
  50. data/lib/stretchy/attributes/type/numeric/integer.rb +21 -1
  51. data/lib/stretchy/attributes/type/numeric/long.rb +20 -0
  52. data/lib/stretchy/attributes/type/numeric/scaled_float.rb +16 -7
  53. data/lib/stretchy/attributes/type/numeric/short.rb +20 -0
  54. data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +21 -1
  55. data/lib/stretchy/attributes/type/percolator.rb +16 -4
  56. data/lib/stretchy/attributes/type/point.rb +19 -9
  57. data/lib/stretchy/attributes/type/range/base.rb +24 -1
  58. data/lib/stretchy/attributes/type/range/date_range.rb +21 -5
  59. data/lib/stretchy/attributes/type/range/double_range.rb +20 -4
  60. data/lib/stretchy/attributes/type/range/float_range.rb +21 -5
  61. data/lib/stretchy/attributes/type/range/integer_range.rb +20 -4
  62. data/lib/stretchy/attributes/type/range/ip_range.rb +20 -4
  63. data/lib/stretchy/attributes/type/range/long_range.rb +20 -4
  64. data/lib/stretchy/attributes/type/rank_feature.rb +16 -6
  65. data/lib/stretchy/attributes/type/rank_features.rb +16 -9
  66. data/lib/stretchy/attributes/type/search_as_you_type.rb +28 -18
  67. data/lib/stretchy/attributes/type/shape.rb +19 -9
  68. data/lib/stretchy/attributes/type/sparse_vector.rb +25 -21
  69. data/lib/stretchy/attributes/type/string.rb +42 -1
  70. data/lib/stretchy/attributes/type/text.rb +53 -28
  71. data/lib/stretchy/attributes/type/token_count.rb +21 -11
  72. data/lib/stretchy/attributes/type/version.rb +16 -6
  73. data/lib/stretchy/attributes/type/wildcard.rb +36 -25
  74. data/lib/stretchy/attributes.rb +29 -0
  75. data/lib/stretchy/delegation/gateway_delegation.rb +78 -0
  76. data/lib/stretchy/index_setting.rb +94 -0
  77. data/lib/stretchy/indexing/bulk.rb +75 -3
  78. data/lib/stretchy/model/callbacks.rb +1 -0
  79. data/lib/stretchy/model/common.rb +157 -0
  80. data/lib/stretchy/model/persistence.rb +144 -0
  81. data/lib/stretchy/model/refreshable.rb +26 -0
  82. data/lib/stretchy/pipeline.rb +2 -1
  83. data/lib/stretchy/pipelines/processor.rb +38 -36
  84. data/lib/stretchy/querying.rb +7 -8
  85. data/lib/stretchy/record.rb +5 -4
  86. data/lib/stretchy/relation.rb +229 -28
  87. data/lib/stretchy/relations/aggregation_methods/aggregation.rb +59 -0
  88. data/lib/stretchy/relations/aggregation_methods/avg.rb +45 -0
  89. data/lib/stretchy/relations/aggregation_methods/bucket_script.rb +47 -0
  90. data/lib/stretchy/relations/aggregation_methods/bucket_selector.rb +47 -0
  91. data/lib/stretchy/relations/aggregation_methods/bucket_sort.rb +47 -0
  92. data/lib/stretchy/relations/aggregation_methods/cardinality.rb +47 -0
  93. data/lib/stretchy/relations/aggregation_methods/children.rb +47 -0
  94. data/lib/stretchy/relations/aggregation_methods/composite.rb +41 -0
  95. data/lib/stretchy/relations/aggregation_methods/date_histogram.rb +53 -0
  96. data/lib/stretchy/relations/aggregation_methods/date_range.rb +53 -0
  97. data/lib/stretchy/relations/aggregation_methods/extended_stats.rb +48 -0
  98. data/lib/stretchy/relations/aggregation_methods/filter.rb +47 -0
  99. data/lib/stretchy/relations/aggregation_methods/filters.rb +47 -0
  100. data/lib/stretchy/relations/aggregation_methods/geo_bounds.rb +40 -0
  101. data/lib/stretchy/relations/aggregation_methods/geo_centroid.rb +40 -0
  102. data/lib/stretchy/relations/aggregation_methods/global.rb +39 -0
  103. data/lib/stretchy/relations/aggregation_methods/histogram.rb +43 -0
  104. data/lib/stretchy/relations/aggregation_methods/ip_range.rb +41 -0
  105. data/lib/stretchy/relations/aggregation_methods/max.rb +40 -0
  106. data/lib/stretchy/relations/aggregation_methods/min.rb +41 -0
  107. data/lib/stretchy/relations/aggregation_methods/missing.rb +40 -0
  108. data/lib/stretchy/relations/aggregation_methods/nested.rb +40 -0
  109. data/lib/stretchy/relations/aggregation_methods/percentile_ranks.rb +45 -0
  110. data/lib/stretchy/relations/aggregation_methods/percentiles.rb +45 -0
  111. data/lib/stretchy/relations/aggregation_methods/range.rb +42 -0
  112. data/lib/stretchy/relations/aggregation_methods/reverse_nested.rb +40 -0
  113. data/lib/stretchy/relations/aggregation_methods/sampler.rb +40 -0
  114. data/lib/stretchy/relations/aggregation_methods/scripted_metric.rb +43 -0
  115. data/lib/stretchy/relations/aggregation_methods/significant_terms.rb +45 -0
  116. data/lib/stretchy/relations/aggregation_methods/stats.rb +42 -0
  117. data/lib/stretchy/relations/aggregation_methods/sum.rb +42 -0
  118. data/lib/stretchy/relations/aggregation_methods/terms.rb +46 -0
  119. data/lib/stretchy/relations/aggregation_methods/top_hits.rb +42 -0
  120. data/lib/stretchy/relations/aggregation_methods/top_metrics.rb +44 -0
  121. data/lib/stretchy/relations/aggregation_methods/value_count.rb +41 -0
  122. data/lib/stretchy/relations/aggregation_methods/weighted_avg.rb +42 -0
  123. data/lib/stretchy/relations/aggregation_methods.rb +20 -749
  124. data/lib/stretchy/relations/finder_methods.rb +2 -18
  125. data/lib/stretchy/relations/null_relation.rb +55 -0
  126. data/lib/stretchy/relations/query_builder.rb +82 -36
  127. data/lib/stretchy/relations/query_methods/bind.rb +19 -0
  128. data/lib/stretchy/relations/query_methods/extending.rb +29 -0
  129. data/lib/stretchy/relations/query_methods/fields.rb +70 -0
  130. data/lib/stretchy/relations/query_methods/filter_query.rb +53 -0
  131. data/lib/stretchy/relations/query_methods/has_field.rb +40 -0
  132. data/lib/stretchy/relations/query_methods/highlight.rb +75 -0
  133. data/lib/stretchy/relations/query_methods/hybrid.rb +60 -0
  134. data/lib/stretchy/relations/query_methods/ids.rb +40 -0
  135. data/lib/stretchy/relations/query_methods/match.rb +52 -0
  136. data/lib/stretchy/relations/query_methods/must_not.rb +54 -0
  137. data/lib/stretchy/relations/query_methods/neural.rb +58 -0
  138. data/lib/stretchy/relations/query_methods/neural_sparse.rb +43 -0
  139. data/lib/stretchy/relations/query_methods/none.rb +21 -0
  140. data/lib/stretchy/relations/query_methods/or_filter.rb +21 -0
  141. data/lib/stretchy/relations/query_methods/order.rb +63 -0
  142. data/lib/stretchy/relations/query_methods/query_string.rb +44 -0
  143. data/lib/stretchy/relations/query_methods/regexp.rb +61 -0
  144. data/lib/stretchy/relations/query_methods/should.rb +51 -0
  145. data/lib/stretchy/relations/query_methods/size.rb +44 -0
  146. data/lib/stretchy/relations/query_methods/skip_callbacks.rb +47 -0
  147. data/lib/stretchy/relations/query_methods/source.rb +59 -0
  148. data/lib/stretchy/relations/query_methods/where.rb +113 -0
  149. data/lib/stretchy/relations/query_methods.rb +48 -569
  150. data/lib/stretchy/relations/scoping/default.rb +136 -0
  151. data/lib/stretchy/relations/scoping/named.rb +70 -0
  152. data/lib/stretchy/relations/scoping/scope_registry.rb +36 -0
  153. data/lib/stretchy/relations/scoping.rb +30 -0
  154. data/lib/stretchy/relations/search_option_methods.rb +2 -0
  155. data/lib/stretchy/version.rb +1 -1
  156. data/lib/stretchy.rb +17 -10
  157. metadata +111 -17
  158. data/lib/stretchy/common.rb +0 -38
  159. data/lib/stretchy/null_relation.rb +0 -53
  160. data/lib/stretchy/persistence.rb +0 -43
  161. data/lib/stretchy/refreshable.rb +0 -15
  162. data/lib/stretchy/scoping/default.rb +0 -134
  163. data/lib/stretchy/scoping/named.rb +0 -68
  164. data/lib/stretchy/scoping/scope_registry.rb +0 -34
  165. data/lib/stretchy/scoping.rb +0 -28
@@ -4,15 +4,62 @@ module Stretchy
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  class_methods do
7
-
7
+ # used to bulk index a collection of records.
8
+ #
9
+ # ### Parameters
10
+ #
11
+ # - `records:` (Array) - The collection of records to be indexed.
12
+ #
13
+ # ### Returns
14
+ #
15
+ # - (Hash) - The response from Elasticsearch after performing the bulk operation.
16
+ #
17
+ # ### Behavior
18
+ #
19
+ # The method sends a bulk request to Elasticsearch with the provided records. The records should be an array of hashes, where each hash describes a single create, index, delete, or update operation.
20
+ # It always returns the response from Elasticsearch.
21
+ #
22
+ # ### Example
23
+ #
24
+ # ```ruby
25
+ # records = [
26
+ # { index: { _index: 'my_index', _id: 1, data: { title: 'foo' } } },
27
+ # { index: { _index: 'my_index', _id: 2, data: { title: 'bar' } } }
28
+ # ]
29
+ # MyModel.bulk(records)
30
+ # ```
31
+ # In this example, the `bulk` method is used to index two records into 'my_index'.
8
32
  def bulk(records)
9
33
  self.gateway.client.bulk body: records
10
34
  end
11
35
 
12
- # bulk_in_batches(records, size: 100) do |batch|
36
+ # used to bulk index a collection of records in batches.
37
+ #
38
+ # ### Parameters
39
+ #
40
+ # - `records:` (Array) - The collection of records to be indexed.
41
+ # - `size:` (Integer) - The size of the batches. Defaults to 1000.
42
+ # - `&block:` (optional) A block that is evaluated for each batch. This can be used to perform operations on each batch.
43
+ #
44
+ # ### Returns
45
+ #
46
+ # - (Array) - The results of the bulk operations.
47
+ #
48
+ # ### Behavior
49
+ #
50
+ # - The method splits the records into batches of the specified size. For each batch, if a block is given, it is evaluated with the batch as argument. Then, the batch is indexed using the `bulk` method.
51
+ # - After all batches have been processed, it refreshes the index to make the changes available for search.
52
+ #
53
+ # ### Example
54
+ #
55
+ # ```ruby
56
+ # records = [Report.new(title: "hello"), Report.new(title: "world"), Report.new(title: "goodbye")]
57
+ # MyModel.bulk_in_batches(records, size: 100) do |batch|
13
58
  # # do something with the batch
14
- # batch.each { |record| record.to_bulk(method)}
59
+ # batch.map! { |record| record.to_bulk(:index)}
15
60
  # end
61
+ # ```
62
+ # In this example, the `bulk_in_batches` method is used to index the `records` in batches of 100.
16
63
  def bulk_in_batches(records, size: 1000)
17
64
  bulk_results = records.each_slice(size).map do |batch|
18
65
  yield batch if block_given?
@@ -32,6 +79,31 @@ module Stretchy
32
79
  # end
33
80
  end
34
81
 
82
+ # used to transform a record into a hash suitable for bulk operations.
83
+ #
84
+ # ### Parameters
85
+ #
86
+ # - `method:` (Symbol) - The operation to be performed. Can be `:index`, `:delete`, or `:update`. Defaults to `:index`.
87
+ #
88
+ # ### Returns
89
+ #
90
+ # - (Hash) - The hash representing the record for the bulk operation.
91
+ #
92
+ # ### Behavior
93
+ #
94
+ # The method transforms the record into a hash that describes the operation to be performed on the record. The operation is specified by the `method` parameter.
95
+ # For `:index`, it includes the index name and the record data (excluding the id).
96
+ # For `:delete`, it includes the index name and the record id.
97
+ # For `:update`, it includes the index name, the record id, and the record data (excluding the id).
98
+ #
99
+ # ### Example
100
+ #
101
+ # ```ruby
102
+ # record = MyModel.new(title: "hello")
103
+ # record.to_bulk(:index)
104
+ # # => { index: { _index: "my_models", data: { title: "hello" } } }
105
+ # ```
106
+ # In this example, the `to_bulk` method is used to transform a `MyModel` instance into a hash suitable for an index operation in a bulk request.
35
107
  def to_bulk(method = :index)
36
108
  case method
37
109
  when :index
@@ -8,6 +8,7 @@ module Stretchy
8
8
  included do
9
9
  mattr_accessor :_circuit_breaker_callbacks, default: []
10
10
 
11
+ define_model_callbacks :initialize, only: :after
11
12
  define_model_callbacks :create, :save, :update, :destroy
12
13
  define_model_callbacks :find, :touch, only: :after
13
14
  end
@@ -0,0 +1,157 @@
1
+ module Stretchy
2
+ module Model
3
+ module Common
4
+ extend ActiveSupport::Concern
5
+
6
+ # Get the highlighted results for a specific field.
7
+ #
8
+ # This method is used to get the highlighted results for a specific field from the search results. It accepts a field for which to get the highlighted results.
9
+ #
10
+ # ### Parameters
11
+ #
12
+ # - `field:` The Symbol or String representing the field for which to get the highlighted results.
13
+ #
14
+ # ### Returns
15
+ # Returns an Array of Strings representing the highlighted results for the specified field.
16
+ #
17
+ # ---
18
+ #
19
+ # ### Examples
20
+ #
21
+ # #### Get the highlighted results for a field
22
+ #
23
+ # ```ruby
24
+ # result = Model.query_string("name: Soph*").highlight(name: {pre_tags: "__", post_tags: "__"}).first
25
+ # result.highlights_for(:name)
26
+ # ```
27
+ #
28
+ def highlights_for(attribute)
29
+ highlights[attribute.to_s]
30
+ end
31
+
32
+ class_methods do
33
+
34
+ # Set or get the default sort key.
35
+ #
36
+ # This method is used to set or get the default sort key to be used in sort operations. If a field is provided, it sets the field as the default sort key. If no field is provided, it returns the current default sort key.
37
+ #
38
+ # `created_at` is the default sort key.
39
+ #
40
+ # ### Parameters
41
+ #
42
+ # - `field:` The Symbol or String representing the field to be used as the default sort key (optional).
43
+ #
44
+ # ### Returns
45
+ # Returns the current default sort key if no field is provided. If a field is provided, it sets the field as the default sort key and returns the field.
46
+ #
47
+ # ---
48
+ #
49
+ # ### Examples
50
+ #
51
+ # #### Set the default sort key
52
+ #
53
+ # ```ruby
54
+ # class Model < StretchyModel
55
+ # default_sort_key :updated_at
56
+ # end
57
+ # ```
58
+ #
59
+ # #### Get the default sort key
60
+ #
61
+ # ```ruby
62
+ # Model.default_sort_key
63
+ # ```
64
+ #
65
+ def default_sort_key(field = nil)
66
+ @default_sort_key = field unless field.nil?
67
+ @default_sort_key
68
+ end
69
+
70
+
71
+ # Set or get the default size.
72
+ #
73
+ # This method is used to set or get the default size to be used in operations that return documents. If a size is provided, it sets the size as the default size.
74
+ # If no size is provided, it returns the current default size.
75
+ #
76
+ # 10000 is the default size for StretchyModel
77
+ #
78
+ # ### Parameters
79
+ #
80
+ # - `size:` The Integer representing the size to be used as the default size (optional).
81
+ #
82
+ # ### Returns
83
+ # Returns the current default size if no size is provided. If a size is provided, it sets the size as the default size and returns the size.
84
+ #
85
+ # ---
86
+ #
87
+ # ### Examples
88
+ #
89
+ # #### Set the default size
90
+ #
91
+ # ```ruby
92
+ # class Model < StretchyModel
93
+ # default_size 100
94
+ # end
95
+ # ```
96
+ #
97
+ # #### Get the default size
98
+ #
99
+ # ```ruby
100
+ # Model.default_size
101
+ # ```
102
+ #
103
+ def default_size(size = nil)
104
+ @default_size = size unless size.nil?
105
+ @default_size
106
+ end
107
+
108
+
109
+ # Set or get the default pipeline.
110
+ #
111
+ # This method is used to set or get the default pipeline at the index level to be used in operations that require a pipeline.
112
+ #
113
+ # If a pipeline is provided, it sets the pipeline as the default pipeline.
114
+ # If no pipeline is provided, it returns the current default pipeline.
115
+ #
116
+ # ### Parameters
117
+ #
118
+ # - `pipeline:` The String representing the pipeline to be used as the default pipeline (optional).
119
+ #
120
+ # ### Returns
121
+ # Returns the current default pipeline if no pipeline is provided. If a pipeline is provided, it sets the pipeline as the default pipeline and returns the pipeline.
122
+ #
123
+ # ---
124
+ #
125
+ # ### Examples
126
+ #
127
+ # #### Set the default pipeline
128
+ #
129
+ # ```ruby
130
+ # class Model < StretchyModel
131
+ # default_pipeline 'my_pipeline'
132
+ # end
133
+ # ```
134
+ #
135
+ # #### Get the default pipeline
136
+ #
137
+ # ```ruby
138
+ # Model.default_pipeline
139
+ # ```
140
+ #
141
+ def default_pipeline(pipeline = nil)
142
+ @default_pipeline = pipeline.to_s unless pipeline.nil?
143
+ @default_pipeline
144
+ end
145
+
146
+ private
147
+
148
+ # Return a Relation instance to chain queries
149
+ #
150
+ def relation
151
+ Relation.create(self, {})
152
+ end
153
+
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,144 @@
1
+ module Stretchy
2
+ module Model
3
+ module Persistence
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ # Create a new instance and save it to the database.
8
+ #
9
+ # This class method is used to create a new instance of the model and save it to the database. It accepts a list of keyword attributes to set on the new instance.
10
+ #
11
+ # ### Parameters
12
+ #
13
+ # - `*args:` A list of keyword arguments representing the attributes to set on the new instance.
14
+ #
15
+ # ### Returns
16
+ # Returns the newly created instance of the model.
17
+ #
18
+ # ---
19
+ #
20
+ # ### Examples
21
+ #
22
+ # #### Create a new instance
23
+ #
24
+ # ```ruby
25
+ # Model.create(name: 'John Doe', age: 30)
26
+ # ```
27
+ #
28
+ def create(*args)
29
+ self.new(*args).save
30
+ end
31
+ end
32
+
33
+ # Save the current instance to the database.
34
+ #
35
+ # This instance method is used to save the current instance of the model to the database. If the instance is a new record, it runs the `:create` callbacks and saves the instance to the database. It then sets the `id` of the instance to the `_id` returned by the save operation.
36
+ #
37
+ # ### Returns
38
+ # Returns the `id` of the saved instance.
39
+ #
40
+ # ---
41
+ #
42
+ # ### Examples
43
+ #
44
+ # #### Save an instance
45
+ #
46
+ # ```ruby
47
+ # model = Model.new(name: 'John Doe', age: 30)
48
+ # model.save
49
+ # ```
50
+ #
51
+ def save
52
+ run_callbacks :save do
53
+ if new_record?
54
+ run_callbacks :create do
55
+ response = self.class.gateway.save(self.attributes)
56
+ self.id = response['_id']
57
+ end
58
+ else
59
+ self.class.gateway.save(self.attributes)
60
+ end
61
+ self
62
+ end
63
+ end
64
+
65
+ # Destroy the current instance.
66
+ #
67
+ # This instance method is used to delete the current instance of the model from the database.
68
+ # It differs from delete in that it runs the `:destroy` callbacks before deleting the instance.
69
+ #
70
+ #
71
+ # ### Returns
72
+ # Returns `true` if the instance was successfully deleted, `false` otherwise.
73
+ #
74
+ # ---
75
+ #
76
+ # ### Examples
77
+ #
78
+ # #### Delete an instance
79
+ #
80
+ # ```ruby
81
+ # model = Model.find(id: '1')
82
+ # model.destroy
83
+ # ```
84
+ #
85
+ def destroy
86
+ run_callbacks :destroy do
87
+ delete
88
+ end
89
+ end
90
+
91
+ # Delete the current instance from the database.
92
+ #
93
+ # This instance method is used to delete the current instance of the model from the database. It uses the `id` of the instance to perform the delete operation.
94
+ #
95
+ # ### Returns
96
+ # Returns `true` if the instance was successfully deleted, `false` otherwise.
97
+ #
98
+ # ---
99
+ #
100
+ # ### Examples
101
+ #
102
+ # #### Delete an instance
103
+ #
104
+ # ```ruby
105
+ # model = Model.find(id: '1')
106
+ # model.delete
107
+ # ```
108
+ #
109
+ def delete
110
+ self.class.gateway.delete(self.id)["result"] == 'deleted'
111
+ end
112
+
113
+ # Update the current instance with the given attributes and save it to the database.
114
+ #
115
+ # This instance method is used to update the current instance of the model with the given attributes and save it to the database. It accepts a list of keyword arguments representing the attributes to update.
116
+ #
117
+ # ### Parameters
118
+ #
119
+ # - `*args:` A list of keyword arguments representing the attributes to update.
120
+ #
121
+ # ### Returns
122
+ # Returns `true` if the instance was successfully updated, `false` otherwise.
123
+ #
124
+ # ---
125
+ #
126
+ # ### Examples
127
+ #
128
+ # #### Update an instance
129
+ #
130
+ # ```ruby
131
+ # model = Model.find(id: '1')
132
+ # model.update(name: 'Jane Doe', age: 32)
133
+ # ```
134
+ #
135
+ def update(*args)
136
+ run_callbacks :update do
137
+ self.assign_attributes(*args)
138
+ self.save
139
+ end
140
+ end
141
+
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,26 @@
1
+ module Stretchy
2
+ module Model
3
+ # Adds callbacks to the model to refresh the index after save or destroy.
4
+ #
5
+ # ```ruby
6
+ # included do
7
+ # after_save :refresh_index
8
+ # after_destroy :refresh_index
9
+ # end
10
+ # ```
11
+ module Refreshable
12
+ extend ActiveSupport::Concern
13
+
14
+
15
+ included do
16
+ after_save :refresh_index
17
+ after_destroy :refresh_index
18
+ end
19
+
20
+ def refresh_index
21
+ self.class.refresh_index!
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -30,7 +30,8 @@ module Stretchy
30
30
  end
31
31
  end
32
32
 
33
- def find(id)
33
+ def find(id=nil)
34
+ id = self.pipeline_name if id.nil?
34
35
  client.get_pipeline(id: id)
35
36
  end
36
37
 
@@ -1,43 +1,45 @@
1
1
  module Stretchy::Pipelines
2
2
  # Creates a new processor for a pipeline
3
3
  #
4
- # Processor type Description
5
- # append Adds one or more values to a field in a document.
6
- # bytes Converts a human-readable byte value to its value in bytes.
7
- # convert Changes the data type of a field in a document.
8
- # copy Copies an entire object in an existing field to another field.
9
- # csv Extracts CSVs and stores them as individual fields in a document.
10
- # date Parses dates from fields and then uses the date or timestamp as the timestamp for a document.
11
- # date_index_name Indexes documents into time-based indexes based on a date or timestamp field in a document.
12
- # dissect Extracts structured fields from a text field using a defined pattern.
13
- # dot_expander Expands a field with dots into an object field.
14
- # drop Drops a document without indexing it or raising any errors.
15
- # fail Raises an exception and stops the execution of a pipeline.
16
- # foreach Allows for another processor to be applied to each element of an array or an object field in a document.
17
- # geoip Adds information about the geographical location of an IP address.
18
- # geojson-feature Indexes GeoJSON data into a geospatial field.
19
- # grok Parses and structures unstructured data using pattern matching.
20
- # gsub Replaces or deletes substrings within a string field of a document.
21
- # html_strip Removes HTML tags from a text field and returns the plain text content.
22
- # ip2geo Adds information about the geographical location of an IPv4 or IPv6 address.
23
- # join Concatenates each element of an array into a single string using a separator character between each element.
24
- # json Converts a JSON string into a structured JSON object.
25
- # kv Automatically parses key-value pairs in a field.
26
- # lowercase Converts text in a specific field to lowercase letters.
27
- # pipeline Runs an inner pipeline.
28
- # remove Removes fields from a document.
29
- # script Runs an inline or stored script on incoming documents.
30
- # set Sets the value of a field to a specified value.
31
- # sort Sorts the elements of an array in ascending or descending order.
32
- # sparse_encoding Generates a sparse vector/token and weights from text fields for neural sparse search using sparse retrieval.
33
- # split Splits a field into an array using a separator character.
34
- # text_embedding Generates vector embeddings from text fields for semantic search.
35
- # text_image_embedding Generates combined vector embeddings from text and image fields for multimodal neural search.
36
- # trim Removes leading and trailing white space from a string field.
37
- # uppercase Converts text in a specific field to uppercase letters.
38
- # urldecode Decodes a string from URL-encoded format.
39
- # user_agent Extracts details from the user agent sent by a browser to its web requests.
4
+ # Below is a list of processors that can be used in a pipeline. Each processor has a specific function that can be applied to a document.
5
+ #
6
+ # For more information on processors, see the [Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-processors.html).
7
+ # or [OpenSearch](https://opensearch.org/docs/latest/ingest-pipelines/processors/index-processors/) documentation.
40
8
  #
9
+ # | type | Description |
10
+ # | --- | --- |
11
+ # | append | Adds one or more values to a field in a document. |
12
+ # | bytes | Converts a human-readable byte value to its value in bytes. |
13
+ # | convert | Changes the data type of a field in a document. |
14
+ # | copy | Copies an entire object in an existing field to another field. |
15
+ # | csv | Extracts CSVs and stores them as individual fields in a document. |
16
+ # | date | Parses dates from fields and then uses the date or timestamp as the timestamp for a document. |
17
+ # | date_index_name | Indexes documents into time-based indexes based on a date or timestamp field in a document. |
18
+ # | dissect | Extracts structured fields from a text field using a defined pattern. |
19
+ # | dot_expander | Expands a field with dots into an object field. |
20
+ # | drop | Drops a document without indexing it or raising any errors. |
21
+ # | fail | Raises an exception and stops the execution of a pipeline. |
22
+ # | foreach | Allows for another processor to be applied to each element of an array or an object field in a document. |
23
+ # | geoip | Adds information about the geographical location of an IP address. |
24
+ # | geojson-feature | Indexes GeoJSON data into a geospatial field. |
25
+ # | grok | Parses and structures unstructured data using pattern matching. |
26
+ # | gsub | Replaces or deletes substrings within a string field of a document. |
27
+ # | html_strip | Removes HTML tags from a text field and returns the plain text content. |
28
+ # | ip2geo | Adds information about the geographical location of an IPv4 or IPv6 address. |
29
+ # | join | Concatenates each element of an array into a single string using a separator character between each element. |
30
+ # | json | Converts a JSON string into a structured JSON object. |
31
+ # | kv | Automatically parses key-value pairs in a field. |
32
+ # | lowercase | Converts text in a specific field to lowercase letters. |
33
+ # | pipeline | Runs an inner pipeline. |
34
+ # | remove | Removes fields from a document. |
35
+ # | script | Runs an inline or stored script on incoming documents. |
36
+ # | set | Sets the value of a field to a specified value. |
37
+ # | sort | Sorts the elements of an array in ascending or descending order. |
38
+ # | sparse_encoding | Generates a sparse vector/token and weights from text fields for neural sparse search using sparse retrieval. |
39
+ # | split | Splits a field into an array using a separator character. |
40
+ # | text_embedding | Generates vector embeddings from text fields for semantic search. |
41
+ # | text_image_embedding | Generates combined vector embeddings from text and image fields for multimodal neural search. |
42
+
41
43
  class Processor
42
44
 
43
45
  attr_reader :type, :opts, :description
@@ -1,13 +1,13 @@
1
1
  module Stretchy
2
2
  module Querying
3
- delegate :first, :first!, :last, :last!, :exists?, :has_field, :any?, :many?, to: :all
4
- delegate :order, :limit, :size, :sort, :rewhere, :eager_load, :includes, :create_with, :none, :unscope, to: :all
5
- delegate :or_filter, :fields, :source, :highlight, to: :all
6
- delegate :neural_sparse, :neural, :hybrid, to: :all
7
- delegate *Stretchy::Relations::AggregationMethods::AGGREGATION_METHODS, to: :all
3
+ delegate :exists?, :any?, :many?, :includes, to: :all
4
+ delegate :rewhere, :eager_load, :create_with, :unscoped, to: :all
5
+
6
+ delegate *Stretchy::Relations::FinderMethods::METHODS, to: :all
7
+ delegate *Stretchy::Relations::SearchOptionMethods::METHODS, to: :all
8
+ delegate *Stretchy::Relations::QueryMethods.registry, to: :all
9
+ delegate *Stretchy::Relations::AggregationMethods.registry, to: :all
8
10
 
9
- delegate :skip_callbacks, :routing, :search_options, to: :all
10
- delegate :must, :must_not, :should, :where_not, :where, :filter_query, :query_string, :regexp, to: :all
11
11
 
12
12
  def fetch_results(es)
13
13
  if es.count?
@@ -17,6 +17,5 @@ module Stretchy
17
17
  end
18
18
  end
19
19
 
20
-
21
20
  end
22
21
  end
@@ -16,12 +16,12 @@ module Stretchy
16
16
  include ActiveModel::Serializers::JSON
17
17
 
18
18
  include Stretchy::Model::Callbacks
19
+ include Stretchy::Model::Common
20
+ include Stretchy::Model::Persistence
21
+ include Stretchy::Model::Refreshable
19
22
  include Stretchy::Indexing::Bulk
20
- include Stretchy::Persistence
21
23
  include Stretchy::Associations
22
- include Stretchy::Refreshable
23
- include Stretchy::Common
24
- include Stretchy::Scoping
24
+ include Stretchy::Relations::Scoping
25
25
  include Stretchy::Utils
26
26
  include Stretchy::SharedScopes
27
27
  include Stretchy::Attributes
@@ -46,6 +46,7 @@ module Stretchy
46
46
  def initialize(attributes = {})
47
47
  @highlights = attributes.delete(:_highlights)
48
48
  super(attributes)
49
+ run_callbacks :initialize
49
50
  end
50
51
 
51
52
  end