Class: Redis::Commands::Search::TextMatchPredicate

Inherits:
Predicate
  • Object
show all
Defined in:
lib/redis/commands/modules/search/query.rb

Overview

A text-match predicate rendering (@field:(pattern)).

Constant Summary

Constants inherited from Predicate

Predicate::TAG_ESCAPE_PATTERN, Predicate::TAG_SPECIAL_CHARACTERS, Predicate::TEXT_ESCAPE_PATTERN, Predicate::TEXT_SPECIAL_CHARACTERS

Instance Attribute Summary

Attributes inherited from Predicate

#field

Instance Method Summary collapse

Constructor Details

#initialize(field, pattern, raw: false) ⇒ TextMatchPredicate

Returns a new instance of TextMatchPredicate.

Parameters:

  • field (String)

    the text field name

  • pattern (String)

    the text pattern to match

  • raw (Boolean) (defaults to: false)

    when true the pattern is interpolated verbatim so RediSearch operators (wildcards, | OR, ...) apply; when false (default) metacharacters are escaped and the pattern is matched literally



675
676
677
678
679
# File 'lib/redis/commands/modules/search/query.rb', line 675

def initialize(field, pattern, raw: false)
  super(field)
  @pattern = pattern
  @raw = raw
end

Instance Method Details

#to_sString

Returns the query-string fragment, e.g. (@title:(hello world)).

Returns:

  • (String)

    the query-string fragment, e.g. (@title:(hello world))



682
683
684
685
686
687
688
689
690
# File 'lib/redis/commands/modules/search/query.rb', line 682

def to_s
  # Group the pattern: RediSearch scopes "@field:" only to the term immediately after it,
  # so a bare "@title:hello world" would match "hello" on the field but "world" globally
  # (across all fields). Wrapping in parentheses scopes the whole pattern to the field;
  # it is a no-op for single-term patterns. Unless raw, escape metacharacters so a value
  # can't inject query syntax.
  pattern = @raw ? @pattern : escape_text(@pattern)
  "(@#{@field}:(#{pattern}))"
end