Comments (6)
SearchTests.cs has Vector similarity tests, here are two of them that might help you understand how to implement vector data insertion and querying.
Later I plan to add it to the Examples folder.
Example with HSET:
[Fact]
public void QueryingVectorFields()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
var schema = new Schema().AddVectorField("v", Schema.VectorField.VectorAlgo.HNSW, new Dictionary<string, object>()
{
["TYPE"] = "FLOAT32",
["DIM"] = "2",
["DISTANCE_METRIC"] = "L2",
});
ft.Create("idx", new FTCreateParams(), schema);
db.HashSet("a", "v", "aaaaaaaa");
db.HashSet("b", "v", "aaaabaaa");
db.HashSet("c", "v", "aaaaabaa");
var q = new Query("*=>[KNN 2 @v $vec]").ReturnFields("__v_score").Dialect(2);
var res = ft.Search("idx", q.AddParam("vec", "aaaaaaaa"));
Assert.Equal(2, res.TotalResults);
}
Example with JSON.SET:
[Fact]
public void VectorSimilaritySearch()
{
IDatabase db = redisFixture.Redis.GetDatabase();
db.Execute("FLUSHALL");
var ft = db.FT();
var json = db.JSON();
json.Set("vec:1", "$", "{\"vector\":[1,1,1,1]}");
json.Set("vec:2", "$", "{\"vector\":[2,2,2,2]}");
json.Set("vec:3", "$", "{\"vector\":[3,3,3,3]}");
json.Set("vec:4", "$", "{\"vector\":[4,4,4,4]}");
var schema = new Schema().AddVectorField(FieldName.Of("$.vector").As("vector"), Schema.VectorField.VectorAlgo.FLAT, new Dictionary<string, object>()
{
["TYPE"] = "FLOAT32",
["DIM"] = "4",
["DISTANCE_METRIC"] = "L2",
});
var idxDef = new FTCreateParams().On(IndexDataType.JSON).Prefix("vec:");
Assert.True(ft.Create("vss_idx", idxDef, schema));
float[] vec = new float[] { 2, 2, 2, 2 };
byte[] queryVec = MemoryMarshal.Cast<float, byte>(vec).ToArray();
var query = new Query("*=>[KNN 3 @vector $query_vec]")
.AddParam("query_vec", queryVec)
.SetSortBy("__vector_score")
.Dialect(2);
var res = ft.Search("vss_idx", query);
Assert.Equal(3, res.TotalResults);
Assert.Equal("vec:2", res.Documents[0].Id.ToString());
Assert.Equal(0, res.Documents[0]["__vector_score"]);
var jsonRes = res.ToJson();
Assert.Equal("{\"vector\":[2,2,2,2]}", jsonRes![0]);
}
I would love to hear if this was helpful, if you have any further questions feel free to write to me.
from nredisstack.
嘿,你成功了吗?如果是这样,我很乐意关闭该问题。如果没有,请随时写信给我问题所在,我会帮助你。
Thank you, it's already done. The code to resolve it is as follows:
var base_query = $"(@modelsId:{{1}})=>[{search_type} {number_of_results} @{vector_field_name} $vec_param]";
var query = new Query(base_query)
.AddParam("vec_param", embedding.SelectMany(BitConverter.GetBytes).ToArray())
.Limit(0, number_of_results)
.Dialect(2);
string ss = query.QueryString;
ISearchCommands ft = db.FT();
var results = ft.Search(index_name, query);
from nredisstack.
Why didn’t the following code work? I want to add conditions for other fields except for the sending vector field.
var base_query = $"(@modelsId:1)=>[{search_type} {number_of_results} @{vector_field_name} $vec_param]";
var query = new Query(base_query)
.AddParam("vec_param", embedding.SelectMany(BitConverter.GetBytes).ToArray())
.Limit(0, number_of_results)
.Dialect(2);
string ss = query.QueryString;
ISearchCommands ft = db.FT();
var results = ft.Search(index_name, query);
from nredisstack.
from nredisstack.
Did you create an index using the FT.CREATE command?
I don't see it in the code you sent
from nredisstack.
hey @qq312888991, Did you manage?
If so, I would be happy to close the issue.
If not, feel free to write me what the problem is and I will help you.
from nredisstack.
Related Issues (20)
- Add support for BLMPOP
- Add support for BLPOP
- Add support for BRPOP
- Add support for BRPOPLPUSH
- Add support for XREADGROUP
- Trying to add nested JSON fields returns success, but doesnt update JSON
- TestJsonTransaction seems to fail arbitrarily HOT 2
- Index.Attribute throws error for sortable fields HOT 7
- FT.Aggregate with APPLY - text fields data is returing in lowercase if we do the multi column sort HOT 7
- Add support for IBatch.JSON() HOT 1
- TimeSeries - Insertion-filter for close samples
- test support for dialect 5 in RediSearch exact matching
- fix result data change for FT.PROFILE
- support for serch with polygons HOT 1
- FT.PROFILE command response layout change
- Support for indexing and querying missing and empty values
- Support for bfloat16 and float16 types in vector fields indexing
- NRedisStack not writing timeseries values to Valkey HOT 1
- TimeSeries Create function always creates the TS as "uncompressed" when specifying true or false to the function argument HOT 1
- Doc tests are failing randomly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nredisstack.