stretchy-model 0.5.0 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/containers/Dockerfile.opensearch +4 -3
  3. data/docker-compose.yml +32 -19
  4. data/lib/elasticsearch/api/actions/machine_learning/models/delete_model.rb +33 -0
  5. data/lib/elasticsearch/api/actions/machine_learning/models/deploy.rb +31 -0
  6. data/lib/elasticsearch/api/actions/machine_learning/models/get_model.rb +43 -0
  7. data/lib/elasticsearch/api/actions/machine_learning/models/get_status.rb +31 -0
  8. data/lib/elasticsearch/api/actions/machine_learning/models/params_registry.rb +45 -0
  9. data/lib/elasticsearch/api/actions/machine_learning/models/register.rb +45 -0
  10. data/lib/elasticsearch/api/actions/machine_learning/models/undeploy.rb +32 -0
  11. data/lib/elasticsearch/api/actions/machine_learning/models/update_model.rb +39 -0
  12. data/lib/elasticsearch/api/namespace/machine_learning/model.rb +27 -0
  13. data/lib/opensearch/api/actions/machine_learning/models/delete_model.rb +33 -0
  14. data/lib/opensearch/api/actions/machine_learning/models/deploy.rb +31 -0
  15. data/lib/opensearch/api/actions/machine_learning/models/get_model.rb +44 -0
  16. data/lib/opensearch/api/actions/machine_learning/models/get_status.rb +31 -0
  17. data/lib/opensearch/api/actions/machine_learning/models/params_registry.rb +45 -0
  18. data/lib/opensearch/api/actions/machine_learning/models/register.rb +45 -0
  19. data/lib/opensearch/api/actions/machine_learning/models/undeploy.rb +31 -0
  20. data/lib/opensearch/api/actions/machine_learning/models/update_model.rb +39 -0
  21. data/lib/opensearch/api/namespace/machine_learning/model.rb +27 -0
  22. data/lib/stretchy/associations.rb +1 -1
  23. data/lib/stretchy/attributes/type/array.rb +16 -11
  24. data/lib/stretchy/attributes/type/base.rb +42 -0
  25. data/lib/stretchy/attributes/type/binary.rb +45 -0
  26. data/lib/stretchy/attributes/type/boolean.rb +48 -0
  27. data/lib/stretchy/attributes/type/completion.rb +25 -0
  28. data/lib/stretchy/attributes/type/constant_keyword.rb +38 -0
  29. data/lib/stretchy/attributes/type/date_time.rb +85 -0
  30. data/lib/stretchy/attributes/type/dense_vector.rb +59 -0
  31. data/lib/stretchy/attributes/type/flattened.rb +31 -0
  32. data/lib/stretchy/attributes/type/geo_point.rb +27 -0
  33. data/lib/stretchy/attributes/type/geo_shape.rb +27 -0
  34. data/lib/stretchy/attributes/type/hash.rb +36 -13
  35. data/lib/stretchy/attributes/type/histogram.rb +7 -0
  36. data/lib/stretchy/attributes/type/ip.rb +29 -0
  37. data/lib/stretchy/attributes/type/join.rb +22 -0
  38. data/lib/stretchy/attributes/type/keyword.rb +36 -10
  39. data/lib/stretchy/attributes/type/match_only_text.rb +8 -0
  40. data/lib/stretchy/attributes/type/nested.rb +25 -0
  41. data/lib/stretchy/attributes/type/numeric/base.rb +32 -0
  42. data/lib/stretchy/attributes/type/numeric/byte.rb +7 -0
  43. data/lib/stretchy/attributes/type/numeric/double.rb +7 -0
  44. data/lib/stretchy/attributes/type/numeric/float.rb +7 -0
  45. data/lib/stretchy/attributes/type/numeric/half_float.rb +7 -0
  46. data/lib/stretchy/attributes/type/numeric/integer.rb +7 -0
  47. data/lib/stretchy/attributes/type/numeric/long.rb +7 -0
  48. data/lib/stretchy/attributes/type/numeric/scaled_float.rb +23 -0
  49. data/lib/stretchy/attributes/type/numeric/short.rb +7 -0
  50. data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +7 -0
  51. data/lib/stretchy/attributes/type/percolator.rb +23 -0
  52. data/lib/stretchy/attributes/type/point.rb +24 -0
  53. data/lib/stretchy/attributes/type/range/base.rb +9 -0
  54. data/lib/stretchy/attributes/type/range/date_range.rb +17 -0
  55. data/lib/stretchy/attributes/type/range/double_range.rb +17 -0
  56. data/lib/stretchy/attributes/type/range/float_range.rb +16 -0
  57. data/lib/stretchy/attributes/type/range/integer_range.rb +16 -0
  58. data/lib/stretchy/attributes/type/range/ip_range.rb +16 -0
  59. data/lib/stretchy/attributes/type/range/long_range.rb +16 -0
  60. data/lib/stretchy/attributes/type/rank_feature.rb +21 -0
  61. data/lib/stretchy/attributes/type/rank_features.rb +34 -0
  62. data/lib/stretchy/attributes/type/search_as_you_type.rb +30 -0
  63. data/lib/stretchy/attributes/type/shape.rb +24 -0
  64. data/lib/stretchy/attributes/type/sparse_vector.rb +37 -0
  65. data/lib/stretchy/attributes/type/string.rb +7 -0
  66. data/lib/stretchy/attributes/type/text.rb +50 -11
  67. data/lib/stretchy/attributes/type/token_count.rb +26 -0
  68. data/lib/stretchy/attributes/type/version.rb +21 -0
  69. data/lib/stretchy/attributes/type/wildcard.rb +35 -0
  70. data/lib/stretchy/attributes.rb +47 -0
  71. data/lib/stretchy/common.rb +8 -2
  72. data/lib/stretchy/delegation/gateway_delegation.rb +9 -5
  73. data/lib/stretchy/machine_learning/model.rb +192 -0
  74. data/lib/stretchy/open_search_compatibility.rb +4 -0
  75. data/lib/stretchy/pipeline.rb +123 -0
  76. data/lib/stretchy/pipelines/processor.rb +55 -0
  77. data/lib/stretchy/querying.rb +4 -3
  78. data/lib/stretchy/rails/instrumentation/publishers.rb +31 -0
  79. data/lib/{rails → stretchy/rails}/instrumentation/railtie.rb +12 -5
  80. data/lib/stretchy/record.rb +1 -1
  81. data/lib/stretchy/relation.rb +2 -1
  82. data/lib/stretchy/relations/finder_methods.rb +19 -2
  83. data/lib/stretchy/relations/query_builder.rb +80 -4
  84. data/lib/stretchy/relations/query_methods.rb +32 -0
  85. data/lib/stretchy/scoping/named.rb +1 -1
  86. data/lib/stretchy/shared_scopes.rb +6 -1
  87. data/lib/stretchy/version.rb +1 -1
  88. data/lib/stretchy.rb +12 -3
  89. metadata +83 -5
  90. data/lib/rails/instrumentation/publishers.rb +0 -29
