stretchy-model 0.6.0 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -1
  3. data/README.md +28 -10
  4. data/Rakefile +56 -0
  5. data/containers/Dockerfile.opensearch +4 -3
  6. data/docker-compose.yml +32 -19
  7. data/docs/.nojekyll +0 -0
  8. data/docs/README.md +147 -0
  9. data/docs/_coverpage.md +14 -0
  10. data/docs/_sidebar.md +14 -0
  11. data/docs/examples/_sidebar.md +15 -0
  12. data/docs/examples/data_analysis.md +216 -0
  13. data/docs/examples/semantic_search_with_llm.md +83 -0
  14. data/docs/examples/simple-ingest-pipeline.md +326 -0
  15. data/docs/guides/_sidebar.md +14 -0
  16. data/docs/guides/aggregations.md +142 -0
  17. data/docs/guides/machine-learning.md +154 -0
  18. data/docs/guides/models.md +372 -0
  19. data/docs/guides/pipelines.md +151 -0
  20. data/docs/guides/querying.md +361 -0
  21. data/docs/guides/quick-start.md +72 -0
  22. data/docs/guides/scopes.md +125 -0
  23. data/docs/index.html +113 -0
  24. data/docs/stretchy.cover.png +0 -0
  25. data/docs/stretchy.logo.png +0 -0
  26. data/docs/styles.css +90 -0
  27. data/lib/elasticsearch/api/actions/machine_learning/models/delete_model.rb +33 -0
  28. data/lib/elasticsearch/api/actions/machine_learning/models/deploy.rb +31 -0
  29. data/lib/elasticsearch/api/actions/machine_learning/models/get_model.rb +43 -0
  30. data/lib/elasticsearch/api/actions/machine_learning/models/get_status.rb +31 -0
  31. data/lib/elasticsearch/api/actions/machine_learning/models/params_registry.rb +45 -0
  32. data/lib/elasticsearch/api/actions/machine_learning/models/register.rb +45 -0
  33. data/lib/elasticsearch/api/actions/machine_learning/models/undeploy.rb +32 -0
  34. data/lib/elasticsearch/api/actions/machine_learning/models/update_model.rb +39 -0
  35. data/lib/elasticsearch/api/namespace/machine_learning/model.rb +27 -0
  36. data/lib/opensearch/api/actions/machine_learning/models/delete_model.rb +33 -0
  37. data/lib/opensearch/api/actions/machine_learning/models/deploy.rb +31 -0
  38. data/lib/opensearch/api/actions/machine_learning/models/get_model.rb +44 -0
  39. data/lib/opensearch/api/actions/machine_learning/models/get_status.rb +31 -0
  40. data/lib/opensearch/api/actions/machine_learning/models/params_registry.rb +45 -0
  41. data/lib/opensearch/api/actions/machine_learning/models/register.rb +45 -0
  42. data/lib/opensearch/api/actions/machine_learning/models/undeploy.rb +31 -0
  43. data/lib/opensearch/api/actions/machine_learning/models/update_model.rb +39 -0
  44. data/lib/opensearch/api/namespace/machine_learning/model.rb +27 -0
  45. data/lib/stretchy/attributes/transformers/keyword_transformer.rb +41 -35
  46. data/lib/stretchy/attributes/type/array.rb +24 -1
  47. data/lib/stretchy/attributes/type/base.rb +6 -2
  48. data/lib/stretchy/attributes/type/binary.rb +24 -17
  49. data/lib/stretchy/attributes/type/boolean.rb +29 -22
  50. data/lib/stretchy/attributes/type/completion.rb +18 -10
  51. data/lib/stretchy/attributes/type/constant_keyword.rb +35 -26
  52. data/lib/stretchy/attributes/type/date_time.rb +81 -20
  53. data/lib/stretchy/attributes/type/dense_vector.rb +46 -49
  54. data/lib/stretchy/attributes/type/flattened.rb +28 -19
  55. data/lib/stretchy/attributes/type/geo_point.rb +21 -12
  56. data/lib/stretchy/attributes/type/geo_shape.rb +21 -12
  57. data/lib/stretchy/attributes/type/hash.rb +24 -10
  58. data/lib/stretchy/attributes/type/histogram.rb +25 -0
  59. data/lib/stretchy/attributes/type/ip.rb +26 -17
  60. data/lib/stretchy/attributes/type/join.rb +16 -7
  61. data/lib/stretchy/attributes/type/keyword.rb +21 -26
  62. data/lib/stretchy/attributes/type/knn_vector.rb +47 -0
  63. data/lib/stretchy/attributes/type/match_only_text.rb +22 -1
  64. data/lib/stretchy/attributes/type/nested.rb +16 -11
  65. data/lib/stretchy/attributes/type/numeric/base.rb +30 -22
  66. data/lib/stretchy/attributes/type/numeric/byte.rb +20 -0
  67. data/lib/stretchy/attributes/type/numeric/double.rb +20 -0
  68. data/lib/stretchy/attributes/type/numeric/float.rb +20 -0
  69. data/lib/stretchy/attributes/type/numeric/half_float.rb +20 -0
  70. data/lib/stretchy/attributes/type/numeric/integer.rb +21 -1
  71. data/lib/stretchy/attributes/type/numeric/long.rb +20 -0
  72. data/lib/stretchy/attributes/type/numeric/scaled_float.rb +16 -7
  73. data/lib/stretchy/attributes/type/numeric/short.rb +20 -0
  74. data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +21 -1
  75. data/lib/stretchy/attributes/type/percolator.rb +16 -4
  76. data/lib/stretchy/attributes/type/point.rb +19 -9
  77. data/lib/stretchy/attributes/type/range/base.rb +24 -1
  78. data/lib/stretchy/attributes/type/range/date_range.rb +21 -5
  79. data/lib/stretchy/attributes/type/range/double_range.rb +20 -4
  80. data/lib/stretchy/attributes/type/range/float_range.rb +21 -5
  81. data/lib/stretchy/attributes/type/range/integer_range.rb +20 -4
  82. data/lib/stretchy/attributes/type/range/ip_range.rb +20 -4
  83. data/lib/stretchy/attributes/type/range/long_range.rb +20 -4
  84. data/lib/stretchy/attributes/type/rank_feature.rb +16 -6
  85. data/lib/stretchy/attributes/type/rank_features.rb +27 -10
  86. data/lib/stretchy/attributes/type/search_as_you_type.rb +28 -18
  87. data/lib/stretchy/attributes/type/shape.rb +19 -9
  88. data/lib/stretchy/attributes/type/sparse_vector.rb +25 -21
  89. data/lib/stretchy/attributes/type/string.rb +42 -1
  90. data/lib/stretchy/attributes/type/text.rb +53 -28
  91. data/lib/stretchy/attributes/type/token_count.rb +21 -11
  92. data/lib/stretchy/attributes/type/version.rb +16 -6
  93. data/lib/stretchy/attributes/type/wildcard.rb +36 -25
  94. data/lib/stretchy/attributes.rb +30 -0
  95. data/lib/stretchy/delegation/gateway_delegation.rb +86 -2
  96. data/lib/stretchy/index_setting.rb +94 -0
  97. data/lib/stretchy/indexing/bulk.rb +75 -3
  98. data/lib/stretchy/machine_learning/model.rb +192 -0
  99. data/lib/stretchy/model/callbacks.rb +1 -0
  100. data/lib/stretchy/model/common.rb +157 -0
  101. data/lib/stretchy/model/persistence.rb +144 -0
  102. data/lib/stretchy/model/refreshable.rb +26 -0
  103. data/lib/stretchy/open_search_compatibility.rb +4 -0
  104. data/lib/stretchy/pipeline.rb +124 -0
  105. data/lib/stretchy/pipelines/processor.rb +57 -0
  106. data/lib/stretchy/querying.rb +7 -7
  107. data/lib/stretchy/rails/instrumentation/publishers.rb +31 -0
  108. data/lib/{rails → stretchy/rails}/instrumentation/railtie.rb +11 -6
  109. data/lib/stretchy/record.rb +5 -4
  110. data/lib/stretchy/relation.rb +230 -28
  111. data/lib/stretchy/relations/aggregation_methods/aggregation.rb +59 -0
  112. data/lib/stretchy/relations/aggregation_methods/avg.rb +45 -0
  113. data/lib/stretchy/relations/aggregation_methods/bucket_script.rb +47 -0
  114. data/lib/stretchy/relations/aggregation_methods/bucket_selector.rb +47 -0
  115. data/lib/stretchy/relations/aggregation_methods/bucket_sort.rb +47 -0
  116. data/lib/stretchy/relations/aggregation_methods/cardinality.rb +47 -0
  117. data/lib/stretchy/relations/aggregation_methods/children.rb +47 -0
  118. data/lib/stretchy/relations/aggregation_methods/composite.rb +41 -0
  119. data/lib/stretchy/relations/aggregation_methods/date_histogram.rb +53 -0
  120. data/lib/stretchy/relations/aggregation_methods/date_range.rb +53 -0
  121. data/lib/stretchy/relations/aggregation_methods/extended_stats.rb +48 -0
  122. data/lib/stretchy/relations/aggregation_methods/filter.rb +47 -0
  123. data/lib/stretchy/relations/aggregation_methods/filters.rb +47 -0
  124. data/lib/stretchy/relations/aggregation_methods/geo_bounds.rb +40 -0
  125. data/lib/stretchy/relations/aggregation_methods/geo_centroid.rb +40 -0
  126. data/lib/stretchy/relations/aggregation_methods/global.rb +39 -0
  127. data/lib/stretchy/relations/aggregation_methods/histogram.rb +43 -0
  128. data/lib/stretchy/relations/aggregation_methods/ip_range.rb +41 -0
  129. data/lib/stretchy/relations/aggregation_methods/max.rb +40 -0
  130. data/lib/stretchy/relations/aggregation_methods/min.rb +41 -0
  131. data/lib/stretchy/relations/aggregation_methods/missing.rb +40 -0
  132. data/lib/stretchy/relations/aggregation_methods/nested.rb +40 -0
  133. data/lib/stretchy/relations/aggregation_methods/percentile_ranks.rb +45 -0
  134. data/lib/stretchy/relations/aggregation_methods/percentiles.rb +45 -0
  135. data/lib/stretchy/relations/aggregation_methods/range.rb +42 -0
  136. data/lib/stretchy/relations/aggregation_methods/reverse_nested.rb +40 -0
  137. data/lib/stretchy/relations/aggregation_methods/sampler.rb +40 -0
  138. data/lib/stretchy/relations/aggregation_methods/scripted_metric.rb +43 -0
  139. data/lib/stretchy/relations/aggregation_methods/significant_terms.rb +45 -0
  140. data/lib/stretchy/relations/aggregation_methods/stats.rb +42 -0
  141. data/lib/stretchy/relations/aggregation_methods/sum.rb +42 -0
  142. data/lib/stretchy/relations/aggregation_methods/terms.rb +46 -0
  143. data/lib/stretchy/relations/aggregation_methods/top_hits.rb +42 -0
  144. data/lib/stretchy/relations/aggregation_methods/top_metrics.rb +44 -0
  145. data/lib/stretchy/relations/aggregation_methods/value_count.rb +41 -0
  146. data/lib/stretchy/relations/aggregation_methods/weighted_avg.rb +42 -0
  147. data/lib/stretchy/relations/aggregation_methods.rb +20 -749
  148. data/lib/stretchy/relations/finder_methods.rb +2 -18
  149. data/lib/stretchy/relations/null_relation.rb +55 -0
  150. data/lib/stretchy/relations/query_builder.rb +139 -23
  151. data/lib/stretchy/relations/query_methods/bind.rb +19 -0
  152. data/lib/stretchy/relations/query_methods/extending.rb +29 -0
  153. data/lib/stretchy/relations/query_methods/fields.rb +70 -0
  154. data/lib/stretchy/relations/query_methods/filter_query.rb +53 -0
  155. data/lib/stretchy/relations/query_methods/has_field.rb +40 -0
  156. data/lib/stretchy/relations/query_methods/highlight.rb +75 -0
  157. data/lib/stretchy/relations/query_methods/hybrid.rb +60 -0
  158. data/lib/stretchy/relations/query_methods/ids.rb +40 -0
  159. data/lib/stretchy/relations/query_methods/match.rb +52 -0
  160. data/lib/stretchy/relations/query_methods/must_not.rb +54 -0
  161. data/lib/stretchy/relations/query_methods/neural.rb +58 -0
  162. data/lib/stretchy/relations/query_methods/neural_sparse.rb +43 -0
  163. data/lib/stretchy/relations/query_methods/none.rb +21 -0
  164. data/lib/stretchy/relations/query_methods/or_filter.rb +21 -0
  165. data/lib/stretchy/relations/query_methods/order.rb +63 -0
  166. data/lib/stretchy/relations/query_methods/query_string.rb +44 -0
  167. data/lib/stretchy/relations/query_methods/regexp.rb +61 -0
  168. data/lib/stretchy/relations/query_methods/should.rb +51 -0
  169. data/lib/stretchy/relations/query_methods/size.rb +44 -0
  170. data/lib/stretchy/relations/query_methods/skip_callbacks.rb +47 -0
  171. data/lib/stretchy/relations/query_methods/source.rb +59 -0
  172. data/lib/stretchy/relations/query_methods/where.rb +113 -0
  173. data/lib/stretchy/relations/query_methods.rb +51 -540
  174. data/lib/stretchy/relations/scoping/default.rb +136 -0
  175. data/lib/stretchy/relations/scoping/named.rb +70 -0
  176. data/lib/stretchy/relations/scoping/scope_registry.rb +36 -0
  177. data/lib/stretchy/relations/scoping.rb +30 -0
  178. data/lib/stretchy/relations/search_option_methods.rb +2 -0
  179. data/lib/stretchy/shared_scopes.rb +6 -1
  180. data/lib/stretchy/version.rb +1 -1
  181. data/lib/stretchy.rb +23 -11
  182. metadata +147 -18
  183. data/lib/rails/instrumentation/publishers.rb +0 -29
  184. data/lib/stretchy/common.rb +0 -33
  185. data/lib/stretchy/null_relation.rb +0 -53
  186. data/lib/stretchy/persistence.rb +0 -43
  187. data/lib/stretchy/refreshable.rb +0 -15
  188. data/lib/stretchy/scoping/default.rb +0 -134
  189. data/lib/stretchy/scoping/named.rb +0 -68
  190. data/lib/stretchy/scoping/scope_registry.rb +0 -34
  191. data/lib/stretchy/scoping.rb +0 -28
