Im trying to implement vector search similarity in c#. I have to use the HSET command to send vectors to my index in Redis. The hset command is something like: HSET item:3 "x00x00x00x00". When I test this, I find out that the string I add which is "x00x00x00x00" gets added as ""\x00\x00\x00\x00". Redis does not remove the extra backslash and thus my searching fails later on.
public static async Task CreateIndexAsync()
{
try
{
await mux.GetDatabase().ExecuteAsync("FT.CREATE", "embeddings", "ON", "HASH", "PREFIX", "1", "item:", "SCHEMA", "vector", "VECTOR", "FLAT", "6", "TYPE", "FLOAT32", "DIM", "2", "DISTANCE_METRIC", "COSINE");
}
catch (Exception)
{
//swallow exception if index exists
}
}`
public static async Task AddAsync(string docId, string prefix, float[] vector)
{
string hex = toHexString(vector);
mux.GetDatabase().Execute("Hset", $"{prefix}{docId}", "vector", hex);
}`
I dont know how to solve this issue, it seems that Redis does not have good c# support
2
Answers
you are more than welcome to use NRedisStack, A .NET client (builds on StackExchange.Redis) and supports Redis Stack commands.
NRedisStack has support for vector search similarity commands.
Your code will look like that with NRedisStack:
I tried to reproduce the problem you described, while using NRedisStack, and this line worked fine:
this is the monitor output:
Links:
NRedisStack – link to github |
NRedisStack – link to NuGet
Just to make it clear from the answer of Shachar Pashchur. You don’t have to translate it to an escaped hexadecimal sequence.
You can just pass a flattened byte array of
float
ordouble
values into the hashset.If you are going to pass in an array of
double
values, remember to change theVectorField
TYPE
toFLOAT64