Class: Leann::SearchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/leann/search_result.rb

Overview

Represents a single search result

Examples:

results = Leann.search("my_index", "query")
results.each do |result|
  puts result.text
  puts result.score
  puts result.[:source]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, text:, score:, metadata: {}) ⇒ SearchResult

Returns a new instance of SearchResult.

Parameters:

  • id (String)
  • text (String)
  • score (Float)
  • metadata (Hash) (defaults to: {})


31
32
33
34
35
36
# File 'lib/leann/search_result.rb', line 31

def initialize(id:, text:, score:, metadata: {})
  @id = id
  @text = text
  @score = score.to_f
  @metadata = .transform_keys(&:to_sym)
end

Instance Attribute Details

#idString (readonly)

Returns Document ID.

Returns:

  • (String)

    Document ID



16
17
18
# File 'lib/leann/search_result.rb', line 16

def id
  @id
end

#metadataHash (readonly)

Returns Document metadata.

Returns:

  • (Hash)

    Document metadata



25
26
27
# File 'lib/leann/search_result.rb', line 25

def 
  @metadata
end

#scoreFloat (readonly)

Returns Similarity score (higher is better).

Returns:

  • (Float)

    Similarity score (higher is better)



22
23
24
# File 'lib/leann/search_result.rb', line 22

def score
  @score
end

#textString (readonly)

Returns Document text.

Returns:

  • (String)

    Document text



19
20
21
# File 'lib/leann/search_result.rb', line 19

def text
  @text
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare by score (for sorting)

Parameters:

Returns:

  • (Integer)


74
75
76
# File 'lib/leann/search_result.rb', line 74

def <=>(other)
  other.score <=> score # Descending order
end

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

Check equality

Parameters:

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/leann/search_result.rb', line 81

def ==(other)
  return false unless other.is_a?(SearchResult)

  id == other.id && text == other.text && score == other.score
end

#hashInteger

Hash code for use as hash key

Returns:

  • (Integer)


90
91
92
# File 'lib/leann/search_result.rb', line 90

def hash
  [id, text, score].hash
end

#inspectString

Detailed inspection

Returns:

  • (String)


56
57
58
# File 'lib/leann/search_result.rb', line 56

def inspect
  "#<Leann::SearchResult id=#{id.inspect} score=#{format("%.4f", score)} text=#{truncated_text(50).inspect}>"
end

#to_hHash

Convert to hash

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
# File 'lib/leann/search_result.rb', line 62

def to_h
  {
    id: id,
    text: text,
    score: score,
    metadata: 
  }
end

#to_sString

Human-readable string representation

Returns:

  • (String)


50
51
52
# File 'lib/leann/search_result.rb', line 50

def to_s
  "[#{format("%.3f", score)}] #{truncated_text(80)}"
end

#truncated_text(max_length = 100, omission: "...") ⇒ String

Truncate text to a maximum length

Parameters:

  • max_length (Integer) (defaults to: 100)
  • omission (String) (defaults to: "...")

Returns:

  • (String)


42
43
44
45
46
# File 'lib/leann/search_result.rb', line 42

def truncated_text(max_length = 100, omission: "...")
  return text if text.length <= max_length

  text[0, max_length - omission.length] + omission
end