diff --git a/lib/stretchy.rb b/lib/stretchy.rb index ce4d6e0..9dd8943 100644 --- a/lib/stretchy.rb +++ b/lib/stretchy.rb @@ -12,7 +12,6 @@ require_relative "rails/instrumentation/railtie" if defined?(Rails) - module Stretchy def self.loader @@ -21,6 +20,7 @@ def self.loader loader.tag = File.basename(__FILE__, ".rb") loader.inflector = Zeitwerk::GemInflector.new(__FILE__) loader.push_dir(__dir__) + loader.inflector.inflect("ip" => "IP") loader end end diff --git a/lib/stretchy/attributes.rb b/lib/stretchy/attributes.rb index 37cb28e..757ab51 100644 --- a/lib/stretchy/attributes.rb +++ b/lib/stretchy/attributes.rb @@ -18,15 +18,59 @@ def inspect def inspect "#<#{self.name} #{attribute_types.map { |k,v| "#{k}: #{v.type}" }.join(', ')}>" end + + def attribute_mappings + {properties: attribute_types.map { |k,v| v.mappings(k) }.reduce({}, :merge)}.as_json + end end def self.register! ActiveModel::Type.register(:array, Stretchy::Attributes::Type::Array) + ActiveModel::Type.register(:binary, Stretchy::Attributes::Type::Binary) + ActiveModel::Type.register(:boolean, Stretchy::Attributes::Type::Boolean) + ActiveModel::Type.register(:constant_keyword, Stretchy::Attributes::Type::ConstantKeyword) + ActiveModel::Type.register(:datetime, Stretchy::Attributes::Type::DateTime) + ActiveModel::Type.register(:flattened, Stretchy::Attributes::Type::Flattened) + ActiveModel::Type.register(:geo_point, Stretchy::Attributes::Type::GeoPoint) + ActiveModel::Type.register(:geo_shape, Stretchy::Attributes::Type::GeoShape) + ActiveModel::Type.register(:histogram, Stretchy::Attributes::Type::Histogram) ActiveModel::Type.register(:hash, Stretchy::Attributes::Type::Hash) + ActiveModel::Type.register(:ip, Stretchy::Attributes::Type::IP) + ActiveModel::Type.register(:join, Stretchy::Attributes::Type::Join) ActiveModel::Type.register(:keyword, Stretchy::Attributes::Type::Keyword) + ActiveModel::Type.register(:match_only_text, Stretchy::Attributes::Type::MatchOnlyText) + ActiveModel::Type.register(:nested, Stretchy::Attributes::Type::Nested) + ActiveModel::Type.register(:percolator, Stretchy::Attributes::Type::Percolator) + ActiveModel::Type.register(:point, Stretchy::Attributes::Type::Point) + ActiveModel::Type.register(:rank_feature, Stretchy::Attributes::Type::RankFeature) + ActiveModel::Type.register(:text, Stretchy::Attributes::Type::Text) + ActiveModel::Type.register(:token_count, Stretchy::Attributes::Type::TokenCount) ActiveModel::Type.register(:dense_vector, Stretchy::Attributes::Type::DenseVector) + + ActiveModel::Type.register(:search_as_you_type, Stretchy::Attributes::Type::SearchAsYouType) ActiveModel::Type.register(:sparse_vector, Stretchy::Attributes::Type::SparseVector) + ActiveModel::Type.register(:string, Stretchy::Attributes::Type::String) + ActiveModel::Type.register(:version, Stretchy::Attributes::Type::Version) + ActiveModel::Type.register(:wildcard, Stretchy::Attributes::Type::Wildcard) + # Numerics + ActiveModel::Type.register(:long, Stretchy::Attributes::Type::Numeric::Long) + ActiveModel::Type.register(:integer, Stretchy::Attributes::Type::Numeric::Integer) + ActiveModel::Type.register(:short, Stretchy::Attributes::Type::Numeric::Short) + ActiveModel::Type.register(:byte, Stretchy::Attributes::Type::Numeric::Byte) + ActiveModel::Type.register(:double, Stretchy::Attributes::Type::Numeric::Double) + ActiveModel::Type.register(:float, Stretchy::Attributes::Type::Numeric::Float) + ActiveModel::Type.register(:half_float, Stretchy::Attributes::Type::Numeric::HalfFloat) + ActiveModel::Type.register(:scaled_float, Stretchy::Attributes::Type::Numeric::ScaledFloat) + ActiveModel::Type.register(:unsigned_long, Stretchy::Attributes::Type::Numeric::UnsignedLong) + # Ranges + ActiveModel::Type.register(:integer_range, Stretchy::Attributes::Type::Range::IntegerRange) + ActiveModel::Type.register(:float_range, Stretchy::Attributes::Type::Range::FloatRange) + ActiveModel::Type.register(:long_range, Stretchy::Attributes::Type::Range::LongRange) + ActiveModel::Type.register(:double_range, Stretchy::Attributes::Type::Range::DoubleRange) + ActiveModel::Type.register(:date_range, Stretchy::Attributes::Type::Range::DateRange) + ActiveModel::Type.register(:ip_range, Stretchy::Attributes::Type::Range::IpRange) + end end end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/array.rb b/lib/stretchy/attributes/type/array.rb index f7ae786..9dd39c8 100644 --- a/lib/stretchy/attributes/type/array.rb +++ b/lib/stretchy/attributes/type/array.rb @@ -1,15 +1,7 @@ -module Stretchy - module Attributes - module Type - - class Array < ActiveModel::Type::Value # :nodoc: - - def type - :array - end - - end - - end +module Stretchy::Attributes::Type + class Array < Stretchy::Attributes::Type::Base # :nodoc: + def type + :array end + end end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/base.rb b/lib/stretchy/attributes/type/base.rb new file mode 100644 index 0000000..bebcfc6 --- /dev/null +++ b/lib/stretchy/attributes/type/base.rb @@ -0,0 +1,38 @@ +module Stretchy + module Attributes + module Type + class Base < ActiveModel::Type::Value + + OPTIONS = [] + + + def initialize(**args) + + define_option_methods! + + args.each do |k, v| + if self.class::OPTIONS.include?(k) + instance_variable_set("@#{k}", v) + args.delete(k) + end + end + super + end + + def mappings(name) + options = {type: type} + self.class::OPTIONS.each { |option| options[option] = send(option) unless send(option).nil? } + { name => options }.as_json + end + + private + + def define_option_methods! + self.class::OPTIONS.each do |option| + define_singleton_method(option.to_sym) { instance_variable_get("@#{option}") } + end + end + end + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/binary.rb b/lib/stretchy/attributes/type/binary.rb new file mode 100644 index 0000000..3c76734 --- /dev/null +++ b/lib/stretchy/attributes/type/binary.rb @@ -0,0 +1,45 @@ +# Public: Defines a binary attribute for the model. +# +# name - The Symbol name of the attribute. +# opts - The Hash options used to refine the attribute (default: {}): +# :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. +# This allows it to be used later for sorting, aggregations, or scripting. Defaults to false. +# :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. +# +# Examples +# +# class MyModel +# include StretchyModel +# attribute :name, :binary, doc_values: true, store: true +# end +# +# Returns nothing. +module Stretchy + module Attributes + module Type + class Binary < ActiveModel::Type::Value + OPTIONS = [:doc_values, :store] + attr_reader *OPTIONS + + def initialize(**args) + args.each do |k, v| + instance_variable_set("@#{k}", v) if OPTIONS.include?(k) + args.delete(k) + end + super + end + + def type + :binary + end + + + def mappings(name) + options = {type: type} + OPTIONS.each { |_| options[_] = self.send(_) } + { name => options }.as_json + end + end + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/boolean.rb b/lib/stretchy/attributes/type/boolean.rb new file mode 100644 index 0000000..28a0a61 --- /dev/null +++ b/lib/stretchy/attributes/type/boolean.rb @@ -0,0 +1,48 @@ +# Public: Defines a boolean attribute for the model. +# +# name - The Symbol name of the attribute. +# opts - The Hash options used to refine the attribute (default: {}): +# :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. +# This allows it to be used later for sorting, aggregations, or scripting. Defaults to true. +# :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. +# :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. +# :null_value - The Boolean value to be substituted for any explicit null values. Defaults to null. +# :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'. +# :script - The String script that will index values generated by this script, rather than reading the values directly from the source. +# :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. +# :meta - The Hash metadata about the field. +# +# Examples +# +# class MyModel +# include StretchyModel +# attribute :name, :boolean, doc_values: true, store: true +# end +# +# Returns nothing. +module Stretchy + module Attributes + module Type + class Boolean < Stretchy::Attributes::Type::Base + + OPTIONS = [:doc_values, :index, :ignore_malformed, :null_value, :on_script_error, :script, :store, :meta] + attr_reader *OPTIONS + + def initialize(**args) + args.each do |k, v| + instance_variable_set("@#{k}", v) if OPTIONS.include?(k) + args.delete(k) + end + super + end + + def type + :boolean + end + + + + end + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/completion.rb b/lib/stretchy/attributes/type/completion.rb new file mode 100644 index 0000000..1814e14 --- /dev/null +++ b/lib/stretchy/attributes/type/completion.rb @@ -0,0 +1,25 @@ +module Stretchy::Attributes::Type + # Public: Defines a completion attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :analyzer - The String index analyzer to use. Defaults to 'simple'. + # :search_analyzer - The String search analyzer to use. Defaults to the value of :analyzer. + # :preserve_separators - The Boolean indicating if separators should be preserved. Defaults to true. + # :preserve_position_increments - The Boolean indicating if position increments should be enabled. Defaults to true. + # :max_input_length - The Integer limit for the length of a single input. Defaults to 50. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :name, :completion, analyzer: 'simple', max_input_length: 100 + # end + # + # Returns nothing. + class Completion < Stretchy::Attributes::Type::Base + OPTIONS = [:analyzer, :search_analyzer, :preserve_separators, :preserve_position_increments, :max_input_length] + attr_reader *OPTIONS + def type + :completion + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/constant_keyword.rb b/lib/stretchy/attributes/type/constant_keyword.rb new file mode 100644 index 0000000..0ec5c48 --- /dev/null +++ b/lib/stretchy/attributes/type/constant_keyword.rb @@ -0,0 +1,38 @@ +module Stretchy::Attributes::Type + # 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. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false. + # :fields - The Hash of multi-fields for the same string value to be indexed in multiple ways. + # :ignore_above - The Integer limit for the length of the string. Strings longer than this limit will not be indexed. Defaults to 2147483647. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :index_options - The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'. + # :meta - The Hash metadata about the field. + # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to false. + # :null_value - The String value to be substituted for any explicit null values. Defaults to null. + # :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'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :similarity - The String scoring algorithm or similarity to be used. Defaults to 'BM25'. + # :normalizer - The String pre-processor for the keyword prior to indexing. Defaults to null. + # :split_queries_on_whitespace - The Boolean indicating if full text queries should split the input on whitespace. Defaults to false. + # :time_series_dimension - The Boolean indicating if the field is a time series dimension. Defaults to false. + # :value - The String value to associate with all documents in the index. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :status, :constant_keyword, value: 'active' + # end + # + # Returns nothing. + class ConstantKeyword < Stretchy::Attributes::Type::Keyword + OPTIONS = OPTIONS + [:value] + attr_reader *OPTIONS + def type + :constant_keyword + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/date_time.rb b/lib/stretchy/attributes/type/date_time.rb new file mode 100644 index 0000000..4c843e1 --- /dev/null +++ b/lib/stretchy/attributes/type/date_time.rb @@ -0,0 +1,31 @@ +module Stretchy::Attributes::Type + # Public: Defines a datetime attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. + # This allows it to be used later for sorting, aggregations, or scripting. Defaults to true. + # :format - The String date format(s) that can be parsed. Defaults to 'strict_date_optional_time||epoch_millis'. + # :locale - The String locale to use when parsing dates. Defaults to the ROOT locale. + # :ignore_malformed - The Boolean indicating if malformed numbers should be ignored. Defaults to false. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :null_value - The Date value to be substituted for any explicit null values. Defaults to null. + # :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'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :meta - The Hash metadata about the field. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :created_at, :datetime, format: 'strict_date_optional_time||epoch_millis', locale: 'en' + # end + # + # Returns nothing. + class DateTime < Stretchy::Attributes::Type::Base + OPTIONS = [:doc_values, :format, :locale, :ignore_malformed, :index, :null_value, :on_script_error, :script, :store, :meta] + attr_reader *OPTIONS + def type + :datetime + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/dense_vector.rb b/lib/stretchy/attributes/type/dense_vector.rb index ae290e2..8afb33b 100644 --- a/lib/stretchy/attributes/type/dense_vector.rb +++ b/lib/stretchy/attributes/type/dense_vector.rb @@ -28,7 +28,7 @@ module Stretchy module Attributes module Type - class DenseVector < ActiveModel::Type::Value# :nodoc: + class DenseVector < Stretchy::Attributes::Type::Base OPTIONS = [:element_type, :dims, :index, :similarity, :index_options] attr_reader *OPTIONS @@ -52,7 +52,7 @@ def mappings(name) "#{name}.tokens": options } }.as_json - end + end end end end diff --git a/lib/stretchy/attributes/type/flattened.rb b/lib/stretchy/attributes/type/flattened.rb new file mode 100644 index 0000000..e4ac419 --- /dev/null +++ b/lib/stretchy/attributes/type/flattened.rb @@ -0,0 +1,31 @@ +module Stretchy::Attributes::Type + # Public: Defines a flattened attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :depth_limit - The Integer maximum allowed depth of the flattened object field. Defaults to 20. + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false. + # :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. + # :index - The Boolean indicating if the field should be searchable. Defaults to true. + # :index_options - The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'. + # :null_value - The String value to be substituted for any explicit null values. Defaults to null. + # :similarity - The String scoring algorithm or similarity to be used. Defaults to 'BM25'. + # :split_queries_on_whitespace - The Boolean indicating if full text queries should split the input on whitespace. Defaults to false. + # :time_series_dimensions - The Array of Strings indicating the fields inside the flattened object that are dimensions of the time series. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :metadata, :flattened, depth_limit: 10, index_options: 'freqs' + # end + # + # Returns nothing. + class Flattened < Stretchy::Attributes::Type::Base + OPTIONS = [:depth_limit, :doc_values, :eager_global_ordinals, :ignore_above, :index, :index_options, :null_value, :similarity, :split_queries_on_whitespace, :time_series_dimensions] + + def type + :flattened + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/geo_point.rb b/lib/stretchy/attributes/type/geo_point.rb new file mode 100644 index 0000000..3e2e66d --- /dev/null +++ b/lib/stretchy/attributes/type/geo_point.rb @@ -0,0 +1,27 @@ +module Stretchy::Attributes::Type + # Public: Defines a geo_point attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :ignore_malformed - The Boolean indicating if malformed geopoints should be ignored. Defaults to false. + # :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. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :null_value - The GeoPoint value to be substituted for any explicit null values. Defaults to null. + # :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'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :location, :geo_point, ignore_malformed: true + # end + # + # Returns nothing. + class GeoPoint < Stretchy::Attributes::Type::Base + OPTIONS = [:ignore_malformed, :ignore_z_value, :index, :null_value, :on_script_error, :script] + + def type + :geo_point + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/geo_shape.rb b/lib/stretchy/attributes/type/geo_shape.rb new file mode 100644 index 0000000..14d6a58 --- /dev/null +++ b/lib/stretchy/attributes/type/geo_shape.rb @@ -0,0 +1,27 @@ +module Stretchy::Attributes::Type + # Public: Defines a geo_shape attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :orientation - The String default orientation for the field’s WKT polygons. Can be 'right', 'counterclockwise', 'ccw', 'left', 'clockwise', or 'cw'. Defaults to 'right'. + # :ignore_malformed - The Boolean indicating if malformed GeoJSON or WKT shapes should be ignored. Defaults to false. + # :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. + # :coerce - The Boolean indicating if unclosed linear rings in polygons should be automatically closed. Defaults to false. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :boundary, :geo_shape, orientation: 'left', coerce: true + # end + # + # Returns nothing. + class GeoShape < Stretchy::Attributes::Type::Base + OPTIONS = [:orientation, :ignore_malformed, :ignore_z_value, :coerce, :index, :doc_values] + + def type + :geo_shape + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/hash.rb b/lib/stretchy/attributes/type/hash.rb index fb4cf49..b772c63 100644 --- a/lib/stretchy/attributes/type/hash.rb +++ b/lib/stretchy/attributes/type/hash.rb @@ -1,17 +1,30 @@ -module Stretchy - module Attributes - module Type - class Hash < ActiveModel::Type::Value # :nodoc: - def type - :hash - end +module Stretchy::Attributes::Type + # Public: Defines a hash attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :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'. + # :enabled - The Boolean indicating if the JSON value for the object field should be parsed and indexed. Defaults to true. + # :subobjects - The Boolean indicating if the object can hold subobjects. Defaults to true. + # :properties - The Hash of fields within the object, which can be of any data type, including object. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :metadata, :hash, dynamic: 'strict', subobjects: false + # end + # + # Returns nothing. + class Hash < Stretchy::Attributes::Type::Base + OPTIONS = [:dynamic, :enabled, :subobjects, :properties] - private + def type + :hash + end + + private - def cast_value(value) - Elasticsearch::Model::HashWrapper[value] - end - end - end + def cast_value(value) + Elasticsearch::Model::HashWrapper[value] end + end end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/histogram.rb b/lib/stretchy/attributes/type/histogram.rb new file mode 100644 index 0000000..6b85fbc --- /dev/null +++ b/lib/stretchy/attributes/type/histogram.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type + class Histogram < Stretchy::Attributes::Type::Base + def type + :histogram + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/ip.rb b/lib/stretchy/attributes/type/ip.rb new file mode 100644 index 0000000..1515c3e --- /dev/null +++ b/lib/stretchy/attributes/type/ip.rb @@ -0,0 +1,29 @@ +module Stretchy::Attributes::Type + # Public: Defines an IP attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :ignore_malformed - The Boolean indicating if malformed IP addresses should be ignored. Defaults to false. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :null_value - The String IPv4 or IPv6 value to be substituted for any explicit null values. Defaults to null. + # :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 'reject' or 'ignore'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :time_series_dimension - The Boolean indicating if the field is a time series dimension. Defaults to false. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :ip_address, :ip, ignore_malformed: true, time_series_dimension: true + # end + # + # Returns nothing. + class IP < Stretchy::Attributes::Type::Base + OPTIONS = [:doc_values, :ignore_malformed, :index, :null_value, :on_script_error, :script, :store, :time_series_dimension] + + def type + :ip + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/join.rb b/lib/stretchy/attributes/type/join.rb new file mode 100644 index 0000000..17fb0d0 --- /dev/null +++ b/lib/stretchy/attributes/type/join.rb @@ -0,0 +1,22 @@ +module Stretchy::Attributes::Type + # Public: Defines a join attribute for the model. + # + # opts - The Hash options used to define the parent/child relation within documents of the same index (default: {}): + # :relations - The Hash defining a set of possible relations within the documents, each relation being a parent name and a child name. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :relation, :join, relations: { question: :answer } + # end + # + # Returns nothing. + class Join < Stretchy::Attributes::Type::Base + OPTIONS = [:relations] + + def type + :join + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/keyword.rb b/lib/stretchy/attributes/type/keyword.rb index 4f5f8ab..b1fa0a9 100644 --- a/lib/stretchy/attributes/type/keyword.rb +++ b/lib/stretchy/attributes/type/keyword.rb @@ -1,11 +1,37 @@ -module Stretchy - module Attributes - module Type - class Keyword < ActiveModel::Type::String # :nodoc: - def type - :keyword - end - end - end +module Stretchy::Attributes::Type + # Public: Defines a keyword attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false. + # :fields - The Hash of multi-fields for the same string value to be indexed in multiple ways. + # :ignore_above - The Integer limit for the length of the string. Strings longer than this limit will not be indexed. Defaults to 2147483647. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :index_options - The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'. + # :meta - The Hash metadata about the field. + # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to false. + # :null_value - The String value to be substituted for any explicit null values. Defaults to null. + # :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'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :similarity - The String scoring algorithm or similarity to be used. Defaults to 'BM25'. + # :normalizer - The String pre-processor for the keyword prior to indexing. Defaults to null. + # :split_queries_on_whitespace - The Boolean indicating if full text queries should split the input on whitespace. Defaults to false. + # :time_series_dimension - The Boolean indicating if the field is a time series dimension. Defaults to false. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :tag, :keyword, ignore_above: 256, time_series_dimension: true + # end + # + # Returns nothing. + class Keyword < Stretchy::Attributes::Type::Base + 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] + + def type + :keyword + end end -end \ No newline at end of file + end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/match_only_text.rb b/lib/stretchy/attributes/type/match_only_text.rb new file mode 100644 index 0000000..c27e782 --- /dev/null +++ b/lib/stretchy/attributes/type/match_only_text.rb @@ -0,0 +1,8 @@ +# a space-optimized variant of text that disables scoring and performs slower on queries that need positions. It is best suited for indexing log messages. +module Stretchy::Attributes::Type + class MatchOnlyText < Stretchy::Attributes::Type::Text + def type + :match_only_text + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/nested.rb b/lib/stretchy/attributes/type/nested.rb new file mode 100644 index 0000000..cebdba0 --- /dev/null +++ b/lib/stretchy/attributes/type/nested.rb @@ -0,0 +1,25 @@ +module Stretchy::Attributes::Type + # Public: Defines a nested attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :dynamic - The String indicating if new properties should be added dynamically to an existing nested object. Can be 'true', 'false', or 'strict'. Defaults to 'true'. + # :properties - The Hash of fields within the nested object, which can be of any data type, including nested. + # :include_in_parent - The Boolean indicating if all fields in the nested object are also added to the parent document as standard fields. Defaults to false. + # :include_in_root - The Boolean indicating if all fields in the nested object are also added to the root document as standard fields. Defaults to false. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :metadata, :nested, dynamic: 'strict', include_in_parent: true + # end + # + # Returns nothing. + class Nested < Stretchy::Attributes::Type::Base + OPTIONS = [:dynamic, :properties, :include_in_parent, :include_in_root] + + def type + :nested + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/base.rb b/lib/stretchy/attributes/type/numeric/base.rb new file mode 100644 index 0000000..caf6e10 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/base.rb @@ -0,0 +1,32 @@ +module Stretchy::Attributes::Type::Numeric + # Public: Defines a numeric attribute for the model. This is a base class for numeric types. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :coerce - The Boolean indicating if strings should be converted to numbers and fractions truncated for integers. Defaults to true. + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :ignore_malformed - The Boolean indicating if malformed numbers should be ignored. Defaults to false. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :meta - The Hash metadata about the field. + # :null_value - The Numeric value to be substituted for any explicit null values. Defaults to null. + # :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'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :time_series_dimension - The Boolean indicating if the field is a time series dimension. Defaults to false. + # :time_series_metric - The String indicating if the field is a time series metric. Can be 'counter', 'gauge', or 'null'. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :age, :integer, coerce: false, time_series_dimension: true + # end + # + # Returns nothing. + class Base < Stretchy::Attributes::Type::Base + OPTIONS = [:coerce, :doc_values, :ignore_malformed, :index, :meta, :null_value, :on_script_error, :script, :store, :time_series_dimension, :time_series_metric] + + def type + raise NotImplementedError, "You must use one of the numeric types: integer, long, short, byte, double, float, half_float, scaled_float." + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/byte.rb b/lib/stretchy/attributes/type/numeric/byte.rb new file mode 100644 index 0000000..da95186 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/byte.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class Byte < Base + def type + :byte + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/double.rb b/lib/stretchy/attributes/type/numeric/double.rb new file mode 100644 index 0000000..73dc901 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/double.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class Double < Base + def type + :double + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/float.rb b/lib/stretchy/attributes/type/numeric/float.rb new file mode 100644 index 0000000..c82aa0d --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/float.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class Float < Base + def type + :float + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/half_float.rb b/lib/stretchy/attributes/type/numeric/half_float.rb new file mode 100644 index 0000000..7f262fe --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/half_float.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class HalfFloat < Base + def type + :half_float + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/integer.rb b/lib/stretchy/attributes/type/numeric/integer.rb new file mode 100644 index 0000000..1f34c92 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/integer.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class Integer < Stretchy::Attributes::Type::Base # :nodoc: + def type + :integer + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/long.rb b/lib/stretchy/attributes/type/numeric/long.rb new file mode 100644 index 0000000..0ad6782 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/long.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class Long < Base + def type + :long + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/scaled_float.rb b/lib/stretchy/attributes/type/numeric/scaled_float.rb new file mode 100644 index 0000000..dafd079 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/scaled_float.rb @@ -0,0 +1,23 @@ +module Stretchy::Attributes::Type::Numeric + + # Public: Defines a scaled_float attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :scaling_factor - The Integer scaling factor to use when encoding values. This parameter is required. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :rating, :scaled_float, scaling_factor: 10 + # end + # + # Returns nothing. + class ScaledFloat < Base + OPTIONS = Base::OPTIONS + [:scaling_factor] + + def type + :scaled_float + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/short.rb b/lib/stretchy/attributes/type/numeric/short.rb new file mode 100644 index 0000000..df5c427 --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/short.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class Short < Base + def type + :short + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/numeric/unsigned_long.rb b/lib/stretchy/attributes/type/numeric/unsigned_long.rb new file mode 100644 index 0000000..44368bd --- /dev/null +++ b/lib/stretchy/attributes/type/numeric/unsigned_long.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type::Numeric + class UnsignedLong < Stretchy::Attributes::Type::Numeric::Base + def type + :unsigned_long + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/percolator.rb b/lib/stretchy/attributes/type/percolator.rb new file mode 100644 index 0000000..32c144f --- /dev/null +++ b/lib/stretchy/attributes/type/percolator.rb @@ -0,0 +1,23 @@ +module Stretchy::Attributes::Type + # Public: Defines a percolator attribute for the model. + # + # The percolator field type parses a JSON structure into a native query and stores that query, + # so that the percolate query can use it to match provided documents. + # + # Any field that contains a JSON object can be configured to be a percolator field. + # The percolator field type has no settings. Just configuring the percolator field type + # is sufficient to instruct Elasticsearch to treat a field as a query. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :query, :percolator + # end + # + # Returns nothing. + class Percolator < Stretchy::Attributes::Type::Base + def type + :percolator + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/point.rb b/lib/stretchy/attributes/type/point.rb new file mode 100644 index 0000000..1b09c54 --- /dev/null +++ b/lib/stretchy/attributes/type/point.rb @@ -0,0 +1,24 @@ +module Stretchy::Attributes::Type + # Public: Defines a point attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :ignore_malformed - The Boolean indicating if malformed points should be ignored. Defaults to false. + # :ignore_z_value - The Boolean indicating if the z value of three dimension points should be ignored. Defaults to true. + # :null_value - The Point value to be substituted for any explicit null values. Defaults to null. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :location, :point, ignore_malformed: true + # end + # + # Returns nothing. + class Point < Stretchy::Attributes::Type::Base + OPTIONS = [:ignore_malformed, :ignore_z_value, :null_value] + + def type + :point + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/range/base.rb b/lib/stretchy/attributes/type/range/base.rb new file mode 100644 index 0000000..cbd03c6 --- /dev/null +++ b/lib/stretchy/attributes/type/range/base.rb @@ -0,0 +1,9 @@ +module Stretchy::Attributes::Type::Range + class Base < Stretchy::Attributes::Type::Base + OPTIONS = [:coerce, :index, :store] + + def type + raise NotImplementedError, "You must use one of the range types: integer_range, float_range, long_range, double_range, date_range, or ip_range." + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/range/date_range.rb b/lib/stretchy/attributes/type/range/date_range.rb new file mode 100644 index 0000000..eab80f7 --- /dev/null +++ b/lib/stretchy/attributes/type/range/date_range.rb @@ -0,0 +1,17 @@ +module Stretchy::Attributes::Type::Range + # Public: Defines a date_range attribute for the model. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :birth_date_range, :date_range + # end + # + # Returns nothing. + class DateRange < Base + + def type + :date_range + end + end +end diff --git a/lib/stretchy/attributes/type/range/double_range.rb b/lib/stretchy/attributes/type/range/double_range.rb new file mode 100644 index 0000000..2dfa7b4 --- /dev/null +++ b/lib/stretchy/attributes/type/range/double_range.rb @@ -0,0 +1,17 @@ +module Stretchy::Attributes::Type::Range + # Public: Defines a double_range attribute for the model. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :weight_range, :double_range + # end + # + # Returns nothing. + class DoubleRange < Base + + def type + :double_range + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/range/float_range.rb b/lib/stretchy/attributes/type/range/float_range.rb new file mode 100644 index 0000000..6119395 --- /dev/null +++ b/lib/stretchy/attributes/type/range/float_range.rb @@ -0,0 +1,16 @@ +module Stretchy::Attributes::Type::Range + # Public: Defines a float_range attribute for the model. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :rating_range, :float_range + # end + # + # Returns nothing. + class FloatRange < Base + def type + :float_range + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/range/integer_range.rb b/lib/stretchy/attributes/type/range/integer_range.rb new file mode 100644 index 0000000..c283f65 --- /dev/null +++ b/lib/stretchy/attributes/type/range/integer_range.rb @@ -0,0 +1,16 @@ +module Stretchy::Attributes::Type::Range + # Public: Defines an integer_range attribute for the model. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :age_range, :integer_range + # end + # + # Returns nothing. + class IntegerRange < Base + def type + :integer_range + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/range/ip_range.rb b/lib/stretchy/attributes/type/range/ip_range.rb new file mode 100644 index 0000000..60fe717 --- /dev/null +++ b/lib/stretchy/attributes/type/range/ip_range.rb @@ -0,0 +1,16 @@ +module Stretchy::Attributes::Type::Range + # Public: Defines an ip_range attribute for the model. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :ip_range, :ip_range + # end + # + # Returns nothing. + class IpRange < Base + def type + :ip_range + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/range/long_range.rb b/lib/stretchy/attributes/type/range/long_range.rb new file mode 100644 index 0000000..4275cb4 --- /dev/null +++ b/lib/stretchy/attributes/type/range/long_range.rb @@ -0,0 +1,16 @@ +module Stretchy::Attributes::Type::Range + # Public: Defines a long_range attribute for the model. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :population_range, :long_range + # end + # + # Returns nothing. + class LongRange < Base + def type + :long_range + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/rank_feature.rb b/lib/stretchy/attributes/type/rank_feature.rb new file mode 100644 index 0000000..29d2bc0 --- /dev/null +++ b/lib/stretchy/attributes/type/rank_feature.rb @@ -0,0 +1,21 @@ +module Stretchy::Attributes::Type + # Public: Defines a rank_feature attribute for the model. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :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. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :url_length, :rank_feature, positive_score_impact: false + # end + # + # Returns nothing. + class RankFeature < Stretchy::Attributes::Type::Base + OPTIONS = [:positive_score_impact] + + def type + :rank_feature + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/rank_features.rb b/lib/stretchy/attributes/type/rank_features.rb new file mode 100644 index 0000000..1d0879c --- /dev/null +++ b/lib/stretchy/attributes/type/rank_features.rb @@ -0,0 +1,24 @@ +module Stretchy::Attributes::Type + # Public: Defines a rank_features attribute for the model. + # + # 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. + + # 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. + # opts - The Hash options used to refine the attribute (default: {}): + # :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. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :negative_reviews, :rank_features, positive_score_impact: false + # end + # + # Returns nothing. + class RankFeatures < Stretchy::Attributes::Type::Base + OPTIONS = [:positive_score_impact] + + def type + :rank_features + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/search_as_you_type.rb b/lib/stretchy/attributes/type/search_as_you_type.rb new file mode 100644 index 0000000..5fcfee2 --- /dev/null +++ b/lib/stretchy/attributes/type/search_as_you_type.rb @@ -0,0 +1,30 @@ +module Stretchy::Attributes::Type + # 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. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :max_shingle_size - The Integer indicating the largest shingle size to create. Valid values are 2 to 4. Defaults to 3. + # :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. + # :index - The Boolean indicating if the field should be searchable. Defaults to true. + # :index_options - The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'. + # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :search_analyzer - The String analyzer that should be used at search time on text fields. Defaults to the analyzer setting. + # :search_quote_analyzer - The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting. + # :similarity - The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'. + # :term_vector - The String indicating if term vectors should be stored for the field. Defaults to 'no'. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :name, :search_as_you_type, max_shingle_size: 4 + # end + # + # Returns nothing. + class SearchAsYouType < Stretchy::Attributes::Type::Base + OPTIONS = [:max_shingle_size, :analyzer, :index, :index_options, :norms, :store, :search_analyzer, :search_quote_analyzer, :similarity, :term_vector] + + def type + :search_as_you_type + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/shape.rb b/lib/stretchy/attributes/type/shape.rb new file mode 100644 index 0000000..237145f --- /dev/null +++ b/lib/stretchy/attributes/type/shape.rb @@ -0,0 +1,24 @@ +module Stretchy::Attributes::Type + # Public: Defines a shape attribute for the model. This field type is used for complex shapes. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :orientation - The String indicating how to interpret vertex order for polygons / multipolygons. Can be 'right', 'ccw', 'counterclockwise', 'left', 'cw', 'clockwise'. Defaults to 'ccw'. + # :ignore_malformed - The Boolean indicating if malformed GeoJSON or WKT shapes should be ignored. Defaults to false. + # :ignore_z_value - The Boolean indicating if the z value of three dimension points should be ignored. Defaults to true. + # :coerce - The Boolean indicating if unclosed linear rings in polygons will be automatically closed. Defaults to false. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :boundary, :shape, orientation: 'cw' + # end + # + # Returns nothing. + class Shape < Stretchy::Attributes::Type::Base + OPTIONS = [:orientation, :ignore_malformed, :ignore_z_value, :coerce] + + def type + :shape + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/sparse_vector.rb b/lib/stretchy/attributes/type/sparse_vector.rb index e77598d..d1df4ab 100644 --- a/lib/stretchy/attributes/type/sparse_vector.rb +++ b/lib/stretchy/attributes/type/sparse_vector.rb @@ -16,7 +16,7 @@ module Stretchy module Attributes module Type - class SparseVector < ActiveModel::Type::Value + class SparseVector < Stretchy::Attributes::Type::Base def type :sparse_vector diff --git a/lib/stretchy/attributes/type/string.rb b/lib/stretchy/attributes/type/string.rb new file mode 100644 index 0000000..f35a3e0 --- /dev/null +++ b/lib/stretchy/attributes/type/string.rb @@ -0,0 +1,7 @@ +module Stretchy::Attributes::Type + class String < Stretchy::Attributes::Type::Base # :nodoc: + def type + :string + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/text.rb b/lib/stretchy/attributes/type/text.rb index d50ebef..bde23c5 100644 --- a/lib/stretchy/attributes/type/text.rb +++ b/lib/stretchy/attributes/type/text.rb @@ -1,12 +1,38 @@ -module Stretchy - module Attributes - module Type - # alias for ActiveModel::Type::String - class Text < ActiveModel::Type::String - def type - :text - end - end +module Stretchy::Attributes::Type + # Public: Defines a text attribute for the model. This field type is used for text strings. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :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. + # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false. + # :fielddata - The Boolean indicating if the field can use in-memory fielddata for sorting, aggregations, or scripting. Defaults to false. + # :fielddata_frequency_filter - The Hash of expert settings which allow to decide which values to load in memory when fielddata is enabled. + # :fields - The Hash of multi-fields allow the same string value to be indexed in multiple ways for different purposes. + # :index - The Boolean indicating if the field should be searchable. Defaults to true. + # :index_options - The String indicating what information should be stored in the index, for search and highlighting purposes. Defaults to 'positions'. + # :index_prefixes - The Hash indicating if term prefixes of between 2 and 5 characters are indexed into a separate field. + # :index_phrases - The Boolean indicating if two-term word combinations (shingles) are indexed into a separate field. Defaults to false. + # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to true. + # :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. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :search_analyzer - The String analyzer that should be used at search time on the text field. Defaults to the analyzer setting. + # :search_quote_analyzer - The String analyzer that should be used at search time when a phrase is encountered. Defaults to the search_analyzer setting. + # :similarity - The String indicating which scoring algorithm or similarity should be used. Defaults to 'BM25'. + # :term_vector - The String indicating if term vectors should be stored for the field. Defaults to 'no'. + # :meta - The Hash of metadata about the field. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :description, :text, analyzer: 'english' + # end + # + # Returns nothing. + class Text < Stretchy::Attributes::Type::Base + 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] + + def type + :text end - end -end \ No newline at end of file + end + end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/token_count.rb b/lib/stretchy/attributes/type/token_count.rb new file mode 100644 index 0000000..1746bcf --- /dev/null +++ b/lib/stretchy/attributes/type/token_count.rb @@ -0,0 +1,26 @@ +module Stretchy::Attributes::Type + # Public: Defines a token_count attribute for the model. This field type is used for counting the number of tokens in a string. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :analyzer - The String analyzer to be used to analyze the string value. Required. + # :enable_position_increments - The Boolean indicating if position increments should be counted. Defaults to true. + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :index - The Boolean indicating if the field should be searchable. Defaults to true. + # :null_value - The Numeric value to be substituted for any explicit null values. Defaults to null. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :description_token_count, :token_count, analyzer: 'standard' + # end + # + # Returns nothing. + class TokenCount < Stretchy::Attributes::Type::Base + OPTIONS = [:analyzer, :enable_position_increments, :doc_values, :index, :null_value, :store] + + def type + :token_count + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/version.rb b/lib/stretchy/attributes/type/version.rb new file mode 100644 index 0000000..72d2d5d --- /dev/null +++ b/lib/stretchy/attributes/type/version.rb @@ -0,0 +1,21 @@ +module Stretchy::Attributes::Type + # Public: Defines a version attribute for the model. This field type is used for software versions following the Semantic Versioning rules. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :meta - The Hash of metadata about the field. + # + # Examples + # + # class MyModel < Stretchy::Record + # attribute :software_version, :version + # end + # + # Returns nothing. + class Version < Stretchy::Attributes::Type::Base + OPTIONS = [:meta] + + def type + :version + end + end +end \ No newline at end of file diff --git a/lib/stretchy/attributes/type/wildcard.rb b/lib/stretchy/attributes/type/wildcard.rb new file mode 100644 index 0000000..07aa585 --- /dev/null +++ b/lib/stretchy/attributes/type/wildcard.rb @@ -0,0 +1,35 @@ +module Stretchy::Attributes::Type + # Public: Defines a wildcard attribute for the model. This field type is a specialization of the keyword field, but it supports wildcard searches. + # + # opts - The Hash options used to refine the attribute (default: {}): + # :doc_values - The Boolean indicating if the field should be stored on disk in a column-stride fashion. Defaults to true. + # :eager_global_ordinals - The Boolean indicating if global ordinals should be loaded eagerly on refresh. Defaults to false. + # :fields - The Hash of multi-fields for the same string value to be indexed in multiple ways. + # :ignore_above - The Integer limit for the length of the string. Strings longer than this limit will not be indexed. Defaults to 2147483647. + # :index - The Boolean indicating if the field should be quickly searchable. Defaults to true. + # :index_options - The String indicating what information should be stored in the index for scoring purposes. Defaults to 'docs'. + # :meta - The Hash metadata about the field. + # :norms - The Boolean indicating if field-length should be taken into account when scoring queries. Defaults to false. + # :null_value - The String value to be substituted for any explicit null values. Defaults to null. + # :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'. + # :script - The String script that will index values generated by this script, rather than reading the values directly from the source. + # :store - The Boolean indicating if the field value should be stored and retrievable separately from the _source field. Defaults to false. + # :similarity - The String scoring algorithm or similarity to be used. Defaults to 'BM25'. + # :normalizer - The String pre-processor for the keyword prior to indexing. Defaults to null. + # :split_queries_on_whitespace - The Boolean indicating if full text queries should split the input on whitespace. Defaults to false. + # :time_series_dimension - The Boolean indicating if the field is a time series dimension. Defaults to false. + # + # Examples + # + # class MyModel + # include StretchyModel + # attribute :description_wildcard, :wildcard + # end + # + # Returns nothing. + class Wildcard < Stretchy::Attributes::Type::Keyword + def type + :wildcard + end + end +end \ No newline at end of file diff --git a/lib/stretchy/record.rb b/lib/stretchy/record.rb index 5bc5100..a4209be 100644 --- a/lib/stretchy/record.rb +++ b/lib/stretchy/record.rb @@ -30,7 +30,7 @@ def self.inherited(base) extend Stretchy::Querying # Set up common attributes - attribute :id, :string #, default: lambda { SecureRandom.uuid } + attribute :id, :keyword #, default: lambda { SecureRandom.uuid } attribute :created_at, :datetime, default: lambda { Time.now.utc } attribute :updated_at, :datetime, default: lambda { Time.now.utc } diff --git a/spec/stretchy/attributes_spec.rb b/spec/stretchy/attributes_spec.rb index a48d90a..f6c598a 100644 --- a/spec/stretchy/attributes_spec.rb +++ b/spec/stretchy/attributes_spec.rb @@ -22,9 +22,10 @@ let(:model) do class AttributeModel < Stretchy::Record attribute :name, :text - attribute :age, :integer + attribute :age, :integer attribute :tags, :array - attribute :data, :hash + attribute :data, :hash, properties: {weights: {type: :array}, biases: {type: :integer}} + attribute :flagged, :boolean, default: false end AttributeModel end @@ -50,6 +51,27 @@ class AttributeModel < Stretchy::Record expect(model.attribute_types['data'].cast({})).to be_a Elasticsearch::Model::HashWrapper end end + + + context 'mappings' do + + it 'returns mappings' do + expect(model.attribute_mappings).to eq({ + properties: { + name: {type: :text}, + age: {type: :integer}, + tags: {type: :array}, + data: {type: :hash, properties: {weights: {type: :array}, biases: {type: :integer}}}, + flagged: {type: :boolean}, + id: { type: :keyword }, + created_at: { type: :datetime }, + updated_at: { type: :datetime } + } + }.as_json) + end + + + end end end \ No newline at end of file