Giter Club home page Giter Club logo

Comments (6)

shacharPash avatar shacharPash commented on August 24, 2024 1

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.

qq312888991 avatar qq312888991 commented on August 24, 2024 1

嘿,你成功了吗?如果是这样,我很乐意关闭该问题。如果没有,请随时写信给我问题所在,我会帮助你。

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.

qq312888991 avatar qq312888991 commented on August 24, 2024

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.

qq312888991 avatar qq312888991 commented on August 24, 2024

1681893984831

from nredisstack.

shacharPash avatar shacharPash commented on August 24, 2024

Did you create an index using the FT.CREATE command?
I don't see it in the code you sent

from nredisstack.

shacharPash avatar shacharPash commented on August 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.