Class: Redis::Commands::Search::Schema
- Inherits:
-
Object
- Object
- Redis::Commands::Search::Schema
- Includes:
- Enumerable
- Defined in:
- lib/redis/commands/modules/search/schema.rb
Overview
A Redis Query Engine index schema: an ordered collection of Field
objects rendered into the SCHEMA section of an FT.CREATE call.
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
-
.build(&block) ⇒ Schema
Build a schema using the block DSL evaluated in a SchemaDefinition.
Instance Method Summary collapse
-
#each {|field| ... } ⇒ Enumerator, Array<Field>
Iterate over the schema's fields.
-
#field(name) ⇒ Field?
Find a field by its name/path or its
ASalias, so e.g. -
#initialize(fields = []) ⇒ Schema
constructor
Build a schema from a list of fields.
-
#to_args ⇒ Array
Render the schema as the array of
FT.CREATEtokens.
Constructor Details
#initialize(fields = []) ⇒ Schema
Build a schema from a list of fields.
16 17 18 |
# File 'lib/redis/commands/modules/search/schema.rb', line 16 def initialize(fields = []) @fields = fields end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
11 12 13 |
# File 'lib/redis/commands/modules/search/schema.rb', line 11 def fields @fields end |
Class Method Details
.build(&block) ⇒ Schema
Build a schema using the block DSL evaluated in a Redis::Commands::Search::SchemaDefinition.
58 59 60 61 62 63 64 65 66 |
# File 'lib/redis/commands/modules/search/schema.rb', line 58 def self.build(&block) definition = SchemaDefinition.new begin definition.instance_eval(&block) rescue ArgumentError => e raise Redis::CommandError, e. end new(definition.fields) end |
Instance Method Details
#each {|field| ... } ⇒ Enumerator, Array<Field>
Iterate over the schema's fields.
34 35 36 |
# File 'lib/redis/commands/modules/search/schema.rb', line 34 def each(&block) @fields.each(&block) end |
#field(name) ⇒ Field?
Find a field by its name/path or its AS alias, so e.g. a JSON field declared as
"$.price" AS "price" is found by either "$.price" or "price".
25 26 27 28 |
# File 'lib/redis/commands/modules/search/schema.rb', line 25 def field(name) name = name.to_s @fields.find { |f| f.name.to_s == name || f.alias_name&.to_s == name } end |
#to_args ⇒ Array
Render the schema as the array of FT.CREATE tokens.
44 45 46 |
# File 'lib/redis/commands/modules/search/schema.rb', line 44 def to_args ['SCHEMA'] + @fields.flat_map(&:to_args) end |