Module: Documentrix::Documents::Cache::Records::FindRecords

Defined in:
lib/documentrix/documents/cache/records.rb

Instance Method Summary collapse

Instance Method Details

#find_records(needle, tags: nil, max_records: nil) {|record| ... } ⇒ Array<Documentrix::Documents::Records>

The find_records method finds records that match the given needle and tags.

Parameters:

  • needle (Array)

    an array containing the embedding vector

  • tags (String, Array) (defaults to: nil)

    a string or array of strings representing the tags to search for

  • max_records (Integer) (defaults to: nil)

    the maximum number of records to return

Yields:

  • (record)

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/documentrix/documents/cache/records.rb', line 70

def find_records(needle, tags: nil, max_records: nil)
  tags    = Documentrix::Utils::Tags.new(Array(tags)).to_a
  records = self
  if tags.present?
    records = records.select { |_key, record| (tags & record.tags).size >= 1 }
  end
  needle_norm = norm(needle)
  records     = records.sort_by { |key, record|
    record.key        = key
    record.similarity = cosine_similarity(
      a: needle,
      b: record.embedding,
      a_norm: needle_norm,
      b_norm: record.norm,
    )
  }
  records.transpose.last&.reverse.to_a
end