Raven DB:这个multimap / reduce索引出了什么问题?

时间:2012-09-20 01:44:40

标签: c# mapreduce ravendb

对上次使用和使用次数的Sum和Max调用是错误的。它们始终是存储在第二个映射值中的值,该值当前设置为0.我可以使其为-100,这就是reduce结果。我注意到有人说你应该把它变成一个阵列,但我不明白这有多大帮助。假设两个地图联合在一起,我使用map / reduce的对象上有一个linq表达式。

这是data => https://gist.github.com/940ccca1b0f8917e9eaf

我想支持像

这样的查询
        var stats = Session.Query<MultiMapApiKeyStats.ApiKeyStats, MultiMapApiKeyStats>()
            .Customize(x => x.WaitForNonStaleResultsAsOfNow())
            .Where(x => x.AccountId == Account.Id)
            .OrderBy(x => x.Key).ToList();

并构建一个表格 table

 public MultiMapApiKeyStats()
        {
            AddMap<KeyUsageBase>(uses => from use in uses
                                         select new
                                         {
                                             AccountId = use.AccountId,
                                             ApiKeyId = use.ApiKeyId,
                                             Key = (string)null,
                                             UsageCount = 1,
                                             LastUsed = use.LastUsedTicks,
                                             Pattern = (string)null,
                                             Status = ApiKey.KeyStatus.None,
                                             Type = ApiKey.ApplicationType.None
                                         });
            AddMap<ApiKey>(keys => from key in keys
                                   select new
                                          {
                                              AccountId = key.AccountId,
                                              ApiKeyId = key.Id,
                                              Key = key.Key,
                                              UsageCount = 0,
                                              LastUsed = 0,
                                              Pattern = key.Pattern,
                                              Status = key.ApiKeyStatus,
                                              Type = key.Type
                                          });


            Reduce = results => from result in results
                                group result by result.ApiKeyId
                                    into g
                                    select new
                                           {
                                           AccountId = g.Select(x => x.AccountId).FirstOrDefault(),
                                           ApiKeyId = g.Key,
                                           Key = g.Select(x => x.Key).FirstOrDefault(x => x != null),
                                           UsageCount = g.Sum(x => x.UsageCount),
                                           LastUsed = g.Max(x => x.LastUsed),
                                           Pattern = g.Select(x => x.Pattern).FirstOrDefault(),
                                           Status = g.Select(x => x.Status).FirstOrDefault(),
                                           Type = g.Select(x => x.Type).FirstOrDefault()
                                           };

1 个答案:

答案 0 :(得分:2)

索引错误sequence contains no matching elements是在空集合上调用First的结果,这就是您应该使用FirstOrDefault的原因。此外,在map和reduce声明中引用自己的类将导致错误,因为RavenDB无法在索引时解析这些类型。