123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::Schema

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(fields = []) ⇒ Schema

Build a schema from a list of fields.

Parameters:

  • fields (Array<Field>) (defaults to: [])

    the fields that make up the schema

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 16

def initialize(fields = [])
  @fields = fields
end

Class Method Details

.build(&block) ⇒ Schema

Build a schema using the block DSL evaluated in a SchemaDefinition.

Examples:

Redis::Commands::Search::Schema.build do
  text_field "title", weight: 5.0, sortable: true
  numeric_field "price"
end

Returns:

  • (Schema)

    the schema built from the block

Raises:

[ GitHub ]

  
# 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.message
  end
  new(definition.fields)
end

Instance Attribute Details

#fields (readonly)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 11

attr_reader :fields

Instance Method Details

#each {|field| ... } ⇒ Enumerator, Array<Field>

Iterate over the schema's fields.

Yield Parameters:

  • field (Field)

    each field in order

Returns:

  • (Enumerator, Array<Field>)

    the fields when no block is given

[ GitHub ]

  
# 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".

Parameters:

  • name (String, Symbol)

    the field name/path or alias to look up

Returns:

  • (Field, nil)

    the matching field, or nil if none matches

[ GitHub ]

  
# 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_argsArray

Render the schema as the array of FT.CREATE tokens.

Examples:

schema.to_args # => ["SCHEMA", "title", "TEXT", "SORTABLE"]

Returns:

  • (Array)

    the SCHEMA keyword followed by each field's tokens

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 44

def to_args
  ['SCHEMA'] + @fields.flat_map(&:to_args)
end