@@ -0,0 +1,31 @@
1
+ module OpenSearch
2
+ module API
3
+ module MachineLearning
4
+ module Models
5
+ module Actions
6
+ # Register a model.
7
+ #
8
+ # @option arguments [String] :id The model id
9
+ # @option arguments [Hash] :body The deploy options
10
+ #
11
+ #
12
+ # POST /_plugins/_ml/models/<model_id>/_undeploy
13
+ def undeploy(arguments = {})
14
+ raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]
15
+ _id = arguments.delete(:id)
16
+ arguments = arguments.clone
17
+ headers = arguments.delete(:headers) || {}
18
+
19
+ method = OpenSearch::API::HTTP_POST
20
+ path = "_plugins/_ml/models/#{Utils.__listify(_id)}/_undeploy"
21
+ params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
22
+
23
+ body = arguments[:body]
24
+ perform_request(method, path, params, body, headers).body
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ module OpenSearch
2
+ module API
3
+ module MachineLearning
4
+ module Models
5
+ module Actions
6
+ # Returns a model.
7
+ #
8
+ # @option arguments [String] :id The model id
9
+ # @option arguments [Hash] :body The request fields
10
+ #
11
+ # Example
12
+ # update_model(id: 109sdj0asl092, "rate_limiter": {
13
+ # "limit": "4",
14
+ # "unit": "MINUTES"
15
+ # }
16
+ # )
17
+ #
18
+ # PUT /_plugins/_ml/models/<model_id>
19
+ def update_model(arguments = {})
20
+ raise ArgumentError, "Required argument 'body' missing" unless arguments[:body]
21
+ raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]
22
+
23
+ _id = arguments.delete(:id)
24
+
25
+ headers = arguments.delete(:headers) || {}
26
+
27
+ method = OpenSearch::API::HTTP_PUT
28
+ path = "_plugins/_ml/models/#{Utils.__listify(_id)}"
29
+ params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
30
+
31
+ body = arguments[:body]
32
+ perform_request(method, path, params, body, headers).body
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ Dir[File.expand_path('../../actions/**/*.rb', __dir__)].sort.each { |f| require f }
2
+
3
+ module OpenSearch
4
+ module API
5
+ module MachineLearning
6
+ module Models
7
+ module Actions; end
8
+
9
+ # Client for the "machine_learning/models" namespace (includes the {MachineLearning::Models::Actions} methods)
10
+ #
11
+ class MachineLearningClient
12
+ include MachineLearning::Models::Actions
13
+ include OpenSearch::API::Common::Client::Base
14
+ include OpenSearch::API::Common::Client
15
+ end
16
+
17
+ # Proxy method for {MachineLearningModel}, available in the receiving object
18
+ #
19
+ def machine_learning
20
+ @machine_learning ||= MachineLearningClient.new(self)
21
+ end
22
+
23
+ alias ml machine_learning
24
+ end
25
+ end
26
+ end
27
+ end
@@ -38,7 +38,7 @@ module Stretchy
38
38
  end
