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

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = []) ⇒ Schema

Build a schema from a list of fields.

Parameters:

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

    the fields that make up the schema



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

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

Instance Attribute Details

#fieldsObject (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.

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:



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

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



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

Parameters:

  • name (String, Symbol)

    the field name/path or alias to look up

Returns:

  • (Field, nil)

    the matching field, or nil if none matches



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_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



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

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