@@ -1,24 +1,41 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a rank_features attribute for the model.
2
+ # The RankFeatures attribute type
3
3
  #
4
- # A rank_features field can index numeric feature vectors, so that they can later be used to boost documents in queries with a rank_feature query.
5
-
6
- # It is analogous to the rank_feature data type but is better suited when the list of features is sparse so that it wouldn’t be reasonable to add one field to the mappings for each of them.
7
- # opts - The Hash options used to refine the attribute (default: {}):
8
- # :positive_score_impact - The Boolean indicating if features correlate positively with the score. If set to false, the score decreases with the value of the feature instead of increasing. Defaults to true.
4
+ # This class is used to define a rank_features attribute for a model. It provides support for the Elasticsearch rank_features data type, which is a type of data type that can index numeric feature vectors, so that they can later be used to boost documents in queries with a rank_feature query. It is better suited when the list of features is sparse so that it wouldn’t be reasonable to add one field to the mappings for each of them.
5
+ #
6
+ # ### Parameters
7
+ #
8
+ # - `type:` `:rank_features`.
9
+ # - `options:` The Hash of options for the attribute.
10
+ # - `:positive_score_impact:` The Boolean indicating if features correlate positively with the score. If set to false, the score decreases with the value of the feature instead of increasing. Defaults to true.
11
+ #
12
+ # ---
13
+ #
14
+ # ### Examples
9
15
  #
