module

Redis::Commands::Vector

Instance methods

vadd(key : String, vector : Enumerable(Float32), element : String, *, reduce : Int | String | Nil = nil, setattr attributes = nil)

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",
  }
Source
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.

Source
vdim(key : String)

Return the dimensionality of the vectors in the vector set stored in key.

Source
vemb(key : String, element : String)

Return the vector for element stored in the vector set in key.

Source
vgetattr(key : String, element : String, as type : T.class) : T | Nil forall T

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.

Source
vgetattr(key : String, element : String)

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
Source
vinfo(key : String)
Source
vrange(key : String, start : String, end stop : String, count : Int | String | Nil = nil)

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).

Source
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 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)
Source
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 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)
Source