39
39
 
40
40
  def association_reflection(association)
41
- Stretchy::Relation.new @@_associations[association], (dirty[association.to_sym] || [])
41
+ Stretchy::Relation.new @@_associations[association], (dirty[association.to_sym] || {})
42
42
  end
43
43
 
44
44
  def _destroy=(bool)
@@ -1,15 +1,20 @@
1
- module Stretchy
2
- module Attributes
3
- module Type
4
-
5
- class Array < ActiveModel::Type::Value # :nodoc:
6
-
7
- def type
8
- :array
9
- end
1
+ module Stretchy::Attributes::Type
2
+ class Array < Stretchy::Attributes::Type::Base # :nodoc:
3
+ OPTIONS = [:data_type, :fields]
4
+ def type
5
+ :array
6
+ end
10
7
 
11
- end
8
+ def type_for_database
9
+ data_type || :text
10
+ end
12
11
 
13
- end
12
+ def mappings(name)
13
+ options = {type: type_for_database}
14
+ self.class::OPTIONS.each { |option| options[option] = send(option) unless send(option).nil? }
15
+ options.delete(:fields) if fields == false
16
+ options[:fields] = {keyword: {type: :keyword, ignore_above: 256}} if type_for_database == :text && fields.nil?
17
+ { name => options }.as_json
14
18
  end
19
+ end
15
20
  end
@@ -0,0 +1,42 @@
1
+ module Stretchy
2
+ module Attributes
3
+ module Type
4
+ class Base < ActiveModel::Type::Value
5
+
6
+ OPTIONS = []
7
+
8
+
9
+ def initialize(**args)
10
+
11
+ define_option_methods!
12
+
13
+ args.each do |k, v|
14
+ if self.class::OPTIONS.include?(k)
15
+ instance_variable_set("@#{k}", v)
16
+ args.delete(k)
17
+ end
18
+ end
19
+ super
20
+ end
21
+
22
+ def mappings(name)
23
+ options = {type: type_for_database}
24
+ self.class::OPTIONS.each { |option| options[option] = send(option) unless send(option).nil? }
25
+ { name => options }.as_json
26
+ end
27
+
28
+ def type_for_database
29
+ type
30
+ end
31
+
32
+ private
33
+
34
+ def define_option_methods!
35
+ self.class::OPTIONS.each do |option|
36
+ define_singleton_method(option.to_sym) { instance_variable_get("@#{option}") }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ # Public: Defines a binary attribute for the model.
2
+ #
3
+ # name - The Symbol name of the attribute.
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion.
6
+ # This allows it to be used later for sorting, aggregations, or scripting. Defaults to false.
7
+ # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
8
+ #
9
+ # Examples
10
+ #
11
+ # class MyModel
12
+ # include StretchyModel
13
+ # attribute :name, :binary, doc_values: true, store: true
14
+ # end
15
+ #
16
+ # Returns nothing.
17
+ module Stretchy
18
+ module Attributes
19
+ module Type
20
+ class Binary < ActiveModel::Type::Value
21
+ OPTIONS = [:doc_values, :store]
22
+ attr_reader *OPTIONS
23
+
24
+ def initialize(**args)
25
+ args.each do |k, v|
26
+ instance_variable_set("@#{k}", v) if OPTIONS.include?(k)
27
+ args.delete(k)
28
+ end
29
+ super
30
+ end
31
+
32
+ def type
33
+ :binary
34
+ end
35
+
36
+
37
+ def mappings(name)
38
+ options = {type: type}
39
+ OPTIONS.each { |_| options[_] = self.send(_) }
40
+ { name => options }.as_json
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ # Public: Defines a boolean attribute for the model.
2
+ #
3
+ # name - The Symbol name of the attribute.
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion.
6
+ # This allows it to be used later for sorting, aggregations, or scripting. Defaults to true.
7
+ # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true.
8
+ # :ignore_malformed - The Boolean indicating if exceptions thrown when trying to index the wrong data type into a field should be ignored. Defaults to false.
9
+ # :null_value - The Boolean value to be substituted for any explicit null values. Defaults to null.
10
+ # :on_script_error - The String defining what to do if the script defined by the :script parameter throws an error at indexing time. Can be 'fail' or 'continue'.
11
+ # :script - The String script that will index values generated by this script, rather than reading the values directly from the source.
12
+ # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
13
+ # :meta - The Hash metadata about the field.
14
+ #
15
+ # Examples
16
+ #
17
+ # class MyModel
18
+ # include StretchyModel
19
+ # attribute :name, :boolean, doc_values: true, store: true
20
+ # end
21
+ #
22
+ # Returns nothing.
23
+ module Stretchy
24
+ module Attributes
25
+ module Type
26
+ class Boolean < Stretchy::Attributes::Type::Base
27
+
28
+ OPTIONS = [:doc_values, :index, :ignore_malformed, :null_value, :on_script_error, :script, :store, :meta]
29
+ attr_reader *OPTIONS
30
+
31
+ def initialize(**args)
32
+ args.each do |k, v|
33
+ instance_variable_set("@#{k}", v) if OPTIONS.include?(k)
34
+ args.delete(k)
35
+ end
36
+ super
37
+ end
38
+
39
+ def type
40
+ :boolean
41
+ end
42
+
43
+
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,25 @@
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a completion attribute for the model.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :analyzer - The String index analyzer to use. Defaults to 'simple'.
6
+ # :search_analyzer - The String search analyzer to use. Defaults to the value of :analyzer.
7
+ # :preserve_separators - The Boolean indicating if separators should be preserved. Defaults to true.
8
+ # :preserve_position_increments - The Boolean indicating if position increments should be enabled. Defaults to true.
9
+ # :max_input_length - The Integer limit for the length of a single input. Defaults to 50.
10
+ #
11
+ # Examples
12
+ #
13
+ # class MyModel < Stretchy::Record
14
+ # attribute :name, :completion, analyzer: 'simple', max_input_length: 100
15
+ # end
16
+ #
17
+ # Returns nothing.
18
+ class Completion < Stretchy::Attributes::Type::Base
19
+ OPTIONS = [:analyzer, :search_analyzer, :preserve_separators, :preserve_position_increments, :max_input_length]
20
+ attr_reader *OPTIONS
21
+ def type
22
+ :completion
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a constant_keyword attribute for the model. This field type is a specialization of the keyword field, but it only accepts a single value.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true.
6
+ # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false.
7
+ # :fields - The Hash of multi-fields for the same string value to be indexed in multiple ways.
8
+ # :ignore_above - The Integer limit for the length of the string. Strings longer than this limit will not be indexed. Defaults to 2147483647.
9
+ # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true.
10
+ # :index_options - The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'.
11
+ # :meta - The Hash metadata about the field.
12
+ # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to false.
13
+ # :null_value - The String value to be substituted for any explicit null values. Defaults to null.
14
+ # :on_script_error - The String defining what to do if the script defined by the :script parameter throws an error at indexing time. Can be 'fail' or 'continue'.
15
+ # :script - The String script that will index values generated by this script, rather than reading the values directly from the source.
16
+ # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
17
+ # :similarity - The String scoring algorithm or similarity to be used. Defaults to 'BM25'.
18
+ # :normalizer - The String pre-processor for the keyword prior to indexing. Defaults to null.
19
+ # :split_queries_on_whitespace - The Boolean indicating if full text queries should split the input on whitespace. Defaults to false.
20
+ # :time_series_dimension - The Boolean indicating if the field is a time series dimension. Defaults to false.
21
+ # :value - The String value to associate with all documents in the index.
22
+ #
23
+ # Examples
24
+ #
25
+ # class MyModel
26
+ # include StretchyModel
27
+ # attribute :status, :constant_keyword, value: 'active'
28
+ # end
29
+ #
30
+ # Returns nothing.
31
+ class ConstantKeyword < Stretchy::Attributes::Type::Keyword
32
+ OPTIONS = OPTIONS + [:value]
33
+ attr_reader *OPTIONS
34
+ def type
35
+ :constant_keyword
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,85 @@
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a datetime attribute for the model.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion.
6
+ # This allows it to be used later for sorting, aggregations, or scripting. Defaults to true.
7
+ # :format - The String date format(s) that can be parsed. Defaults to 'strict_date_optional_time||epoch_millis'.
8
+ # :locale - The String locale to use when parsing dates. Defaults to the ROOT locale.
9
+ # :ignore_malformed - The Boolean indicating if malformed numbers should be ignored. Defaults to false.
10
+ # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true.
11
+ # :null_value - The Date value to be substituted for any explicit null values. Defaults to null.
12
+ # :on_script_error - The String defining what to do if the script defined by the :script parameter throws an error at indexing time. Can be 'fail' or 'continue'.
13
+ # :script - The String script that will index values generated by this script, rather than reading the values directly from the source.
14
+ # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
15
+ # :meta - The Hash metadata about the field.
16
+ #
17
+ # Examples
18
+ #
19
+ # class MyModel < Stretchy::Record
20
+ # attribute :created_at, :datetime, format: 'strict_date_optional_time||epoch_millis', locale: 'en'
21
+ # end
22
+ #
23
+ # Returns nothing.
24
+ class DateTime < Stretchy::Attributes::Type::Base
25
+ OPTIONS = [:doc_values, :format, :locale, :ignore_malformed, :index, :null_value, :on_script_error, :script, :store, :meta]
26
+ attr_reader *OPTIONS
27
+ include ActiveModel::Type::Helpers::Timezone
28
+ include ActiveModel::Type::Helpers::AcceptsMultiparameterTime.new(
29
+ defaults: { 4 => 0, 5 => 0 }
30
+ )
31
+ include ActiveModel::Type::Helpers::TimeValue
32
+
33
+ def initialize(**args)
34
+ @model_format = args.delete(:model_format)
35
+ super
36
+ end
37
+
38
+ def type
39
+ :datetime
40
+ end
41
+
42
+ def type_for_database
43
+ :date
44
+ end
45
+
46
+ private
47
+ def cast_value(value)
48
+ return apply_seconds_precision(value) unless value.is_a?(::String)
49
+ return if value.empty?
50
+
51
+ fast_string_to_time(value) || fallback_string_to_time(value) || custom_string_to_time(value)
52
+ end
53
+
54
+ # '0.123456' -> 123456
55
+ # '1.123456' -> 123456
56
+ def microseconds(time)
57
+ time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
58
+ end
59
+
60
+ def custom_string_to_time(string)
61
+ ::Date.strptime(string, @model_format)
62
+ end
63
+
64
+ def fallback_string_to_time(string)
65
+ time_hash = begin
66
+ ::Date._parse(string)
67
+ rescue ArgumentError => e
68
+ end
69
+ return unless time_hash
70
+
71
+ time_hash[:sec_fraction] = microseconds(time_hash)
72
+
73
+ new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset))
74
+ end
75
+
76
+ def value_from_multiparameter_assignment(values_hash)
77
+ missing_parameters = [1, 2, 3].delete_if { |key| values_hash.key?(key) }
78
+ unless missing_parameters.empty?
79
+ raise ArgumentError, "Provided hash #{values_hash} doesn't contain necessary keys: #{missing_parameters}"
80
+ end
81
+ super
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,59 @@
1
+ # Public: Defines a dense vector attribute for the model.
2
+ #
3
+ # name - The Symbol name of the attribute.
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :element_type - The String data type used to encode vectors. Can be 'float' or 'byte'.
6
+ # 'float' indexes a 4-byte floating-point value per dimension.
7
+ # 'byte' indexes a 1-byte integer value per dimension.
8
+ # Vectors using 'byte' require dimensions with integer values between -128 to 127.
9
+ # :dims - The Integer number of vector dimensions. Can’t exceed 4096.
10
+ # If not specified, it will be set to the length of the first vector added to the field.
11
+ # :index - The Boolean indicating if you can search this field using the kNN search API (default: true).
12
+ # :similarity - The String vector similarity metric to use in kNN search. Can be 'l2_norm', 'dot_product', 'cosine', or 'max_inner_product'.
13
+ # This parameter can only be specified when :index is true.
14
+ # :index_options - The Hash that configures the kNN indexing algorithm. Can only be specified when :index is true.
15
+ # :type - The String type of kNN algorithm to use. Can be either 'hnsw' or 'int8_hnsw'.
16
+ # :m - The Integer number of neighbors each node will be connected to in the HNSW graph.
17
+ # :ef_construction - The Integer number of candidates to track while assembling the list of nearest neighbors for each new node.
18
+ # :confidence_interval - The Float confidence interval to use when quantizing the vectors. Only applicable to 'int8_hnsw' index types.
19
+ #
20
+ # Examples
21
+ #
22
+ # class MyModel
23
+ # include StretchyModel
24
+ # attribute :vector_embeddings, :dense_vector, element_type: 'float', dims: 3
25
+ # end
26
+ #
27
+ # Returns nothing.
28
+ module Stretchy
29
+ module Attributes
30
+ module Type
31
+ class DenseVector < Stretchy::Attributes::Type::Base
32
+ OPTIONS = [:element_type, :dims, :index, :similarity, :index_options]
33
+ attr_reader *OPTIONS
34
+
35
+ def initialize(**args)
36
+ args.each do |k, v|
37
+ instance_variable_set("@#{k}", v) if OPTIONS.include?(k)
38
+ args.delete(k)
39
+ end
40
+ super
41
+ end
42
+
43
+ def type
44
+ :dense_vector
45
+ end
46
+
47
+ def mappings(name)
48
+ options = {type: type}
49
+ OPTIONS.each { |_| options[_] = self.send(_) }
50
+ {
51
+ properties: {
52
+ "#{name}.tokens": options
53
+ }
54
+ }.as_json
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,31 @@
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a flattened attribute for the model.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :depth_limit - The Integer maximum allowed depth of the flattened object field. Defaults to 20.
6
+ # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true.
7
+ # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false.
8
+ # :ignore_above - The Integer limit for the length of leaf values. Values longer than this limit will not be indexed. By default, there is no limit.
9
+ # :index - The Boolean indicating if the field should be searchable. Defaults to true.
10
+ # :index_options - The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'.
11
+ # :null_value - The String value to be substituted for any explicit null values. Defaults to null.
12
+ # :similarity - The String scoring algorithm or similarity to be used. Defaults to 'BM25'.
13
+ # :split_queries_on_whitespace - The Boolean indicating if full text queries should split the input on whitespace. Defaults to false.
14
+ # :time_series_dimensions - The Array of Strings indicating the fields inside the flattened object that are dimensions of the time series.
15
+ #
16
+ # Examples
17
+ #
18
+ # class MyModel
19
+ # include StretchyModel
20
+ # attribute :metadata, :flattened, depth_limit: 10, index_options: 'freqs'
21
+ # end
22
+ #
23
+ # Returns nothing.
24
+ class Flattened < Stretchy::Attributes::Type::Base
25
+ OPTIONS = [:depth_limit, :doc_values, :eager_global_ordinals, :ignore_above, :index, :index_options, :null_value, :similarity, :split_queries_on_whitespace, :time_series_dimensions]
26
+
27
+ def type
28
+ :flattened
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a geo_point attribute for the model.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :ignore_malformed - The Boolean indicating if malformed geopoints should be ignored. Defaults to false.
6
+ # :ignore_z_value - The Boolean indicating if three dimension points should be accepted but only latitude and longitude values should be indexed. Defaults to true.
7
+ # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true.
8
+ # :null_value - The GeoPoint value to be substituted for any explicit null values. Defaults to null.
9
+ # :on_script_error - The String defining what to do if the script defined by the :script parameter throws an error at indexing time. Can be 'fail' or 'continue'.
10
+ # :script - The String script that will index values generated by this script, rather than reading the values directly from the source.
11
+ #
12
+ # Examples
13
+ #
14
+ # class MyModel
15
+ # include StretchyModel
16
+ # attribute :location, :geo_point, ignore_malformed: true
17
+ # end
18
+ #
19
+ # Returns nothing.
20
+ class GeoPoint < Stretchy::Attributes::Type::Base
21
+ OPTIONS = [:ignore_malformed, :ignore_z_value, :index, :null_value, :on_script_error, :script]
22
+
23
+ def type
24
+ :geo_point
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a geo_shape attribute for the model.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :orientation - The String default orientation for the field’s WKT polygons. Can be 'right', 'counterclockwise', 'ccw', 'left', 'clockwise', or 'cw'. Defaults to 'right'.
6
+ # :ignore_malformed - The Boolean indicating if malformed GeoJSON or WKT shapes should be ignored. Defaults to false.
7
+ # :ignore_z_value - The Boolean indicating if three dimension points should be accepted but only latitude and longitude values should be indexed. Defaults to true.
8
+ # :coerce - The Boolean indicating if unclosed linear rings in polygons should be automatically closed. Defaults to false.
9
+ # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true.
10
+ # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true.
11
+ #
12
+ # Examples
13
+ #
14
+ # class MyModel
15
+ # include StretchyModel
16
+ # attribute :boundary, :geo_shape, orientation: 'left', coerce: true
17
+ # end
18
+ #
19
+ # Returns nothing.
20
+ class GeoShape < Stretchy::Attributes::Type::Base
21
+ OPTIONS = [:orientation, :ignore_malformed, :ignore_z_value, :coerce, :index, :doc_values]
22
+
23
+ def type
24
+ :geo_shape
25
+ end
26
+ end
27
+ end
@@ -1,17 +1,40 @@
1
- module Stretchy
2
- module Attributes
3
- module Type
4
- class Hash < ActiveModel::Type::Value # :nodoc:
5
- def type
6
- :hash
7
- end
1
+ module Stretchy::Attributes::Type
2
+ # Public: Defines a hash attribute for the model.
3
+ #
4
+ # opts - The Hash options used to refine the attribute (default: {}):
5
+ # :dynamic - The String indicating if new properties should be added dynamically to an existing object. Can be 'true', 'runtime', 'false', or 'strict'. Defaults to 'true'.
6
+ # :enabled - The Boolean indicating if the JSON value for the object field should be parsed and indexed. Defaults to true.
7
+ # :subobjects - The Boolean indicating if the object can hold subobjects. Defaults to true.
8
+ # :properties - The Hash of fields within the object, which can be of any data type, including object.
9
+ #
10
+ # Examples
11
+ #
12
+ # class MyModel < Stretchy::Record
13
+ # attribute :metadata, :hash, dynamic: 'strict', subobjects: false
14
+ # end
15
+ #
16
+ # Returns nothing.
17
+ class Hash < Stretchy::Attributes::Type::Base
18
+ OPTIONS = [:dynamic, :enabled, :subobjects, :properties]
8
19
 
9
- private
20
+ def type
21
+ :hash
22
+ end
23
+
24
+ def type_for_database
25
+ :object
26
+ end
27
+
28
+ def mappings(name)
29
+ options = {}
30
+ OPTIONS.each { |option| options[option] = send(option) unless send(option).nil? }
31
+ { name => options }.as_json
32
+ end
33
+
34
+ private
10
35
 
11
- def cast_value(value)
12
- Elasticsearch::Model::HashWrapper[value]
13
- end
14
- end
15
- end
36
+ def cast_value(value)
37
+ Elasticsearch::Model::HashWrapper[value]
16
38
  end
39
+ end
17
40
  end
@@ -0,0 +1,7 @@
1
+ module Stretchy::Attributes::Type
2
+ class Histogram < Stretchy::Attributes::Type::Base
3
+ def type
4
+ :histogram
5
+ end
6
+ end
7
+ end