10
- # Examples
16
+ # #### Define a rank_features attribute
11
17
  #
12
- # class MyModel < Stretchy::Record
18
+ # ```ruby
19
+ # class MyModel < StretchyModel
13
20
  # attribute :negative_reviews, :rank_features, positive_score_impact: false
14
21
  # end
22
+ # ```
15
23
  #
16
- # Returns nothing.
17
- class RankFeatures < Stretchy::Attributes::Type::Base
24
+ class RankFeatures < Stretchy::Attributes::Type::Hash
18
25
  OPTIONS = [:positive_score_impact]
19
26
 
27
+ def mappings(name)
28
+ options = {type: type}
29
+ OPTIONS.each { |option| options[option] = send(option) unless send(option).nil? }
30
+ { name => options }.as_json
31
+ end
32
+
20
33
  def type
21
34
  :rank_features
22
35
  end
36
+
37
+ def type_for_database
38
+ :rank_features
39
+ end
23
40
  end
24
41
  end
@@ -1,25 +1,35 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a search_as_you_type attribute for the model. This field type is optimized to provide out-of-the-box support for queries that serve an as-you-type completion use case.
3
- #
4
- # opts - The Hash options used to refine the attribute (default: {}):
5
- # :max_shingle_size - The Integer indicating the largest shingle size to create. Valid values are 2 to 4. Defaults to 3.
6
- # :analyzer - The String analyzer to be used for text fields, both at index-time and at search-time. Defaults to the default index analyzer, or the standard analyzer.
7
- # :index - The Boolean indicating if the field should be searchable. Defaults to true.
8
- # :index_options - The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'.
9
- # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true.
10
- # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
11
- # :search_analyzer - The String analyzer that should be used at search time on text fields. Defaults to the analyzer setting.
12
- # :search_quote_analyzer - The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting.
13
- # :similarity - The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'.
14
- # :term_vector - The String indicating if term vectors should be stored for the field. Defaults to 'no'.
15
- #
16
- # Examples
17
- #
18
- # class MyModel < Stretchy::Record
2
+ # The SearchAsYouType attribute type
3
+ #
4
+ # This class is used to define a search_as_you_type attribute for a model. This field type is optimized to provide out-of-the-box support for queries that serve an as-you-type completion use case.
5
+ #
6
+ # ### Parameters
7
+ #
8
+ # - `type:` `:search_as_you_type`.
9
+ # - `options:` The Hash of options for the attribute.
10
+ # - `:max_shingle_size:` The Integer indicating the largest shingle size to create. Valid values are 2 to 4. Defaults to 3.
11
+ # - `:analyzer:` The String analyzer to be used for text fields, both at index-time and at search-time. Defaults to the default index analyzer, or the standard analyzer.
12
+ # - `:index:` The Boolean indicating if the field should be searchable. Defaults to true.
13
+ # - `:index_options:` The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'.
14
+ # - `:norms:` The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true.
15
+ # - `:store:` The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
16
+ # - `:search_analyzer:` The String analyzer that should be used at search time on text fields. Defaults to the analyzer setting.
17
+ # - `:search_quote_analyzer:` The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting.
18
+ # - `:similarity:` The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'.
19
+ # - `:term_vector:` The String indicating if term vectors should be stored for the field. Defaults to 'no'.
20
+ #
21
+ # ---
22
+ #
23
+ # ### Examples
24
+ #
25
+ # #### Define a search_as_you_type attribute
26
+ #
27
+ # ```ruby
28
+ # class MyModel < StretchyModel
19
29
  # attribute :name, :search_as_you_type, max_shingle_size: 4
