我上次使用的 Sum 和 Max 调用和使用计数是错误的。它们始终是存储在当前设置为 0 的第二个映射值中的值。我可以将其设为 -100,这就是 reduce 结果。我注意到有人说你应该把它做成一个数组,但我不明白这有什么帮助。我有一个使用 map/reduce 的对象的 linq 表达式,假设这两个映射联合在一起,它可以正常工作。
这是数据=> 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();
并建立一个像
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()
};