REDIS OM .NET - InvalidOperationException when grouping

201 views Asked by At

I keep getting this exception when performing a simple linq group query using the Redis OM library:

System.InvalidOperationException: 'Searches can only be performed on objects decorated with a RedisObjectDefinitionAttribute that specifies a particular index'

My code:

Agents = (RedisCollection<Agent>)RedisProvider.RedisCollection<Agent>();
var groups = Agents.GroupBy(x => x.AccountId);
foreach (var group in groups)
{
   Console.WriteLine($"{group.Key}");
}

My indexed model:

[Document(StorageType = StorageType.Json, Prefixes = new[] { "Agent" })]
public class Agent
{
        [RedisIdField]
        [Indexed] public string Id { get; set; }

        [Indexed(Sortable = true, Aggregatable = true)]
        public int AccountId { get; set; }
}

The index is created in Redis and I am able to perform Where() queries in Linq successfully on the same redis collection, interestingly group.Count() also works correctly. It is just once I try to iterate over group items it throws this exception.

I've done some Googling but cannot find a solution or info on the 'Redis Object Definition Attribute' the exception.

1

There are 1 answers

0
slorello On BEST ANSWER

The issue is that you are trying to run a GroupBy in Redis against the RedisCollection, that's not a valid operation. You can materialize your collection (call ToList or some other enumeration on it), prior to running the GroupBy, or you can run a GroupBy in the context of a RedisAggregationSet, which will allow you to do run reductions against it.

The tradeoff here is if you want to run the GroupBy post-materialization, you have to pull the whole result set back and have your app cobble everything into a group. If you do it within the context of the RedisAggregationSet, all the processing can be done server side.