我们有一个包含流媒体视频的网站,我们希望在上周,月份和年份(滚动窗口)显示三个最受关注视频的报告.
每次观看视频时,我们都会在ravendb中存储一个文档:
public class ViewedContent { public string Id { get; set; } public int ProductId { get; set; } public DateTime DateViewed { get; set; } }
我们无法确定如何定义最能支持生成这三个报告的索引/ mapreduces.
我们尝试了以下map / reduce.
public class ViewedContentResult { public int ProductId { get; set; } public DateTime DateViewed { get; set; } public int Count { get; set; } } public class ViewedContentIndex : AbstractIndexCreationTask<ViewedContent,ViewedContentResult> { public ViewedContentIndex() { Map = docs => from doc in docs select new { doc.ProductId,DateViewed = doc.DateViewed.Date,Count = 1 }; Reduce = results => from result in results group result by result.DateViewed into agg select new { ProductId = agg.Key,Count = agg.Sum(x => x.Count) }; } }
var lastSevenDays = session.Query<ViewedContent,ViewedContentIndex>() .Where( x => x.DateViewed > DateTime.UtcNow.Date.AddDays(-7) );
Error: “DateViewed is not indexed”
最终,我们想查询类似于:
var lastSevenDays = session.Query<ViewedContent,ViewedContentIndex>() .Where( x => x.DateViewed > DateTime.UtcNow.Date.AddDays(-7) ) .GroupBy( x => x.ProductId ) .OrderBy( x => x.Count )
这实际上并没有编译,因为OrderBy是错误的; Count在这里不是有效的属性.
任何帮助在这里将不胜感激.
解决方法
如果你在sql土地上,每个报告都是一个不同的GROUP BY,它告诉你需要三个索引 – 一个只有月份,一个按周,一个月,一个一年(或者可能略有不同,取决于关于你实际上将如何进行查询.
现在,你有一个DateTime – 这会出现一些问题 – 你真正想要做的是索引DateTime的Year组件,日期时间的Month组件和该日期时间的Day组件. (或者只是其中的一个或两个,具体取决于您要生成哪个报告.
我只是在这里引用你的代码,所以很明显它不会编译,但是:
public class ViewedContentIndex : AbstractIndexCreationTask<ViewedContent,ViewedContentResult> { public ViewedContentIndex() { Map = docs => from doc in docs select new { doc.ProductId,Day = doc.DateViewed.Day,Month = doc.DateViewed.Month,Year = doc.DateViewed.Year Count = 1 }; Reduce = results => from result in results group result by new { doc.ProductId,doc.DateViewed.Day,doc.DateViewed.Month,doc.DateViewed.Year } into agg select new { ProductId = agg.Key.ProductId,Day = agg.Key.Day,Month = agg.Key.Month,Year = agg.Key.Year Count = agg.Sum(x => x.Count) }; }
}
希望你可以通过这个看到我想要实现的目标 – 你想要你的组中的所有组件,因为它们使你的分组独特.
我不记得RavenDB是否允许你用DateTimes做这个,而我还没有在这台计算机上得到它,所以不能验证这一点,但理论仍然是一样的.
所以,要重新迭代
您希望按周产品ID为报表编制索引
您希望按月产品ID为报表编制索引
您希望按年份产品ID为报表编制索引
我希望这有帮助,对不起我不能给你一个可编辑的例子,缺乏乌鸦使它有点困难:-)