4

刚刚升级到 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)
                                               });
    }
}
4

2 回答 2

6

禁止可能受减少批次大小(例如计数和平均)影响的聚合,因为它们会产生错误的结果。您可能已经能够在 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
                                               });
    }
}
于 2013-01-09T13:48:16.520 回答
2

这是问题 RavenDB-783。这是自 v2.0 以来的预期行为。

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

于 2013-01-09T13:16:12.920 回答