Module: Redis::Commands::VectorSets
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | lib/redis/commands/vector_sets.rb |
Instance Method Summary
-
#vadd(key, vector, element, reduce: nil, cas: nil, quantization: nil, ef: nil, attributes: nil, m: nil) ⇒ Boolean
Add a new element into a vector set, or update its vector if it already exists.
-
#vcard(key) ⇒ Integer
Return the number of elements in a vector set.
-
#vdim(key) ⇒ Integer
Return the number of dimensions of the vectors in a vector set.
-
#vemb(key, element, raw: nil) ⇒ Array<Float>, ...
Return the approximate vector associated with an element in a vector set.
-
#vgetattr(key, element, raw: false) ⇒ Object, ...
Return the JSON attributes associated with an element in a vector set.
-
#vinfo(key) ⇒ Hash?
Return metadata and internal details about a vector set, including size, dimensions, quantization type, and graph structure.
-
#vismember(key, element) ⇒ Boolean
Check if an element exists in a vector set.
-
#vlinks(key, element, withscores: false, with_scores: withscores) ⇒ Array<Array<String>>, ...
Return the neighbors of an element in a vector set, one entry per layer of the HNSW graph.
-
#vrandmember(key, count = nil) ⇒ String, ...
Return one or more random elements from a vector set.
-
#vrange(key, start, stop, count = nil) ⇒ Array<String>
Return the elements of a vector set within a lexicographical range, in lexicographical (byte-by-byte) order.
-
#vrem(key, element) ⇒ Boolean
Remove an element from a vector set.
-
#vsetattr(key, element, attributes) ⇒ Boolean
Associate JSON attributes with an element in a vector set, update them, or delete them.
-
#vsim(key, vector: nil, element: nil, withscores: false, with_scores: withscores, withattribs: false, with_attribs: withattribs, raw: false, count: nil, epsilon: nil, ef: nil, filter: nil, filter_ef: nil, truth: nil, nothread: nil) ⇒ Array<String>, Hash
Return elements similar to a given vector or element, by approximate (HNSW) or exact (
truth: true) similarity search.
Instance Method Details
#vadd(key, vector, element, reduce: nil, cas: nil, quantization: nil, ef: nil, attributes: nil, m: nil) ⇒ Boolean
Add a new element into a vector set, or update its vector if it already exists.
The vector can be given as an Array of numbers (sent as VALUES), or
as a String holding a little-endian FP32 blob (sent as FP32), e.g.
values.pack("e*").
# File 'lib/redis/commands/vector_sets.rb', line 42
def vadd(key, vector, element, reduce: nil, cas: nil, quantization: nil, ef: nil, attributes: nil, m: nil) args = [:vadd, key] args << "REDUCE" << Integer(reduce) if reduce if vector.is_a?(String) args << "FP32" << vector else args << "VALUES" << vector.size args.concat(vector.map { |value| Float(value) }) end args << element args << "CAS" if cas if quantization case quantization.to_s.downcase when "noquant" then args << "NOQUANT" when "q8" then args << "Q8" when "bin" then args << "BIN" else raise ArgumentError, "unknown quantization: #{quantization.inspect}" end end args << "EF" << Integer(ef) if ef if attributes args << "SETATTR" << (attributes.is_a?(String) ? attributes : ::JSON.generate(attributes)) end args << "M" << Integer(m) if m send_command(args, &BoolifyBoolean) end
#vcard(key) ⇒ Integer
Return the number of elements in a vector set.
# File 'lib/redis/commands/vector_sets.rb', line 85
def vcard(key) send_command([:vcard, key]) end
#vdim(key) ⇒ Integer
Return the number of dimensions of the vectors in a vector set.
For a vector set created with the REDUCE option this reports the
reduced dimension, although full-size vectors must still be used when
querying with VSIM.
# File 'lib/redis/commands/vector_sets.rb', line 102
def vdim(key) send_command([:vdim, key]) end
#vemb(key, element, raw: nil) ⇒ Array<Float>, ...
Return the approximate vector associated with an element in a vector set. The round trip is approximate because vectors are normalized and (by default) quantized on insertion.
# File 'lib/redis/commands/vector_sets.rb', line 123
def vemb(key, element, raw: nil) if raw send_command([:vemb, key, element, "RAW"], &HashifyVectorEmbeddingRaw) else send_command([:vemb, key, element], &FloatifyArray) end end
#vgetattr(key, element, raw: false) ⇒ Object, ...
Return the JSON attributes associated with an element in a vector set.
By default the reply is parsed with JSON.parse; pass raw: true to get
the JSON string as stored.
# File 'lib/redis/commands/vector_sets.rb', line 396
def vgetattr(key, element, raw: false) send_command([:vgetattr, key, element]) do |reply| if reply.nil? || raw reply else ::JSON.parse(reply) end end end
#vinfo(key) ⇒ Hash?
Return metadata and internal details about a vector set, including size, dimensions, quantization type, and graph structure.
# File 'lib/redis/commands/vector_sets.rb', line 347
def vinfo(key) send_command([:vinfo, key], &Hashify) end
#vismember(key, element) ⇒ Boolean
Check if an element exists in a vector set.
# File 'lib/redis/commands/vector_sets.rb', line 274
def vismember(key, element) send_command([:vismember, key, element], &BoolifyBoolean) end
#vlinks(key, element, withscores: false, with_scores: withscores) ⇒ Array<Array<String>>, ...
Return the neighbors of an element in a vector set, one entry per layer of the HNSW graph.
# File 'lib/redis/commands/vector_sets.rb', line 147
def vlinks(key, element, withscores: false, with_scores: withscores) if with_scores send_command([:vlinks, key, element, "WITHSCORES"], &HashifyVectorLinksWithScores) else send_command([:vlinks, key, element]) end end
#vrandmember(key, count = nil) ⇒ String, ...
Return one or more random elements from a vector set.
Behaves like SRANDMEMBER: a positive count returns up to that many distinct elements, a negative count returns exactly that many elements possibly with duplicates, and a count exceeding the set size returns the whole set.
# File 'lib/redis/commands/vector_sets.rb', line 298
def vrandmember(key, count = nil) if count.nil? send_command([:vrandmember, key]) else send_command([:vrandmember, key, Integer(count)]) end end
#vrange(key, start, stop, count = nil) ⇒ Array<String>
Return the elements of a vector set within a lexicographical range, in lexicographical (byte-by-byte) order.
Boundaries follow the ZRANGEBYLEX syntax: "[elem" inclusive,
"(elem" exclusive, "-" minimum, "+" maximum. A negative count
returns all elements in the range. VRANGE is a stateless iterator: to
paginate, pass the last returned element as an exclusive start on the
next call.
# File 'lib/redis/commands/vector_sets.rb', line 331
def vrange(key, start, stop, count = nil) args = [:vrange, key, start, stop] args << Integer(count) if count send_command(args) end
#vrem(key, element) ⇒ Boolean
Remove an element from a vector set. Memory is reclaimed immediately.
# File 'lib/redis/commands/vector_sets.rb', line 260
def vrem(key, element) send_command([:vrem, key, element], &BoolifyBoolean) end
#vsetattr(key, element, attributes) ⇒ Boolean
Associate JSON attributes with an element in a vector set, update them, or delete them. Attributes can be used in filtered similarity searches with VSIM.
# File 'lib/redis/commands/vector_sets.rb', line 369
def vsetattr(key, element, attributes) json = case attributes when nil then "" when String then attributes else ::JSON.generate(attributes) end send_command([:vsetattr, key, element, json], &BoolifyBoolean) end
#vsim(key, vector: nil, element: nil, withscores: false, with_scores: withscores, withattribs: false, with_attribs: withattribs, raw: false, count: nil, epsilon: nil, ef: nil, filter: nil, filter_ef: nil, truth: nil, nothread: nil) ⇒ Array<String>, Hash
Return elements similar to a given vector or element, by approximate
(HNSW) or exact (truth: true) similarity search.
The query is either a vector — an Array of numbers (sent as VALUES)
or a little-endian FP32 String blob (sent as FP32) — or the name of
an existing element (sent as ELE). Exactly one of vector: and
element: must be given.
# File 'lib/redis/commands/vector_sets.rb', line 199
def vsim(key, vector: nil, element: nil, withscores: false, with_scores: withscores, withattribs: false, with_attribs: withattribs, raw: false, count: nil, epsilon: nil, ef: nil, filter: nil, filter_ef: nil, truth: nil, nothread: nil) unless vector.nil? ^ element.nil? raise ArgumentError, "must provide exactly one of vector or element" end args = [:vsim, key] if element args << "ELE" << element elsif vector.is_a?(String) args << "FP32" << vector else args << "VALUES" << vector.size args.concat(vector.map { |value| Float(value) }) end args << "WITHSCORES" if with_scores args << "WITHATTRIBS" if with_attribs args << "COUNT" << Integer(count) if count args << "EPSILON" << Float(epsilon) if epsilon args << "EF" << Integer(ef) if ef args << "FILTER" << filter if filter args << "FILTER-EF" << Integer(filter_ef) if filter_ef args << "TRUTH" if truth args << "NOTHREAD" if nothread if with_scores && with_attribs send_command(args) do |reply| reply = HashifyVectorScoresWithAttribs.call(reply) if raw || !reply.is_a?(Hash) reply else reply.transform_values { |(score, attribs)| [score, attribs && ::JSON.parse(attribs)] } end end elsif with_scores send_command(args, &HashifyVectorScores) elsif with_attribs send_command(args) do |reply| reply = Hashify.call(reply) if raw || !reply.is_a?(Hash) reply else reply.transform_values { |attribs| attribs && ::JSON.parse(attribs) } end end else send_command(args) end end