123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::IndexDefinition

Relationships & Source Files
Inherits: Object
Defined in: lib/redis/commands/modules/search/index_definition.rb

Overview

The definition portion of an FT.CREATE call: which keyspace the index is built over and how documents are scored and filtered. Renders into a token array exposed via #args.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(prefix: [], filter: nil, language_field: nil, language: nil, score_field: nil, score: 1.0, payload_field: nil, index_type: nil) ⇒ IndexDefinition

Build an index definition and pre-render its FT.CREATE tokens.

Examples:

Redis::Commands::Search::IndexDefinition.new(prefix: ["doc:"], index_type: IndexType::JSON).args
  # => ["ON", "JSON", "PREFIX", 1, "doc:", "SCORE", 1.0]

Parameters:

  • prefix (Array<String>, String, nil)

    key prefix(es) the index applies to (+PREFIX+); a single String is wrapped into a one-element list and nil means "no prefix"

  • filter (String, nil)

    a filter expression (+FILTER+)

  • language_field (String, nil)

    the field holding each document's language (+LANGUAGE_FIELD+)

  • language (String, nil)

    the default document language (+LANGUAGE+)

  • score_field (String, nil)

    the field holding each document's score (+SCORE_FIELD+)

  • score (Numeric)

    the default document score (+SCORE+)

  • payload_field (String, nil)

    the field holding each document's payload (+PAYLOAD_FIELD+)

  • index_type (String, nil)

    the indexed data structure, IndexType::HASH or IndexType::JSON (+ON+)

Raises:

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 37

def initialize(
  prefix: [], filter: nil, language_field: nil, language: nil,
  score_field: nil, score: 1.0, payload_field: nil, index_type: nil
)
  @args = []
  @index_type = index_type
  # Array() makes the prefix handling nil-safe (nil -> []) and wraps a lone String prefix
  # into a one-element list, so PREFIX always emits the correct count. dup detaches us from
  # the caller's list (Array() returns it unchanged when it's already an Array) so later
  # external mutations can't alter this definition.
  @prefixes = Array(prefix).dup
  append_index_type(index_type)
  append_prefix(@prefixes)
  append_filter(filter)
  append_language(language_field, language)
  append_score(score_field, score)
  append_payload(payload_field)
end

Instance Attribute Details

#argsArray, ... (readonly)

Returns:

  • (Array)

    the rendered FT.CREATE definition tokens

  • (Array<String>)

    the literal key prefixes the index applies to

  • (String, nil)

    the indexed data structure (IndexType::HASH/IndexType::JSON), or nil

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 19

attr_reader :args, :prefixes, :index_type

#index_typeArray, ... (readonly)

Returns:

  • (Array)

    the rendered FT.CREATE definition tokens

  • (Array<String>)

    the literal key prefixes the index applies to

  • (String, nil)

    the indexed data structure (IndexType::HASH/IndexType::JSON), or nil

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 19

attr_reader :args, :prefixes, :index_type

#prefixesArray, ... (readonly)

Returns:

  • (Array)

    the rendered FT.CREATE definition tokens

  • (Array<String>)

    the literal key prefixes the index applies to

  • (String, nil)

    the indexed data structure (IndexType::HASH/IndexType::JSON), or nil

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 19

attr_reader :args, :prefixes, :index_type

Instance Method Details

#append_filter(filter) (private)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 76

def append_filter(filter)
  if filter
    @args << "FILTER"
    @args << filter
  end
end

#append_index_type(index_type) (private)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 58

def append_index_type(index_type)
  if index_type == IndexType::HASH
    @args += ["ON", "HASH"]
  elsif index_type == IndexType::JSON
    @args += ["ON", "JSON"]
  elsif !index_type.nil?
    raise ArgumentError, "index_type must be IndexType::HASH or IndexType::JSON"
  end
end

#append_language(language_field, language) (private)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 83

def append_language(language_field, language)
  if language_field
    @args << "LANGUAGE_FIELD"
    @args << language_field
  end
  if language
    @args << "LANGUAGE"
    @args << language
  end
end

#append_payload(payload_field) (private)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 105

def append_payload(payload_field)
  if payload_field
    @args << "PAYLOAD_FIELD"
    @args << payload_field
  end
end

#append_prefix(prefix) (private)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 68

def append_prefix(prefix)
  unless prefix.empty?
    @args << "PREFIX"
    @args << prefix.length
    prefix.each { |p| @args << p }
  end
end

#append_score(score_field, score) (private)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/index_definition.rb', line 94

def append_score(score_field, score)
  if score_field
    @args << "SCORE_FIELD"
    @args << score_field
  end
  if score
    @args << "SCORE"
    @args << score
  end
end