stretchy-model 0.6.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) 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 +15 -0
  9. data/docs/examples/_sidebar.md +15 -0
  10. data/docs/examples/data_analysis.md +216 -0
  11. data/docs/examples/neural_search_with_llm.md +381 -0
  12. data/docs/examples/simple-ingest-pipeline.md +326 -0
  13. data/docs/guides/_sidebar.md +15 -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/elasticsearch/api/actions/connector/check_in.rb +64 -0
  26. data/lib/elasticsearch/api/actions/connector/delete.rb +64 -0
  27. data/lib/elasticsearch/api/actions/connector/get.rb +64 -0
  28. data/lib/elasticsearch/api/actions/connector/last_sync.rb +66 -0
  29. data/lib/elasticsearch/api/actions/connector/list.rb +60 -0
  30. data/lib/elasticsearch/api/actions/connector/post.rb +57 -0
  31. data/lib/elasticsearch/api/actions/connector/put.rb +66 -0
  32. data/lib/elasticsearch/api/actions/connector/update_api_key_id.rb +66 -0
  33. data/lib/elasticsearch/api/actions/connector/update_configuration.rb +66 -0
  34. data/lib/elasticsearch/api/actions/connector/update_error.rb +66 -0
  35. data/lib/elasticsearch/api/actions/connector/update_filtering.rb +66 -0
  36. data/lib/elasticsearch/api/actions/connector/update_index_name.rb +66 -0
  37. data/lib/elasticsearch/api/actions/connector/update_name.rb +66 -0
  38. data/lib/elasticsearch/api/actions/connector/update_native.rb +66 -0
  39. data/lib/elasticsearch/api/actions/connector/update_pipeline.rb +66 -0
  40. data/lib/elasticsearch/api/actions/connector/update_scheduling.rb +66 -0
  41. data/lib/elasticsearch/api/actions/connector/update_service_type.rb +66 -0
  42. data/lib/elasticsearch/api/actions/connector/update_status.rb +66 -0
  43. data/lib/elasticsearch/api/namespace/connector.rb +36 -0
  44. data/lib/opensearch/api/actions/machine_learning/connector/delete.rb +42 -0
  45. data/lib/opensearch/api/actions/machine_learning/connector/get.rb +42 -0
  46. data/lib/opensearch/api/actions/machine_learning/connector/list.rb +38 -0
  47. data/lib/opensearch/api/actions/machine_learning/connector/post.rb +35 -0
  48. data/lib/opensearch/api/actions/machine_learning/connector/put.rb +44 -0
  49. data/lib/opensearch/api/actions/machine_learning/models/predict.rb +32 -0
  50. data/lib/opensearch/api/namespace/connector.rb +19 -0
  51. data/lib/stretchy/attributes/transformers/keyword_transformer.rb +41 -35
  52. data/lib/stretchy/attributes/type/array.rb +24 -1
  53. data/lib/stretchy/attributes/type/base.rb +6 -2
  54. data/lib/stretchy/attributes/type/binary.rb +24 -17
  55. data/lib/stretchy/attributes/type/boolean.rb +29 -22
  56. data/lib/stretchy/attributes/type/completion.rb +18 -10
  57. data/lib/stretchy/attributes/type/constant_keyword.rb +35 -26
  58. data/lib/stretchy/attributes/type/date_time.rb +28 -17
  59. data/lib/stretchy/attributes/type/dense_vector.rb +46 -49
  60. data/lib/stretchy/attributes/type/flattened.rb +28 -19
  61. data/lib/stretchy/attributes/type/geo_point.rb +21 -12
  62. data/lib/stretchy/attributes/type/geo_shape.rb +21 -12
  63. data/lib/stretchy/attributes/type/hash.rb +24 -10
  64. data/lib/stretchy/attributes/type/histogram.rb +25 -0
  65. data/lib/stretchy/attributes/type/ip.rb +26 -17
  66. data/lib/stretchy/attributes/type/join.rb +16 -7
  67. data/lib/stretchy/attributes/type/keyword.rb +21 -26
  68. data/lib/stretchy/attributes/type/knn_vector.rb +47 -0
  69. data/lib/stretchy/attributes/type/match_only_text.rb +22 -1
  70. data/lib/stretchy/attributes/type/nested.rb +16 -11
  71. data/lib/stretchy/attributes/type/numeric/base.rb +30 -22
  72. data/lib/stretchy/attributes/type/numeric/byte.rb +20 -0
  73. data/lib/stretchy/attributes/type/numeric/double.rb +20 -0
  74. data/lib/stretchy/attributes/type/numeric/float.rb +20 -0
  75. data/lib/stretchy/attributes/type/numeric/half_float.rb +20 -0
  76. data/lib/stretchy/attributes/type/numeric/integer.rb +21 -1
  77. data/lib/stretchy/attributes/type/numeric/long.rb +20 -0
  78. data/lib/stretchy/attributes/type/numeric/scaled_float.rb +16 -7
  79. data/lib/stretchy/attributes/type/numeric/short.rb +20 -0
  80. data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +21 -1
  81. data/lib/stretchy/attributes/type/percolator.rb +16 -4
  82. data/lib/stretchy/attributes/type/point.rb +19 -9
  83. data/lib/stretchy/attributes/type/range/base.rb +24 -1
  84. data/lib/stretchy/attributes/type/range/date_range.rb +21 -5
  85. data/lib/stretchy/attributes/type/range/double_range.rb +20 -4
  86. data/lib/stretchy/attributes/type/range/float_range.rb +21 -5
  87. data/lib/stretchy/attributes/type/range/integer_range.rb +20 -4
  88. data/lib/stretchy/attributes/type/range/ip_range.rb +20 -4
  89. data/lib/stretchy/attributes/type/range/long_range.rb +20 -4
  90. data/lib/stretchy/attributes/type/rank_feature.rb +16 -6
  91. data/lib/stretchy/attributes/type/rank_features.rb +16 -9
  92. data/lib/stretchy/attributes/type/search_as_you_type.rb +28 -18
  93. data/lib/stretchy/attributes/type/shape.rb +19 -9
  94. data/lib/stretchy/attributes/type/sparse_vector.rb +25 -21
  95. data/lib/stretchy/attributes/type/string.rb +42 -1
  96. data/lib/stretchy/attributes/type/text.rb +53 -28
  97. data/lib/stretchy/attributes/type/token_count.rb +21 -11
  98. data/lib/stretchy/attributes/type/version.rb +16 -6
  99. data/lib/stretchy/attributes/type/wildcard.rb +36 -25
  100. data/lib/stretchy/attributes.rb +29 -0
  101. data/lib/stretchy/delegation/gateway_delegation.rb +78 -0
  102. data/lib/stretchy/index_setting.rb +94 -0
  103. data/lib/stretchy/indexing/bulk.rb +75 -3
  104. data/lib/stretchy/machine_learning/connector.rb +130 -0
  105. data/lib/stretchy/machine_learning/errors.rb +25 -0
  106. data/lib/stretchy/machine_learning/model.rb +162 -109
  107. data/lib/stretchy/machine_learning/registry.rb +19 -0
  108. data/lib/stretchy/model/callbacks.rb +1 -0
  109. data/lib/stretchy/model/common.rb +157 -0
  110. data/lib/stretchy/model/persistence.rb +144 -0
  111. data/lib/stretchy/model/refreshable.rb +26 -0
  112. data/lib/stretchy/open_search_compatibility.rb +2 -0
  113. data/lib/stretchy/pipeline.rb +2 -1
  114. data/lib/stretchy/pipelines/processor.rb +40 -36
  115. data/lib/stretchy/querying.rb +7 -8
  116. data/lib/stretchy/rails/railtie.rb +11 -0
  117. data/lib/stretchy/rails/tasks/connector/create.rake +32 -0
  118. data/lib/stretchy/rails/tasks/connector/delete.rake +27 -0
  119. data/lib/stretchy/rails/tasks/connector/status.rake +31 -0
  120. data/lib/stretchy/rails/tasks/connector/update.rake +32 -0
  121. data/lib/stretchy/rails/tasks/index/create.rake +28 -0
  122. data/lib/stretchy/rails/tasks/index/delete.rake +27 -0
  123. data/lib/stretchy/rails/tasks/index/status.rake +23 -0
  124. data/lib/stretchy/rails/tasks/ml/delete.rake +25 -0
  125. data/lib/stretchy/rails/tasks/ml/deploy.rake +78 -0
  126. data/lib/stretchy/rails/tasks/ml/status.rake +31 -0
  127. data/lib/stretchy/rails/tasks/pipeline/create.rake +27 -0
  128. data/lib/stretchy/rails/tasks/pipeline/delete.rake +26 -0
  129. data/lib/stretchy/rails/tasks/pipeline/status.rake +25 -0
  130. data/lib/stretchy/rails/tasks/status.rake +15 -0
  131. data/lib/stretchy/rails/tasks/stretchy.rake +42 -0
  132. data/lib/stretchy/record.rb +5 -4
  133. data/lib/stretchy/relation.rb +229 -28
  134. data/lib/stretchy/relations/aggregation_methods/aggregation.rb +59 -0
  135. data/lib/stretchy/relations/aggregation_methods/avg.rb +45 -0
  136. data/lib/stretchy/relations/aggregation_methods/bucket_script.rb +47 -0
  137. data/lib/stretchy/relations/aggregation_methods/bucket_selector.rb +47 -0
  138. data/lib/stretchy/relations/aggregation_methods/bucket_sort.rb +47 -0
  139. data/lib/stretchy/relations/aggregation_methods/cardinality.rb +47 -0
  140. data/lib/stretchy/relations/aggregation_methods/children.rb +47 -0
  141. data/lib/stretchy/relations/aggregation_methods/composite.rb +41 -0
  142. data/lib/stretchy/relations/aggregation_methods/date_histogram.rb +53 -0
  143. data/lib/stretchy/relations/aggregation_methods/date_range.rb +53 -0
  144. data/lib/stretchy/relations/aggregation_methods/extended_stats.rb +48 -0
  145. data/lib/stretchy/relations/aggregation_methods/filter.rb +47 -0
  146. data/lib/stretchy/relations/aggregation_methods/filters.rb +47 -0
  147. data/lib/stretchy/relations/aggregation_methods/geo_bounds.rb +40 -0
  148. data/lib/stretchy/relations/aggregation_methods/geo_centroid.rb +40 -0
  149. data/lib/stretchy/relations/aggregation_methods/global.rb +39 -0
  150. data/lib/stretchy/relations/aggregation_methods/histogram.rb +43 -0
  151. data/lib/stretchy/relations/aggregation_methods/ip_range.rb +41 -0
  152. data/lib/stretchy/relations/aggregation_methods/max.rb +40 -0
  153. data/lib/stretchy/relations/aggregation_methods/min.rb +41 -0
  154. data/lib/stretchy/relations/aggregation_methods/missing.rb +40 -0
  155. data/lib/stretchy/relations/aggregation_methods/nested.rb +40 -0
  156. data/lib/stretchy/relations/aggregation_methods/percentile_ranks.rb +45 -0
  157. data/lib/stretchy/relations/aggregation_methods/percentiles.rb +45 -0
  158. data/lib/stretchy/relations/aggregation_methods/range.rb +42 -0
  159. data/lib/stretchy/relations/aggregation_methods/reverse_nested.rb +40 -0
  160. data/lib/stretchy/relations/aggregation_methods/sampler.rb +40 -0
  161. data/lib/stretchy/relations/aggregation_methods/scripted_metric.rb +43 -0
  162. data/lib/stretchy/relations/aggregation_methods/significant_terms.rb +45 -0
  163. data/lib/stretchy/relations/aggregation_methods/stats.rb +42 -0
  164. data/lib/stretchy/relations/aggregation_methods/sum.rb +42 -0
  165. data/lib/stretchy/relations/aggregation_methods/terms.rb +46 -0
  166. data/lib/stretchy/relations/aggregation_methods/top_hits.rb +42 -0
  167. data/lib/stretchy/relations/aggregation_methods/top_metrics.rb +44 -0
  168. data/lib/stretchy/relations/aggregation_methods/value_count.rb +41 -0
  169. data/lib/stretchy/relations/aggregation_methods/weighted_avg.rb +42 -0
  170. data/lib/stretchy/relations/aggregation_methods.rb +20 -749
  171. data/lib/stretchy/relations/finder_methods.rb +2 -18
  172. data/lib/stretchy/relations/null_relation.rb +55 -0
  173. data/lib/stretchy/relations/query_builder.rb +82 -36
  174. data/lib/stretchy/relations/query_methods/bind.rb +19 -0
  175. data/lib/stretchy/relations/query_methods/extending.rb +29 -0
  176. data/lib/stretchy/relations/query_methods/fields.rb +70 -0
  177. data/lib/stretchy/relations/query_methods/filter_query.rb +53 -0
  178. data/lib/stretchy/relations/query_methods/has_field.rb +40 -0
  179. data/lib/stretchy/relations/query_methods/highlight.rb +75 -0
  180. data/lib/stretchy/relations/query_methods/hybrid.rb +60 -0
  181. data/lib/stretchy/relations/query_methods/ids.rb +40 -0
  182. data/lib/stretchy/relations/query_methods/match.rb +52 -0
  183. data/lib/stretchy/relations/query_methods/must_not.rb +54 -0
  184. data/lib/stretchy/relations/query_methods/neural.rb +58 -0
  185. data/lib/stretchy/relations/query_methods/neural_sparse.rb +43 -0
  186. data/lib/stretchy/relations/query_methods/none.rb +21 -0
  187. data/lib/stretchy/relations/query_methods/or_filter.rb +21 -0
  188. data/lib/stretchy/relations/query_methods/order.rb +63 -0
  189. data/lib/stretchy/relations/query_methods/query_string.rb +44 -0
  190. data/lib/stretchy/relations/query_methods/regexp.rb +61 -0
  191. data/lib/stretchy/relations/query_methods/should.rb +51 -0
  192. data/lib/stretchy/relations/query_methods/size.rb +44 -0
  193. data/lib/stretchy/relations/query_methods/skip_callbacks.rb +47 -0
  194. data/lib/stretchy/relations/query_methods/source.rb +59 -0
  195. data/lib/stretchy/relations/query_methods/where.rb +113 -0
  196. data/lib/stretchy/relations/query_methods.rb +48 -569
  197. data/lib/stretchy/relations/scoping/default.rb +136 -0
  198. data/lib/stretchy/relations/scoping/named.rb +70 -0
  199. data/lib/stretchy/relations/scoping/scope_registry.rb +36 -0
  200. data/lib/stretchy/relations/scoping.rb +30 -0
  201. data/lib/stretchy/relations/search_option_methods.rb +2 -0
  202. data/lib/stretchy/version.rb +1 -1
  203. data/lib/stretchy.rb +24 -10
  204. metadata +170 -17
  205. data/lib/stretchy/common.rb +0 -38
  206. data/lib/stretchy/null_relation.rb +0 -53
  207. data/lib/stretchy/persistence.rb +0 -43
  208. data/lib/stretchy/refreshable.rb +0 -15
  209. data/lib/stretchy/scoping/default.rb +0 -134
  210. data/lib/stretchy/scoping/named.rb +0 -68
  211. data/lib/stretchy/scoping/scope_registry.rb +0 -34
  212. data/lib/stretchy/scoping.rb +0 -28
