Class: Redis::Commands::Search::Document

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

Overview

A single document returned by FT.SEARCH.

Behaves like a read-only hash over the document's returned fields, while also exposing the document id and, when requested, its score and payload.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, attributes: {}, score: nil, payload: nil) ⇒ Document

Returns a new instance of Document.

Parameters:

  • id (String)

    the document key/id

  • attributes (Hash{String => Object}) (defaults to: {})

    the returned fields keyed by field name

  • score (Float, Array, nil) (defaults to: nil)

    the relevance score (Float; [Float, explanation] with EXPLAINSCORE)

  • payload (String, nil) (defaults to: nil)

    the document payload



24
25
26
27
28
29
# File 'lib/redis/commands/modules/search/result.rb', line 24

def initialize(id, attributes: {}, score: nil, payload: nil)
  @id = id
  @attributes = attributes
  @score = score
  @payload = payload
end

Instance Attribute Details

#attributesString, ... (readonly)

Returns:

  • (String)

    the document key/id

  • (Float, Array, nil)

    the relevance score (present when WITHSCORES was set), normalized to a Float across RESP2/RESP3; with EXPLAINSCORE it is [Float, explanation]

  • (String, nil)

    the document payload (present when WITHPAYLOADS was set)

  • (Hash{String => Object})

    the returned fields keyed by field name



18
19
20
# File 'lib/redis/commands/modules/search/result.rb', line 18

def attributes
  @attributes
end

#idString, ... (readonly)

Returns:

  • (String)

    the document key/id

  • (Float, Array, nil)

    the relevance score (present when WITHSCORES was set), normalized to a Float across RESP2/RESP3; with EXPLAINSCORE it is [Float, explanation]

  • (String, nil)

    the document payload (present when WITHPAYLOADS was set)

  • (Hash{String => Object})

    the returned fields keyed by field name



18
19
20
# File 'lib/redis/commands/modules/search/result.rb', line 18

def id
  @id
end

#payloadString, ... (readonly)

Returns:

  • (String)

    the document key/id

  • (Float, Array, nil)

    the relevance score (present when WITHSCORES was set), normalized to a Float across RESP2/RESP3; with EXPLAINSCORE it is [Float, explanation]

  • (String, nil)

    the document payload (present when WITHPAYLOADS was set)

  • (Hash{String => Object})

    the returned fields keyed by field name



18
19
20
# File 'lib/redis/commands/modules/search/result.rb', line 18

def payload
  @payload
end

#scoreString, ... (readonly)

Returns:

  • (String)

    the document key/id

  • (Float, Array, nil)

    the relevance score (present when WITHSCORES was set), normalized to a Float across RESP2/RESP3; with EXPLAINSCORE it is [Float, explanation]

  • (String, nil)

    the document payload (present when WITHPAYLOADS was set)

  • (Hash{String => Object})

    the returned fields keyed by field name



18
19
20
# File 'lib/redis/commands/modules/search/result.rb', line 18

def score
  @score
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns whether other is a Document with equal id, attributes, score and payload.

Parameters:

  • other (Object)

    the object to compare against

Returns:

  • (Boolean)

    whether other is a Document with equal id, attributes, score and payload



54
55
56
57
58
59
60
# File 'lib/redis/commands/modules/search/result.rb', line 54

def ==(other)
  other.is_a?(Document) &&
    id == other.id &&
    attributes == other.attributes &&
    score == other.score &&
    payload == other.payload
end

#[](key) ⇒ Object?

Look up a returned field by name.

Parameters:

  • key (String, Symbol)

    the field name

Returns:

  • (Object, nil)

    the field value, or nil when the field is absent



35
36
37
# File 'lib/redis/commands/modules/search/result.rb', line 35

def [](key)
  @attributes[key.to_s]
end

#hashInteger

Returns a hash derived from id, attributes, score and payload.

Returns:

  • (Integer)

    a hash derived from id, attributes, score and payload



64
65
66
# File 'lib/redis/commands/modules/search/result.rb', line 64

def hash
  [id, attributes, score, payload].hash
end

#key?(key) ⇒ Boolean

Returns whether the document has the given field.

Parameters:

  • key (String, Symbol)

    the field name

Returns:

  • (Boolean)

    whether the document has the given field



41
42
43
# File 'lib/redis/commands/modules/search/result.rb', line 41

def key?(key)
  @attributes.key?(key.to_s)
end

#to_hHash{String => Object}

The document id plus its returned fields, as a flat hash.

Returns:

  • (Hash{String => Object})

    the attributes merged with "id"



48
49
50
# File 'lib/redis/commands/modules/search/result.rb', line 48

def to_h
  @attributes.merge("id" => @id)
end