123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Inherits: Object
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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(id, attributes: {}, score: nil, payload: nil) ⇒ Document

Parameters:

  • id (String)

    the document key/id

  • attributes (Hash{String => Object})

    the returned fields keyed by field name

  • score (Float, Array, nil)

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

  • payload (String, nil)

    the document payload

[ GitHub ]

  
# 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

[ GitHub ]

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

attr_reader :id, :score, :payload, :attributes

#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

[ GitHub ]

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

attr_reader :id, :score, :payload, :attributes

#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

[ GitHub ]

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

attr_reader :id, :score, :payload, :attributes

#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

[ GitHub ]

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

attr_reader :id, :score, :payload, :attributes

Instance Method Details

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

Parameters:

  • other (Object)

    the object to compare against

Returns:

  • (Boolean)

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

[ GitHub ]

  
# 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

[ GitHub ]

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

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

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/result.rb', line 61

alias eql? ==

#hashInteger

Returns:

  • (Integer)

    a hash derived from id, attributes, score and payload

[ GitHub ]

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

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

#key?(key) ⇒ Boolean

Parameters:

  • key (String, Symbol)

    the field name

Returns:

  • (Boolean)

    whether the document has the given field

[ GitHub ]

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

[ GitHub ]

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

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