Map predicate or abbreviation (RavenDb)?

If I want to apply a predicate to a document before aggregating in the Decrease function, do I want to place this predicate in the Map function or the Decrease function?

So, for example, a predicate in a Map function would look like this:

        Map = orders => orders
            .Where(order => order.Status != OrderStatus.Cancelled)
            .Select(order => new
                {
                    Name = order.Firstname + ' ' + order.Lastname,
                    TotalSpent = order.Total,
                    NumberOfOrders = 1
                });
        Reduce = results => results
            .GroupBy(result => result.Email)
            .Select(customer => new
                {
                    Name = customer.Select(c => c.Name).FirstOrDefault(),
                    TotalSpent = customer.Sum(c => c.TotalSpent),
                    NumberOfOrders = customer.Sum(c => c.NumberOfOrders)
                });

      

And including it in the "Reduce" function would look like this:

        Map = orders => orders
            .Select(order => new
                {
                    Name = order.Firstname + ' ' + order.Lastname,
                    TotalSpent = order.Total,
                    NumberOfOrders = 1,
                    Status = order.Status
                });
        Reduce = results => results
            .Where(order => order.Status != OrderStatus.Cancelled)
            .GroupBy(result => result.Email)
            .Select(customer => new
                {
                    Name = customer.Select(c => c.Name).FirstOrDefault(),
                    TotalSpent = customer.Sum(c => c.TotalSpent),
                    NumberOfOrders = customer.Sum(c => c.NumberOfOrders),
                    Status = (OrderStatus)0
                });

      

The latter obviously makes more sense, however that means I have to add the Status property to the Reduce result class and then just set it to some unknown value in Reduce, since it doesn't really mean anything.

+3


source to share


1 answer


For map / cut, only the first approach works. And no, the order will be ignored and you cannot do something like FirstOrDefault in the result.



You need to think of the map / pruning as two independent functions, whereas the pruning function can be executed multiple times on the same input, so the input format must match the output format. It can also happen on different servers in parallel and asynchronous, so new documents can be saved while indexing is running.

+4


source







All Articles