MultiMap / Reduce - 计数= 0?

时间:2012-01-27 01:03:05

标签: mapreduce ravendb

我想为查询创建一个索引,我想在视图中返回一个音频项目列表以及这些项目的统计信息,即TotalDownloads& TotalPlays。

以下是我的相关文档:

Audio
 - Id
 - ArtistName
 - Name

AudioCounter
 - AudioId
 - Type
 - DateTime

这是我目前的索引:

public class AudioWithCounters : AbstractMultiMapIndexCreationTask<AudioWithCounters.AudioViewModel>
{
    public class AudioViewModel
    {
        public string Id { get; set; }
        public string ArtistName { get; set; }
        public string Name { get; set; }
        public int TotalDownloads { get; set; }
        public int TotalPlays { get; set; }
    }

    public AudioWithCounters()
    {
        AddMap<Audio>(audios => from audio in audios
                                select new
                                {
                                    Id = audio.Id,
                                    ArtistName = audio.ArtistName,
                                    Name = audio.Name,
                                    TotalDownloads = 0,
                                    TotalPlays = 0
                                });

        AddMap<AudioCounter>(counters => from counter in counters
                                         where counter.Type == Core.Enums.Audio.AudioCounterType.Download
                                select new
                                {
                                    Id = counter.AudioId,
                                    ArtistName = (string)null,
                                    Name = (string)null,
                                    TotalDownloads = 1,
                                    TotalPlays = 0
                                });

        AddMap<AudioCounter>(counters => from counter in counters
                                         where counter.Type == Core.Enums.Audio.AudioCounterType.Download
                                         select new
                                         {
                                             Id = counter.AudioId,
                                             ArtistName = (string)null,
                                             Name = (string)null,
                                             TotalDownloads = 0,
                                             TotalPlays = 1
                                         });

        Reduce = results => from result in results
                            group result by result.Id
                                into g
                                select new
                                {
                                    Id = g.Key,
                                    ArtistName = g.Select(x => x.ArtistName).Where(x => x != null).First(),
                                    Name = g.Select(x => x.Name).Where(x => x != null).First(),
                                    TotalDownloads = g.Sum(x => x.TotalDownloads),
                                    TotalPlays = g.Sum(x => x.TotalPlays)
                                };
    }
}

然而,我的TotalDownloads&amp; TotalPlays始终为0,即使那里应该有数据。我做错了什么?

1 个答案:

答案 0 :(得分:2)

在reduce功能中,将.First()替换为.FirstOrDefault(),然后就可以了。

除此之外,第二个map-function中还有一个拼写错误,因为你在同一个AudioCounterType.Download上进行过滤。