Skip to content

Wrong query when aggregating with filter #230

@lem0na

Description

@lem0na

Thanks for reporting an issue in NRedisStack! Please update the appropriate text below, as much data as possible really helps!

NRedisStack Version: 0.11.0

Redis Stack Version: redis_version:7.2.3 from official docker image
MODULE LIST
name timeseries ver 11009 path /opt/redis-stack/lib/redistimeseries.so args
name redisgears_2 ver 20014 path /opt/redis-stack/lib/redisgears.so args v8-plugin-path /opt/redis-stack/lib/libredisgears_v8_plugin.so
name search ver 20809 path /opt/redis-stack/lib/redisearch.so args MAXSEARCHRESULTS 10000 MAXAGGREGATERESULTS 10000
name bf ver 20608 path /opt/redis-stack/lib/redisbloom.so args
name RedisCompat ver 1 path /opt/redis-stack/lib/rediscompat.so args
name ReJSON ver 20607 path /opt/redis-stack/lib/rejson.so args

Description: I am trying to evaluate Redis for aggregations. I saved a few JSON records and tried to aggregate using filter on one of the properties. The generated query throws an exception that a property is not loaded or in the pipeline.

example code

using NRedisStack.Search;
using NRedisStack;
using StackExchange.Redis;
using NRedisStack.RedisStackCommands;
using NRedisStack.Search.Literals.Enums;
using RedisTest3;
using NRedisStack.Search.Aggregation;
using NRedisStack.Literals.Enums;
using static NRedisStack.Search.Query;
using System.Text.RegularExpressions;


string session = "user";

ConfigurationOptions options = new ConfigurationOptions
{
    EndPoints = {
            { "localhost", 6379 },
        },
};

ConnectionMultiplexer cluster = ConnectionMultiplexer.Connect(options);
IDatabase db = cluster.GetDatabase();
SearchCommandBuilder.DropIndex("idx:users", dd: true);
db.Execute("FLUSHALL");

ISearchCommands ft = db.FT();
IJsonCommands json = db.JSON();

var schema = new Schema()
    .AddNumericField(new FieldName("$.UserId", "UserId"), sortable: true)
    .AddTagField(new FieldName("$.CreatedDay", "CreatedDay"))
    .AddNumericField(new FieldName("$.StatusId", "StatusId"), sortable: true);

ft.Create(
    "idx:users",
    new FTCreateParams().On(IndexDataType.JSON).Prefix($"{session}:"),
    schema);

var parameters = FTCreateParams.CreateParams().Filter("@StatusId>1").Prefix($"{session}:");
ft.Create("idx:users2", parameters, schema);

BacktestResultRedis res1 = new BacktestResultRedis
{
    CreatedDay = "2023-10-01",
    StatusId = 1,
    UserId = 1000
};

BacktestResultRedis res2 = new BacktestResultRedis
{
    CreatedDay = "2023-11-01",
    StatusId = 2,
    UserId = 1001
};

BacktestResultRedis res3 = new BacktestResultRedis
{
    CreatedDay = "2023-10-01",
    StatusId = 3,
    UserId = 1002
};

BacktestResultRedis res4 = new BacktestResultRedis
{
    CreatedDay = "2023-12-01",
    StatusId = 2,
    UserId = 1003
};

BacktestResultRedis res5 = new BacktestResultRedis
{
    CreatedDay = "2023-12-11",
    StatusId = 1,
    UserId = 1003
};

json.Set($"{session}:1", "$", res1);
json.Set($"{session}:2", "$", res2);
json.Set($"{session}:3", "$", res3);
json.Set($"{session}:4", "$", res4);
json.Set($"{session}:5", "$", res5);

var request = new AggregationRequest("*", 3).Filter("@StatusId==1")
                .GroupBy("@CreatedDay", Reducers.CountDistinct("@UserId"), Reducers.Count().As("count")) ;
var result = ft.Aggregate("idx:users", request);
    public class BacktestResultRedis
    {
        public string CreatedDay { get; set; }
        public byte StatusId { get; set; }
        public long UserId { get; set; }
    }

I expect the generated query to be

FT.AGGREGATE idx:users * FILTER @StatusId==1 GROUPBY 1 @CreatedDay REDUCE COUNT_DISTINCT 1 @UserId REDUCE COUNT 0 AS count DIALECT 3

which executed in the CLI returns expected results.
But the actual query is

FT.AGGREGATE idx:users * GROUPBY 1 @CreatedDay REDUCE COUNT_DISTINCT 1 @UserId REDUCE COUNT 0 AS count FILTER @StatusId==1 DIALECT 3

which executed in the CLI throws an error "Property StatusId not loaded nor in pipeline".

In AggregationRequest class I see that there is a function SerializeRedisArgs which looks like generates the query with a specific order of commands which probably is the issue.

I am doing something wrong or this is a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions