Reduce在分组中不能包含Average()方法

时间:2013-01-09 11:07:58

标签: c# .net ravendb

刚升级到v2,这不再有效;如果我尝试使用Count()

,我会收到类似的错误
public class Deck_Ratings : AbstractIndexCreationTask<DeckRating, Deck_Ratings.ReduceResult>
{
    public class ReduceResult
    {
        public string DeckId { get; set; }
        public int Rating { get; set; }
    }

    public Deck_Ratings()
    {
        Map = deckRatings => deckRatings.Select(deckRating => new 
                             { 
                                 deckRating.DeckId, 
                                 deckRating.Rating 
                             });
        Reduce = reduceResults => reduceResults
                                      .GroupBy(reduceResult => reduceResult.DeckId)
                                      .Select(grouping => new 
                                              {
                                                  DeckId = grouping.Key, 
                                                  Rating = grouping.Average(reduceResult => reduceResult.Rating)
                                               });
    }
}

2 个答案:

答案 0 :(得分:4)

禁止可能受reduce批量大小影响的聚合(例如Count和Average),因为它们会产生错误的结果。您可能已经能够在1.0下使用它,但您的平均值可能是错误的,除非您拥有的项目非常少,以至于它们都在一个减少批次中完成。要了解有关减少批次的更多信息,请阅读Map / Reduce - A Visual Explanation

您必须通过将每个项目的总和加1来计算项目。您必须通过将值的总和作为总和,将1的总和作为计数,然后除以它们来平均项目。

public class Deck_Ratings : AbstractIndexCreationTask<DeckRating, Deck_Ratings.ReduceResult>
{
    public class ReduceResult
    {
        public string DeckId { get; set; }
        public int TotalRating { get; set; }
        public int CountRating { get; set; }
        public double AverageRating { get; set; }
    }

    public Deck_Ratings()
    {
        Map = deckRatings => deckRatings.Select(deckRating => new 
                             { 
                                 deckRating.DeckId,
                                 TotalRating = deckRating.Rating,
                                 CountRating = 1,
                                 AverageRating = 0
                             });
        Reduce = reduceResults => reduceResults
                                      .GroupBy(reduceResult => reduceResult.DeckId)
                                      .Select(grouping => new 
                                              {
                                                  DeckId = grouping.Key, 
                                                  TotalRating = grouping.Sum(reduceResult => reduceResult.TotalRating)
                                                  CountRating = grouping.Sum(reduceResult => reduceResult.CountRating)
                                               })
                                      .Select(x => new
                                               {
                                                  x.DeckId,
                                                  x.TotalRating,
                                                  x.CountRating,
                                                  AverageRating = x.TotalRating / x.CountRating
                                               });
    }
}

答案 1 :(得分:2)

This is issue RavenDB-783。这是自v2.0以来的预期行为。

不确定他推荐什么作为替代方案。