20
30
  # end
31
+ # ```
21
32
  #
22
- # Returns nothing.
23
33
  class SearchAsYouType < Stretchy::Attributes::Type::Base
24
34
  OPTIONS = [:max_shingle_size, :analyzer, :index, :index_options, :norms, :store, :search_analyzer, :search_quote_analyzer, :similarity, :term_vector]
25
35
 
@@ -1,19 +1,29 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a shape attribute for the model. This field type is used for complex shapes.
2
+ # The Shape attribute type
3
3
  #
4
- # opts - The Hash options used to refine the attribute (default: {}):
5
- # :orientation - The String indicating how to interpret vertex order for polygons / multipolygons. Can be 'right', 'ccw', 'counterclockwise', 'left', 'cw', 'clockwise'. Defaults to 'ccw'.
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 the z value of three dimension points should be ignored. Defaults to true.
8
- # :coerce - The Boolean indicating if unclosed linear rings in polygons will be automatically closed. Defaults to false.
4
+ # This class is used to define a shape attribute for a model. It provides support for the Elasticsearch shape data type, which is a type of data type that can hold complex shapes represented as GeoJSON or WKT.
9
5
  #
10
- # Examples
6
+ # ### Parameters
11
7
  #
12
- # class MyModel < Stretchy::Record
8
+ # - `type:` `:shape`.
9
+ # - `options:` The Hash of options for the attribute.
10
+ # - `:orientation:` The String indicating how to interpret vertex order for polygons / multipolygons. Can be 'right', 'ccw', 'counterclockwise', 'left', 'cw', 'clockwise'. Defaults to 'ccw'.
11
+ # - `:ignore_malformed:` The Boolean indicating if malformed GeoJSON or WKT shapes should be ignored. Defaults to false.
12
+ # - `:ignore_z_value:` The Boolean indicating if the z value of three dimension points should be ignored. Defaults to true.
13
+ # - `:coerce:` The Boolean indicating if unclosed linear rings in polygons will be automatically closed. Defaults to false.
14
+ #
15
+ # ---
16
+ #
17
+ # ### Examples
18
+ #
19
+ # #### Define a shape attribute
20
+ #
21
+ # ```ruby
22
+ # class MyModel < StretchyModel
13
23
  # attribute :boundary, :shape, orientation: 'cw'
