|
6 | 6 | using static NRedisStack.Search.Schema;
|
7 | 7 | using NRedisStack.Search.Aggregation;
|
8 | 8 | using NRedisStack.Search.Literals.Enums;
|
| 9 | +using System.Runtime.InteropServices; |
9 | 10 |
|
10 | 11 | namespace NRedisStack.Tests.Search;
|
11 | 12 |
|
@@ -1920,6 +1921,98 @@ public async Task TestVectorCount_Issue70()
|
1920 | 1921 | Assert.Equal(expected.Count(), actual.Args.Length);
|
1921 | 1922 | }
|
1922 | 1923 |
|
| 1924 | + [Fact] |
| 1925 | + public void VectorSimilaritySearch() |
| 1926 | + { |
| 1927 | + IDatabase db = redisFixture.Redis.GetDatabase(); |
| 1928 | + db.Execute("FLUSHALL"); |
| 1929 | + var ft = db.FT(); |
| 1930 | + var json = db.JSON(); |
| 1931 | + |
| 1932 | + json.Set("vec:1", "$", "{\"vector\":[1,1,1,1]}"); |
| 1933 | + json.Set("vec:2", "$", "{\"vector\":[2,2,2,2]}"); |
| 1934 | + json.Set("vec:3", "$", "{\"vector\":[3,3,3,3]}"); |
| 1935 | + json.Set("vec:4", "$", "{\"vector\":[4,4,4,4]}"); |
| 1936 | + |
| 1937 | + var schema = new Schema().AddVectorField(FieldName.Of("$.vector").As("vector"), Schema.VectorField.VectorAlgo.FLAT, new Dictionary<string, object>() |
| 1938 | + { |
| 1939 | + ["TYPE"] = "FLOAT32", |
| 1940 | + ["DIM"] = "4", |
| 1941 | + ["DISTANCE_METRIC"] = "L2", |
| 1942 | + }); |
| 1943 | + |
| 1944 | + var idxDef = new FTCreateParams().On(IndexDataType.JSON).Prefix("vec:"); |
| 1945 | + Assert.True(ft.Create("vss_idx", idxDef, schema)); |
| 1946 | + |
| 1947 | + float[] vec = new float[] { 2, 2, 2, 2 }; |
| 1948 | + byte[] queryVec = MemoryMarshal.Cast<float, byte>(vec).ToArray(); |
| 1949 | + |
| 1950 | + |
| 1951 | + var query = new Query("*=>[KNN 3 @vector $query_vec]") |
| 1952 | + .AddParam("query_vec", queryVec) |
| 1953 | + .SetSortBy("__vector_score") |
| 1954 | + .Dialect(2); |
| 1955 | + var res = ft.Search("vss_idx", query); |
| 1956 | + |
| 1957 | + Assert.Equal(3, res.TotalResults); |
| 1958 | + |
| 1959 | + Assert.Equal("vec:2", res.Documents[0].Id.ToString()); |
| 1960 | + |
| 1961 | + Assert.Equal(0, res.Documents[0]["__vector_score"]); |
| 1962 | + |
| 1963 | + var jsonRes = res.ToJson(); |
| 1964 | + Assert.Equal("{\"vector\":[2,2,2,2]}", jsonRes![0]); |
| 1965 | + } |
| 1966 | + |
| 1967 | + [Fact] |
| 1968 | + public void QueryingVectorFields() |
| 1969 | + { |
| 1970 | + IDatabase db = redisFixture.Redis.GetDatabase(); |
| 1971 | + db.Execute("FLUSHALL"); |
| 1972 | + var ft = db.FT(); |
| 1973 | + var json = db.JSON(); |
| 1974 | + |
| 1975 | + var schema = new Schema().AddVectorField("v", Schema.VectorField.VectorAlgo.HNSW, new Dictionary<string, object>() |
| 1976 | + { |
| 1977 | + ["TYPE"] = "FLOAT32", |
| 1978 | + ["DIM"] = "2", |
| 1979 | + ["DISTANCE_METRIC"] = "L2", |
| 1980 | + }); |
| 1981 | + |
| 1982 | + ft.Create("idx", new FTCreateParams(), schema); |
| 1983 | + |
| 1984 | + db.HashSet("a", "v", "aaaaaaaa"); |
| 1985 | + db.HashSet("b", "v", "aaaabaaa"); |
| 1986 | + db.HashSet("c", "v", "aaaaabaa"); |
| 1987 | + |
| 1988 | + var q = new Query("*=>[KNN 2 @v $vec]").ReturnFields("__v_score").Dialect(2); |
| 1989 | + var res = ft.Search("idx", q.AddParam("vec", "aaaaaaaa")); |
| 1990 | + Assert.Equal(2, res.TotalResults); |
| 1991 | + } |
| 1992 | + |
| 1993 | + [Fact] |
| 1994 | + public async Task TestVectorFieldJson_Issue102Async() |
| 1995 | + { |
| 1996 | + IDatabase db = redisFixture.Redis.GetDatabase(); |
| 1997 | + db.Execute("FLUSHALL"); |
| 1998 | + var ft = db.FT(); |
| 1999 | + var json = db.JSON(); |
| 2000 | + |
| 2001 | + // JSON.SET 1 $ '{"vec":[1,2,3,4]}' |
| 2002 | + await json.SetAsync("1", "$", "{\"vec\":[1,2,3,4]}"); |
| 2003 | + |
| 2004 | + // FT.CREATE my_index ON JSON SCHEMA $.vec as vector VECTOR FLAT 6 TYPE FLOAT32 DIM 4 DISTANCE_METRIC L2 |
| 2005 | + var schema = new Schema().AddVectorField(FieldName.Of("$.vec").As("vector"), Schema.VectorField.VectorAlgo.FLAT, new Dictionary<string, object>() |
| 2006 | + { |
| 2007 | + ["TYPE"] = "FLOAT32", |
| 2008 | + ["DIM"] = "4", |
| 2009 | + ["DISTANCE_METRIC"] = "L2", |
| 2010 | + }); |
| 2011 | + |
| 2012 | + Assert.True(await ft.CreateAsync("my_index", new FTCreateParams().On(IndexDataType.JSON), schema)); |
| 2013 | + |
| 2014 | + } |
| 2015 | + |
1923 | 2016 | [Fact]
|
1924 | 2017 | public void TestModulePrefixs1()
|
1925 | 2018 | {
|
|
0 commit comments