module
Redis::Commands::Vector
Direct including types
Defined in:
commands/vector.crInstance Method Summary
-
#vadd(key : String, vector : Enumerable(Float32), element : String, *, reduce : Int | String | Nil = nil, setattr attributes = nil)
Add
elementwith its associatedvectorto the vector set stored inkey. -
#vcard(key : String)
Return the number of vectors contained in the vector set stored in
key, also referred to as the cardinality of the set. -
#vdim(key : String)
Return the dimensionality of the vectors in the vector set stored in
key. -
#vemb(key : String, element : String)
Return the vector for
elementstored in the vector set inkey. -
#vgetattr(key : String, element : String, as type : T.class) : T | Nil forall T
Get the attributes for
elementstored inkey, deserializing them as the typeT. -
#vgetattr(key : String, element : String)
Get the attributes for
elementstored inkeyas a JSON string. -
#vrange(key : String, start : String, end stop : String, count : Int | String | Nil = nil)
Return an array of elements between
startandendin the vector set stored inkey, optionally limiting the result size tocount. -
#vsim(key : String, element : String, *, epsilon : Float | String | Nil = nil, withscores : Bool = false, count : Int | String | Nil = nil)
Return the elements of the vector set stored in
keywhose vectors are most similar to that ofelement, which must also be stored inkey. -
#vsim(key : String, vector : Array(Float32), *, epsilon : Float | String | Nil = nil, withscores : Bool = false, count : Int | String | Nil = nil)
Return the elements of the vector set stored in
keywhose vectors are most similar tovector, which does not need to be stored inkey.
Instance Method Detail
Add element with its associated vector to the vector set stored in key.
redis.vadd "embeddings", vectorize(text), text
You can optionally reduce vector to a given dimensionality with the
reduce argument if you need to sacrifice precision to conserve space.
redis.vadd "embeddings", vectorize(text), text, reduce: 50
You can also set attributes for this vector. Any JSON-serializable value
can be passed into the setattr argument.
redis.vadd "embeddings", vectorize(text), text,
setattr: {
lang: "en",
}
Return the number of vectors contained in the vector set stored in key,
also referred to as the cardinality of the set.
Return the vector for element stored in the vector set in key.
Get the attributes for element stored in key, deserializing them as the
type T.
struct Properties
include JSON::Serializable
getter post_id : Int64
getter author_id : Int64
end
if properties = redis.vgetattr("embeddings", post_text, as: Properties)
author = AuthorQuery.new.find(properties.author_id)
end
NOTE You can only run this method against Immediate types, such as
Client, Connection, ReplicationClient, or Cluster. Calling this
method on a Pipeline or Transaction will result in a compile-time error.
If you need to run this in a pipeline, you will need to deserialize the
string manually with the other #vgetattr overload.
Get the attributes for element stored in key as a JSON string.
struct Properties
include JSON::Serializable
getter post_id : Int64
getter author_id : Int64
end
if (json = redis.vgetattr("embeddings", post_text)) && (properties = Properties.from_json(json))
author = AuthorQuery.new.find(properties.author_id)
end
Return an array of elements between start and end in the vector set
stored in key, optionally limiting the result size to count.
The values for start and end have identical semantics to zrange with
the bylex option
(by: :lex in this shard).
Return the elements of the vector set stored in key whose vectors are most
similar to that of element, which must also be stored in key. You can
limit the similarity comparison using epsilon as an upper limit of cosine
distance or by capping the number of results with count (defaults to 10).
results = redis.vsim("embeddings", text, epsilon: 0.2, count: 25)
Return the elements of the vector set stored in key whose vectors are most
similar to vector, which does not need to be stored in key. You can
limit the similarity comparison using epsilon as an upper limit of cosine
distance or by capping the number of results with count (defaults to 10).
results = redis.vsim("embeddings", vectorize(text), epsilon: 0.2, count: 25)