14
24
  # end
25
+ # ```
15
26
  #
16
- # Returns nothing.
17
27
  class Shape < Stretchy::Attributes::Type::Base
18
28
  OPTIONS = [:orientation, :ignore_malformed, :ignore_z_value, :coerce]
19
29
 
@@ -1,21 +1,27 @@
1
- #
2
- # attribute :ml, :sparse_vector
3
- #
4
- # {
5
- # "mappings": {
6
- # "properties": {
7
- # "ml.tokens": {
8
- # "type": "sparse_vector"
9
- # }
10
- # }
11
- # }
12
- # }
13
- #
14
-
15
-
16
1
  module Stretchy
17
2
  module Attributes
18
3
  module Type
4
+ # The SparseVector attribute type
5
+ #
6
+ # This class is used to define a sparse_vector attribute for a model. It provides support for the Elasticsearch sparse_vector data type, which is a type of data type that can hold sparse vectors of float values.
7
+ #
8
+ # ### Parameters
9
+ #
10
+ # - `type:` `:sparse_vector`.
11
+ # - `options:` The Hash of options for the attribute. This type does not have any specific options.
12
+ #
13
+ # ---
14
+ #
15
+ # ### Examples
16
+ #
17
+ # #### Define a sparse_vector attribute
18
+ #
19
+ # ```ruby
20
+ # class MyModel < StretchyModel
21
+ # attribute :ml, :sparse_vector
22
+ # end
23
+ # ```
24
+ #
19
25
  class SparseVector < Stretchy::Attributes::Type::Base
20
26
 
21
27
  def type
@@ -23,12 +29,10 @@ module Stretchy
23
29
  end
24
30
 
25
31
  def mappings(name)