@@ -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
@@ -46,9 +48,11 @@ module Stretchy::Pipelines
46
48
  @type = type
47
49
  @description = opts[:description]
48
50
  @opts = opts
51
+ @model = opts.delete(:model)
49
52
  end
50
53
 
51
54
  def to_hash
55
+ @opts[:model_id] = @model.model_id if @model
52
56
  { type => @opts }
53
57
  end
54
58
  end
@@ -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
@@ -0,0 +1,11 @@
1
+ module Stretchy
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+
5
+ rake_tasks do
6
+ load "#{__dir__}/tasks/stretchy.rake"
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ namespace :stretchy do
2
+ namespace :connector do
3
+ desc "Create connector"
4
+ task create: :environment do
5
+ klass = ENV['MODEL']
6
+ Rails.application.eager_load!
7
+ models = klass.nil? ? Stretchy::MachineLearning::Connector.descendants : [klass.constantize]
8
+ models.each do |model|
9
+ spinner = TTY::Spinner.new("Creating Connector #{model} ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
10
+ spinner.auto_spin
11
+
12
+ # if model.exists?
13
+ # spinner.stop(Rainbow("[EXISTS]").yellow)
14
+ # next
15
+ # end
16
+
17
+ begin
18
+ response = model.create!
19
+ rescue Stretchy::MachineLearning::Errors::ConnectorMissingError => e
20
+ spinner.stop(Rainbow("[FAILED]").red)
21
+ next
22
+ end
23
+ status = if response
24
+ Rainbow("[SUCCESS]").green
25
+ else
26
+ Rainbow("[FAILED]").red
27
+ end
28
+ spinner.stop(status)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ namespace :stretchy do
2
+ namespace :connector do
3
+ desc "Delete Connector"
4
+ task delete: :environment do
5
+ klass = ENV['MODEL']
6
+ Rails.application.eager_load!
7
+ models = klass.nil? ? Stretchy::MachineLearning::Connector.descendants : [klass.constantize]
8
+ models.each do |model|
9
+ spinner = TTY::Spinner.new("Deleting Connector #{model} ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
10
+ spinner.auto_spin
11
+
12
+ begin
13
+ response = model.delete!
14
+ rescue Stretchy::MachineLearning::Errors::ConnectorMissingError => e
15
+ spinner.stop(Rainbow("[MISSING]").white)
16
+ next
17
+ end
18
+ status = if response['result'] == 'deleted'
19
+ Rainbow("[SUCCESS]").green
20
+ else
21
+ Rainbow("[#{response['result'].upcase}]").white
22
+ end
23
+ spinner.stop(status)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ namespace :stretchy do
2
+ namespace :connector do
3
+
4
+ desc "Check the status of all connectors"
5
+ task status: :environment do
6
+ klass = ENV['MODEL']
7
+
8
+ Rails.application.eager_load!
9
+ models = klass.nil? ? Stretchy::MachineLearning::Connector.descendants : [klass.constantize]
10
+
11
+ puts Rainbow("Connector").color :gray
12
+ models.each do |model|
13
+
14
+ begin
15
+ response = model.find
16
+ rescue Stretchy::MachineLearning::Errors::ConnectorMissingError => e
17
+ puts "#{model}".ljust(JUSTIFICATION) + Rainbow("[MISSING]").white
18
+ next
19
+ end
20
+ status = if response
21
+ Rainbow("[CREATED]").green.bright
22
+ else
23
+ Rainbow("[MISSING]").white
24
+ end
25
+ puts "#{model}".ljust(JUSTIFICATION) + status
26
+ end
27
+ puts "\n\n"
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ namespace :stretchy do
2
+ namespace :connector do
3
+ desc "Update connector"
4
+ task update: :environment do
5
+ klass = ENV['MODEL']
6
+ Rails.application.eager_load!
7
+ models = klass.nil? ? Stretchy::MachineLearning::Connector.descendants : [klass.constantize]
8
+ models.each do |model|
9
+ spinner = TTY::Spinner.new("Updating Connector #{model} ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
10
+ spinner.auto_spin
11
+
12
+ # if model.exists?
13
+ # spinner.stop(Rainbow("[EXISTS]").yellow)
14
+ # next
15
+ # end
16
+
17
+ begin
18
+ response = model.update!
19
+ rescue Stretchy::MachineLearning::Errors::ConnectorMissingError => e
20
+ spinner.stop(Rainbow("[FAILED]").red)
21
+ next
22
+ end
23
+ status = if response
24
+ Rainbow("[SUCCESS]").green
25
+ else
26
+ Rainbow("[FAILED]").red
27
+ end
28
+ spinner.stop(status)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ namespace :stretchy do
2
+ namespace :index do
3
+ desc "Create indices"
4
+ task create: :environment do
5
+ klass = ENV['MODEL']
6
+ Rails.application.eager_load!
7
+ models = klass.nil? ? StretchyModel.descendants : [klass.constantize]
8
+ models.each do |model|
9
+ next if model.name == "Stretchy::MachineLearning::Registry"
10
+ spinner = TTY::Spinner.new("Creating index #{model} (#{model.index_name}) ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
11
+ spinner.auto_spin
12
+ if model.index_exists?
13
+ spinner.stop( Rainbow("[EXISTS]").orange)
14
+ next
15
+ end
16
+
17
+ response = model.create_index!
18
+ status = if response
19
+ Rainbow("[SUCCESS]").green
20
+ else
21
+ Rainbow("[FAILED]").red
22
+ end
23
+
24
+ spinner.stop(status)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ namespace :stretchy do
2
+ namespace :index do
3
+ desc "Delete indices"
4
+ task delete: :environment do
5
+ klass = ENV['MODEL']
6
+ models = klass.nil? ? StretchyModel.descendants : [klass.constantize]
7
+ models.each do |model|
8
+ next if model.name == "Stretchy::MachineLearning::Registry"
9
+ spinner = TTY::Spinner.new("Deleting index #{model} (#{model.index_name}) ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
10
+ spinner.auto_spin
11
+ unless model.index_exists?
12
+ spinner.stop( Rainbow("[MISSING]").white)
13
+ next
14
+ end
15
+
16
+ response = model.delete_index!
17
+ status = if response
18
+ Rainbow("[SUCCESS]").green
19
+ else
20
+ Rainbow("[FAILED]").red
21
+ end
22
+
23
+ spinner.stop(status)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ namespace :stretchy do
2
+ namespace :index do
3
+ desc "Check the status of all indexes"
4
+ task status: :environment do
5
+ klass = ENV['MODEL']
6
+
7
+ Rails.application.eager_load!
8
+ models = klass.nil? ? StretchyModel.descendants : [klass.constantize]
9
+
10
+ puts Rainbow("Indexes").color :gray
11
+ models.each do |model|
12
+ response = model.index_exists?
13
+ status = if response
14
+ Rainbow("[CREATED]").green.bright
15
+ else
16
+ Rainbow("[MISSING]").white
17
+ end
18
+ puts "#{model.index_name}".ljust(JUSTIFICATION) + status
19
+ end
20
+ puts "\n\n"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ namespace :stretchy do
2
+ namespace :ml do
3
+
4
+ desc "Delete the model ENV['MODEL']"
5
+ task delete: :environment do
6
+ klass = ENV['MODEL']
7
+ Rails.application.eager_load!
8
+ models = klass.nil? ? Stretchy::MachineLearning::Model.descendants : [klass.constantize]
9
+
10
+ models.each do |model|
11
+ spinner = TTY::Spinner.new(("Deleting #{model} ".ljust(JUSTIFICATION) + ":spinner"), format: :dots)
12
+ spinner.auto_spin
13
+ unless model.registered?
14
+ spinner.stop(Rainbow("[MISSING]").white)
15
+ next
16
+ end
17
+
18
+ model.delete
19
+
20
+ status = !model.registered? ? Rainbow("[DELETED]").green : Rainbow("[NOT DELETED]").yellow
21
+ spinner.stop(status)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,78 @@
1
+ namespace :stretchy do
2
+ namespace :ml do
3
+
4
+ desc "Register the model ENV['MODEL']"
5
+ task register: :environment do
6
+ klass = ENV['MODEL']
7
+ Rails.application.eager_load!
8
+ models = klass.nil? ? Stretchy::MachineLearning::Model.descendants : [klass.constantize]
9
+
10
+ models.each do |model|
11
+ spinner = TTY::Spinner.new(("Registering #{model} ".ljust(JUSTIFICATION) + ":spinner"), format: :dots)
12
+ spinner.auto_spin
13
+ if model.registered?
14
+ spinner.stop(Rainbow("[SUCCESS]").green)
15
+ next
16
+ end
17
+
18
+ model.register do |m|
19
+ m.wait_until_complete do
20
+ m.registered?
21
+ end
22
+ end
23
+
24
+ status = model.registered? ? Rainbow("[SUCCESS]").green : Rainbow("[FAILED]").red
25
+ spinner.stop(status)
26
+ end
27
+ end
28
+
29
+ desc "Undeploy the model ENV['MODEL']"
30
+ task undeploy: :environment do
31
+ klass = ENV['MODEL']
32
+ Rails.application.eager_load!
33
+ models = klass.nil? ? Stretchy::MachineLearning::Model.descendants : [klass.constantize]
34
+ models.each do |model|
35
+ spinner = TTY::Spinner.new("Undeploying #{model} ". ljust(JUSTIFICATION) + ":spinner", format: :dots)
36
+ spinner.auto_spin
37
+
38
+ unless model.deployed?
39
+ spinner.stop(Rainbow("[NOT DEPLOYED]").white)
40
+ next
41
+ end
42
+
43
+ model.undeploy do |m|
44
+ m.wait_until_complete do
45
+ !m.deployed?
46
+ end
47
+ end
48
+ status = !model.deployed? ? Rainbow("[SUCCESS]").green : Rainbow("[DEPLOYED]").yellow
49
+ spinner.stop(status)
50
+ end
51
+ end
52
+
53
+ desc "Deploy the model ENV['MODEL']"
54
+ task deploy: :environment do
55
+ klass = ENV['MODEL']
56
+ Rails.application.eager_load!
57
+ models = klass.nil? ? Stretchy::MachineLearning::Model.descendants : [klass.constantize]
58
+ models.each do |model|
59
+ spinner = TTY::Spinner.new("Deploying #{model} ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
60
+ spinner.auto_spin
61
+
62
+ if model.deployed?
63
+ spinner.stop(Rainbow("[SUCCESS]").green)
64
+ next
65
+ end
66
+
67
+ model.deploy do |m|
68
+ m.wait_until_complete do
69
+ m.deployed?
70
+ end
71
+ end
72
+ status = model.deployed? ? Rainbow("[SUCCESS]").green : Rainbow("[FAILED]").red
73
+ spinner.stop(status)
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,31 @@
1
+ namespace :stretchy do
2
+ namespace :ml do
3
+
4
+ desc "Check the status of all ml models"
5
+ task status: :environment do
6
+ klass = ENV['MODEL']
7
+
8
+ Rails.application.eager_load!
9
+ models = klass.nil? ? Stretchy::MachineLearning::Model.descendants : [klass.constantize]
10
+
11
+ puts Rainbow("Machine Learning").color :gray
12
+ models.each do |model|
13
+
14
+ response = model.find['model_state']
15
+ status = case response
16
+ when 'DEPLOYED'
17
+ Rainbow("[#{response}]").green.bright
18
+ when 'DEPLOY_FAILED'
19
+ Rainbow("[#{response}]").red.bright
20
+ when 'CREATED'
21
+ Rainbow("[#{response}]").yellow.bright
22
+ else
23
+ Rainbow("[MISSING]").white
24
+ end
25
+ puts "#{model}".ljust(JUSTIFICATION) + status
26
+ end
27
+ puts "\n\n"
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ namespace :stretchy do
2
+ namespace :pipeline do
3
+ desc "Create pipeline"
4
+ task create: :environment do
5
+ klass = ENV['MODEL']
6
+ Rails.application.eager_load!
7
+ models = klass.nil? ? Stretchy::Pipeline.descendants : [klass.constantize]
8
+ models.each do |model|
9
+ spinner = TTY::Spinner.new("Creating Pipeline #{model} ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
10
+ spinner.auto_spin
11
+
12
+ begin
13
+ response = model.create!
14
+ rescue => e
15
+ spinner.stop(Rainbow("[FAILED]").red)
16
+ next
17
+ end
18
+ status = if response['acknowledged']
19
+ Rainbow("[SUCCESS]").green
20
+ else
21
+ Rainbow("[FAILED]").red
22
+ end
23
+ spinner.stop(status)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ namespace :stretchy do
2
+ namespace :pipeline do
3
+ desc "Delete pipeline"
4
+ task delete: :environment do
5
+ klass = ENV['MODEL']
6
+ models = klass.nil? ? Stretchy::Pipeline.descendants : [klass.constantize]
7
+ models.each do |model|
8
+ spinner = TTY::Spinner.new("Deleting Pipeline #{model} ".ljust(JUSTIFICATION) + ":spinner", format: :dots)
9
+ spinner.auto_spin
10
+
11
+ unless model.exists?
12
+ spinner.stop(Rainbow("[MISSING]").white)
13
+ next
14
+ end
15
+
16
+ response = model.delete!
17
+ status = if response['acknowledged']
18
+ Rainbow("[SUCCESS]").green
19
+ else
20
+ Rainbow("[FAILED]").red
21
+ end
22
+ spinner.stop(status)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ namespace :stretchy do
2
+ namespace :pipeline do
3
+
4
+ desc "Check the status of all pipelines"
5
+ task status: :environment do
6
+ klass = ENV['MODEL']
7
+
8
+ Rails.application.eager_load!
9
+ models = klass.nil? ? Stretchy::Pipeline.descendants : [klass.constantize]
10
+
11
+ puts Rainbow("Pipelines").color :gray
12
+ models.each do |model|
13
+ response = model.exists?
14
+ status = if response
15
+ Rainbow("[CREATED]").green.bright
16
+ else
17
+ Rainbow("[MISSING]").white
18
+ end
19
+ puts "#{model.pipeline_name}".ljust(JUSTIFICATION) + status
20
+ end
21
+ puts "\n\n"
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ namespace :stretchy do
2
+ namespace :status do
3
+ desc "Check the status of all indexes"
4
+ task all: :environment do
5
+ Rake::Task['stretchy:index:status'].invoke
6
+ Rake::Task['stretchy:pipeline:status'].invoke
7
+ Rake::Task['stretchy:ml:status'].invoke
8
+ Rake::Task['stretchy:connector:status'].invoke
9
+ end
10
+ end
11
+
12
+ desc "Check the status of all indexes, pipelines and ml models"
13
+ task status: 'stretchy:status:all' do
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ require 'stretchy-model'
2
+ require 'tty-spinner'
3
+
4
+ JUSTIFICATION = 90 unless defined?(JUSTIFICATION)
5
+
6
+ path = File.expand_path(__dir__)
7
+ Dir.glob("#{path}/**/*.rake").each do |f|
8
+ import f unless f == __FILE__
9
+ end
10
+
11
+ namespace :stretchy do
12
+ desc "Create all indexes, pipelines and deploy all models"
13
+ task up: :environment do
14
+ Rake::Task['stretchy:connector:create'].invoke
15
+ Rake::Task['stretchy:ml:register'].invoke
16
+ Rake::Task['stretchy:ml:deploy'].invoke
17
+ Rake::Task['stretchy:pipeline:create'].invoke
18
+ Rake::Task['stretchy:index:create'].invoke
19
+ end
20
+
21
+ desc "Delete all indexes, pipelines and undeploy all models"
22
+ task down: :environment do
23
+ Rake::Task['stretchy:ml:undeploy'].invoke
24
+ Rake::Task['stretchy:ml:delete'].invoke
25
+ Rake::Task['stretchy:connector:delete'].invoke
26
+ Rake::Task['stretchy:pipeline:delete'].invoke
27
+ Rake::Task['stretchy:index:delete'].invoke
28
+ end
29
+
30
+ desc "Enable Machine Learning on all nodes"
31
+ task ml_on_all_nodes: :environment do
32
+ puts "Enabling Machine Learning on all nodes..."
33
+ puts Stretchy::MachineLearning::Model.ml_on_all_nodes!
34
+ end
35
+
36
+ desc "Machine Learning on ML nodes only"
37
+ task ml_on_ml_nodes: :environment do
38
+ puts "Enabling Machine Learning on ML nodes only..."
39
+ puts Stretchy::MachineLearning::Model.ml_on_ml_nodes!
40
+ end
41
+
42
+ 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