Search code examples
stackexchange.redisredisearch

NRediSearch - Getting total documents matched count


Is there a way to get a total results count when calling Aggregate function?

Note that I'm not using Aggregate function to aggregate results, but as an advanced search query, because Search function does not allow to sort by multiple fields.

RediSearch returns total documents matched count, but I can't find a way to get this number using NRediSearch library.


Solution

  • With NRediSearch

    Using NRediSearch, you would need to build and execute aggregation that will run a GROUPBY 0 and the COUNT reducer, say you have a person-idx index and you want to count all the Person documents in Redis:

    var client = new Client("person-idx", muxer.GetDatabase());
    var result = await client.AggregateAsync(new AggregationBuilder().GroupBy(new List<string>(), new List<Reducer>{Reducers.Count()}));
    Console.WriteLine(result.GetResults().First().Values.First());
    

    Will get the count you are looking for.

    With Redis.OM

    There's a newer library Redis.OM which you can also use to make these aggregations a bit simpler, the same operation would be done with the following:

    var peopleAggregations = provider.AggregationSet<Person>();
    Console.WriteLine(peopleAggregations.Count());