26
- {
27
- properties: {
28
- "#{name}.tokens": {
29
- type: "sparse_vector"
30
- }
31
- }
32
+ {
33
+ "#{name}.tokens": {
34
+ type: "sparse_vector"
35
+ }
32
36
  }.as_json
33
37
  end
34
38
  end
@@ -1,5 +1,46 @@
1
1
  module Stretchy::Attributes::Type
2
- class String < Stretchy::Attributes::Type::Text # :nodoc:
2
+ # The String attribute type
3
+ #
4
+ # _alias for `:text`_
5
+ #
6
+ # This class is used to define a string attribute for a model. It provides support for the Elasticsearch text data type, which is a type of data type that can hold text. In this library, the string type is an alias for the text type.
7
+ #
8
+ # ### Parameters
9
+ #
10
+ # - `type:` `:string`.
11
+ # - `options:` The Hash of options for the attribute.
12
+ # - `:analyzer:` The String analyzer to be used for the text field, both at index-time and at search-time. Defaults to the default index analyzer, or the standard analyzer.
13
+ # - `:eager_global_ordinals:` The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false.
14
+ # - `:fielddata:` The Boolean indicating if the field can use in-memory fielddata for sorting, aggregations, or scripting. Defaults to false.
15
+ # - `:fielddata_frequency_filter:` The Hash of expert settings which allow to decide which values to load in memory when fielddata is enabled.
16
+ # - `:fields:` The Hash of multi-fields allow the same string value to be indexed in multiple ways for different purposes. By default, a 'keyword' field is added. Set to false to disable.
17
+ # - `:index:` The Boolean indicating if the field should be searchable. Defaults to true.
18
+ # - `:index_options:` The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'.
19
+ # - `:index_prefixes:` The Hash indicating if term prefixes of between 2 and 5 characters are indexed into a separate field.
20
+ # - `:index_phrases:` The Boolean indicating if two-term word combinations (shingles) are indexed into a separate field. Defaults to false.
21
+ # - `:norms:` The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true.
22
+ # - `:position_increment_gap:` The Integer indicating the number of fake term position which should be inserted between each element of an array of strings. Defaults to 100.
23
+ # - `:store:` The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
24
+ # - `:search_analyzer:` The String analyzer that should be used at search time on the text field. Defaults to the analyzer setting.
25
+ # - `:search_quote_analyzer:` The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting.
26
+ # - `:similarity:` The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'.
27
+ # - `:term_vector:` The String indicating if term vectors should be stored for the field. Defaults to 'no'.
28
+ # - `:meta:` The Hash of metadata about the field.
29
+ #
30
+ # ---
31
+ #
32
+ # ### Examples
33
+ #
34
+ # #### Define a string attribute
35
+ #
36
+ # ```ruby
37
+ # class MyModel < StretchyModel
38
+ # attribute :name, :string
39
+ # end
40
+ # ```
41
+ #
42
+ class String < Stretchy::Attributes::Type::Text
43
+
3
44
  def type
4
45
  :string
5
46
  end
@@ -1,37 +1,57 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a text attribute for the model. This field type is used for text strings.
3
- #
4
- # opts - The Hash options used to refine the attribute (default: {}):
5
- # :analyzer - The String analyzer to be used for the text field, both at index-time and at search-time. Defaults to the default index analyzer, or the standard analyzer.
6
- # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false.
7
- # :fielddata - The Boolean indicating if the field can use in-memory fielddata for sorting, aggregations, or scripting. Defaults to false.
8
- # :fielddata_frequency_filter - The Hash of expert settings which allow to decide which values to load in memory when fielddata is enabled.
9
- # :fields - The Hash of multi-fields allow the same string value to be indexed in multiple ways for different purposes. By default, a 'keyword' field is added. Set to false to disable.
10
- # :index - The Boolean indicating if the field should be searchable. Defaults to true.
11
- # :index_options - The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'.
12
- # :index_prefixes - The Hash indicating if term prefixes of between 2 and 5 characters are indexed into a separate field.
13
- # :index_phrases - The Boolean indicating if two-term word combinations (shingles) are indexed into a separate field. Defaults to false.
14
- # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true.
15
- # :position_increment_gap - The Integer indicating the number of fake term position which should be inserted between each element of an array of strings. Defaults to 100.
16
- # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
17
- # :search_analyzer - The String analyzer that should be used at search time on the text field. Defaults to the analyzer setting.
18
- # :search_quote_analyzer - The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting.
19
- # :similarity - The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'.
20
- # :term_vector - The String indicating if term vectors should be stored for the field. Defaults to 'no'.
21
- # :meta - The Hash of metadata about the field.
22
- #
23
- #
24
- # Examples
25
- #
26
- # class MyModel
27
- # include StretchyModel
2
+ # The Text attribute type
3
+ #
4
+ # This class is used to define a text attribute for a model. It provides support for the Elasticsearch text data type, which is a type of data type that can hold text strings.
5
+ #
6
+ # >[!NOTE]
7
+ # >
8
+ # > The default for the `:text` type is to have a keyword multified if `field:` is not specified and `fields:` is not explicitly false.
9
+ # > This can be disabled by setting `Stretchy.configuration.add_keyword_field_to_text_attributes` to false.
10
+ # > The default keyword field name is `:keyword`, but this can be changed by setting `Stretchy.configuration.default_keyword_field`.
11
+ #
12
+ # ### Parameters
13
+ #
14
+ # - `type:` `:text`.
15
+ # - `options:` The Hash of options for the attribute.
16
+ # - `:analyzer:` The String analyzer to be used for the text field, both at index-time and at search-time. Defaults to the default index analyzer, or the standard analyzer.
17
+ # - `:eager_global_ordinals:` The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false.
18
+ # - `:fielddata:` The Boolean indicating if the field can use in-memory fielddata for sorting, aggregations, or scripting. Defaults to false.
19
+ # - `:fielddata_frequency_filter:` The Hash of expert settings which allow to decide which values to load in memory when fielddata is enabled.
20
+ # - `:fields:` The Hash of multi-fields allow the same string value to be indexed in multiple ways for different purposes. By default, a 'keyword' field is added. Set to false to disable.
21
+ # - `:index:` The Boolean indicating if the field should be searchable. Defaults to true.
22
+ # - `:index_options:` The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'.
23
+ # - `:index_prefixes:` The Hash indicating if term prefixes of between 2 and 5 characters are indexed into a separate field.
24
+ # - `:index_phrases:` The Boolean indicating if two-term word combinations (shingles) are indexed into a separate field. Defaults to false.
25
+ # - `:norms:` The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true.
26
+ # - `:position_increment_gap:` The Integer indicating the number of fake term position which should be inserted between each element of an array of strings. Defaults to 100.
27
+ # - `:store:` The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
28
+ # - `:search_analyzer:` The String analyzer that should be used at search time on the text field. Defaults to the analyzer setting.
29
+ # - `:search_quote_analyzer:` The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting.
30
+ # - `:similarity:` The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'.
31
+ # - `:term_vector:` The String indicating if term vectors should be stored for the field. Defaults to 'no'.
32
+ # - `:meta:` The Hash of metadata about the field.
33
+ #
34
+ # ---
35
+ #
36
+ # ### Examples
37
+ #
38
+ # #### Define a text attribute
39
+ #
40
+ # ```ruby
41
+ # class MyModel < StretchyModel
28
42
  # attribute :description, :text, analyzer: 'english'
29
43
  # end
44
+ # ```
30
45
  #
31
- # Returns nothing.
32
46
  class Text < Stretchy::Attributes::Type::Base
33
47
  OPTIONS = [:analyzer, :eager_global_ordinals, :fielddata, :fielddata_frequency_filter, :fields, :index, :index_options, :index_prefixes, :index_phrases, :norms, :position_increment_gap, :store, :search_analyzer, :search_quote_analyzer, :similarity, :term_vector, :meta]
34
-
48
+
49
+ def initialize(**args)
50
+ # Add a keyword field by default if no fields are specified
51
+ args.reverse_merge!(fields: {keyword: {type: :keyword, ignore_above: 256}}) if args[:fields].nil? && Stretchy.configuration.add_keyword_field_to_text_attributes
52
+ super
53
+ end
54
+
35
55
  def type
36
56
  :text
37
57
  end
@@ -40,6 +60,11 @@ module Stretchy::Attributes::Type
40
60
  :text
41
61
  end
42
62
 
63
+ # The default for the `:text` type is to have a keyword field if no fields are specified.
64
+ def keyword_field?
65
+ fields.find { |k,d| d[:type].to_sym == :keyword}.present?
66
+ end
67
+
43
68
  def mappings(name)
44
69
  options = {type: type_for_database}
45
70
  OPTIONS.each { |option| options[option] = send(option) unless send(option).nil? }
@@ -1,21 +1,31 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a token_count attribute for the model. This field type is used for counting the number of tokens in a string.
2
+ # The TokenCount attribute type
3
3
  #
4
- # opts - The Hash options used to refine the attribute (default: {}):
5
- # :analyzer - The String analyzer to be used to analyze the string value. Required.
6
- # :enable_position_increments - The Boolean indicating if position increments should be counted. Defaults to true.
7
- # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true.
8
- # :index - The Boolean indicating if the field should be searchable. Defaults to true.
9
- # :null_value - The Numeric value to be substituted for any explicit null values. Defaults to null.
10
- # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
4
+ # This class is used to define a token_count attribute for a model. It provides support for the Elasticsearch token_count data type, which is a type of data type that can count the number of tokens in a string.
11
5
  #
12
- # Examples
6
+ # ### Parameters
13
7
  #
14
- # class MyModel < Stretchy::Record
8
+ # - `type:` `:token_count`.
9
+ # - `options:` The Hash of options for the attribute.
10
+ # - `:analyzer:` The String analyzer to be used to analyze the string value. Required.
11
+ # - `:enable_position_increments:` The Boolean indicating if position increments should be counted. Defaults to true.
12
+ # - `:doc_values:` The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true.
13
+ # - `:index:` The Boolean indicating if the field should be searchable. Defaults to true.
14
+ # - `:null_value:` The Numeric value to be substituted for any explicit null values. Defaults to null.
15
+ # - `:store:` The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
16
+ #
17
+ # ---
18
+ #
19
+ # ### Examples
20
+ #
21
+ # #### Define a token_count attribute
22
+ #
23
+ # ```ruby
24
+ # class MyModel < StretchyModel
15
25
  # attribute :description_token_count, :token_count, analyzer: 'standard'
16
26
  # end
27
+ # ```
17
28
  #
18
- # Returns nothing.
19
29
  class TokenCount < Stretchy::Attributes::Type::Base
20
30
  OPTIONS = [:analyzer, :enable_position_increments, :doc_values, :index, :null_value, :store]
21
31
 
@@ -1,16 +1,26 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a version attribute for the model. This field type is used for software versions following the Semantic Versioning rules.
2
+ # The Version attribute type
3
3
  #
4
- # opts - The Hash options used to refine the attribute (default: {}):
5
- # :meta - The Hash of metadata about the field.
4
+ # This class is used to define a version attribute for a model. This field type is used for software versions following the Semantic Versioning rules.
6
5
  #
7
- # Examples
6
+ # ### Parameters
8
7
  #
9
- # class MyModel < Stretchy::Record
8
+ # - `type:` `:version`.
9
+ # - `options:` The Hash of options for the attribute.
10
+ # - `:meta:` The Hash of metadata about the field.
11
+ #
12
+ # ---
13
+ #
14
+ # ### Examples
15
+ #
16
+ # #### Define a version attribute
17
+ #
18
+ # ```ruby
19
+ # class MyModel < StretchyModel
10
20
  # attribute :software_version, :version
11
21
  # end
22
+ # ```
12
23
  #
13
- # Returns nothing.
14
24
  class Version < Stretchy::Attributes::Type::Base
15
25
  OPTIONS = [:meta]
16
26
 
@@ -1,33 +1,44 @@
1
1
  module Stretchy::Attributes::Type
2
- # Public: Defines a wildcard attribute for the model. This field type is a specialization of the keyword field, but it supports wildcard searches.
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
- #
22
- # Examples
23
- #
24
- # class MyModel
25
- # include StretchyModel
2
+ # The Wildcard attribute type
3
+ #
4
+ # This class is used to define a wildcard attribute for a model. This field type is a specialization of the keyword field, but it supports wildcard searches.
5
+ #
6
+ # ### Parameters
7
+ #
8
+ # - `type:` `:wildcard`.
9
+ # - `options:` The Hash of options for the attribute.
10
+ # - `:doc_values:` The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true.
11
+ # - `:eager_global_ordinals:` The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false.
12
+ # - `:fields:` The Hash of multi-fields for the same string value to be indexed in multiple ways.
13
+ # - `:ignore_above:` The Integer limit for the length of the string. Strings longer than this limit will not be indexed. Defaults to 2147483647.
14
+ # - `:index:` The Boolean indicating if the field should be quickly searchable. Defaults to true.
15
+ # - `:index_options:` The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'.
16
+ # - `:meta:` The Hash metadata about the field.
17
+ # - `:norms:` The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to false.
18
+ # - `:null_value:` The String value to be substituted for any explicit null values. Defaults to null.
19
+ # - `: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'.
20
+ # - `:script:` The String script that will index values generated by this script, rather than reading the values directly from the source.
21
+ # - `:store:` The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false.
22
+ # - `:similarity:` The String scoring algorithm or similarity to be used. Defaults to 'BM25'.
23
+ # - `:normalizer:` The String pre-processor for the keyword prior to indexing. Defaults to null.
24
+ # - `:split_queries_on_whitespace:` The Boolean indicating if full text queries should split the input on whitespace. Defaults to false.
25
+ # - `:time_series_dimension:` The Boolean indicating if the field is a time series dimension. Defaults to false.
26
+ #
27
+ # ---
28
+ #
29
+ # ### Examples
30
+ #
31
+ # #### Define a wildcard attribute
32
+ #
33
+ # ```ruby
34
+ # class MyModel < StretchyModel
26
35
  # attribute :description_wildcard, :wildcard
27
36
  # end
37
+ # ```
28
38
  #
29
- # Returns nothing.
30
39
  class Wildcard < Stretchy::Attributes::Type::Keyword
40
+ OPTIONS = [:doc_values, :eager_global_ordinals, :fields, :ignore_above, :index, :index_options, :meta, :norms, :null_value, :on_script_error, :script, :store, :similarity, :normalizer, :split_queries_on_whitespace, :time_series_dimension]
41
+ attr_reader *OPTIONS
31
42
  def type
32
43
  :wildcard
33
44
  end
@@ -1,4 +1,32 @@
1
1
  module Stretchy
2
+ # used to define and manage the attributes of a model in Stretchy. It provides methods for getting and setting attribute values, inspecting the model, and registering attribute types.
3
+ #
4
+ # ### Methods
5
+ #
6
+ # - `[](attribute)`: Retrieves the value of the specified attribute.
7
+ # - `[]=(attribute, value)`: Sets the value of the specified attribute.
8
+ # - `inspect`: Returns a string representation of the model, including its class name and attributes.
9
+ # - `self.inspect`: Returns a string representation of the model class, including its name and attribute types.
10
+ # - `attribute_mappings`: Returns a JSON representation of the attribute mappings for the model.
11
+ # - `self.register!`: Registers the attribute types with ActiveModel.
12
+ #
13
+ # ### Example
14
+ #
15
+ # In this example, the `Attributes` module is used to define an attribute for `MyModel`, get and set the attribute value, inspect the model and the model class, and get the attribute mappings.
16
+ #
17
+ # ```ruby
18
+ # class MyModel < Stretchy::Record
19
+ # attribute :title, :string
20
+ # end
21
+ #
22
+ # model = MyModel.new(title: "hello")
23
+ # model[:title] # => "hello"
24
+ # model.inspect # => "#<MyModel title: hello>"
25
+ # MyModel.inspect # => "#<MyModel title: string>"
26
+ # MyModel.attribute_mappings # => {properties: {title: {type: "string"}}}
27
+ # ```
28
+ #
29
+ #
2
30
  module Attributes
3
31
  extend ActiveSupport::Concern
4
32
 
@@ -38,11 +66,13 @@ module Stretchy
38
66
  ActiveModel::Type.register(:ip, Stretchy::Attributes::Type::IP)
39
67
  ActiveModel::Type.register(:join, Stretchy::Attributes::Type::Join)
40
68
  ActiveModel::Type.register(:keyword, Stretchy::Attributes::Type::Keyword)
69
+ ActiveModel::Type.register(:knn_vector, Stretchy::Attributes::Type::KnnVector)
41
70
  ActiveModel::Type.register(:match_only_text, Stretchy::Attributes::Type::MatchOnlyText)
42
71
  ActiveModel::Type.register(:nested, Stretchy::Attributes::Type::Nested)
43
72
  ActiveModel::Type.register(:percolator, Stretchy::Attributes::Type::Percolator)
44
73
  ActiveModel::Type.register(:point, Stretchy::Attributes::Type::Point)
45
74
  ActiveModel::Type.register(:rank_feature, Stretchy::Attributes::Type::RankFeature)
75
+ ActiveModel::Type.register(:rank_features, Stretchy::Attributes::Type::RankFeatures)
46
76
 
47
77
  ActiveModel::Type.register(:text, Stretchy::Attributes::Type::Text)
48
78
  ActiveModel::Type.register(:token_count, Stretchy::Attributes::Type::TokenCount)