【问题标题】:RavenDB Map/Reduce with grouping by dateRavenDB Map/Reduce 按日期分组
【发布时间】:2012-08-07 16:27:26
【问题描述】:

我必须创建一个查询以获取每年/每月发布的统计信息,例如按日期分组。我创建了一个索引:

public class Posts_Count : AbstractIndexCreationTask<Post, ArchiveItem>
{
    public Posts_Count()
    {
        Map = posts => from post in posts
              select new
                         {
                             Year = post.PublishedOn.Year,
                             Month = post.PublishedOn.Month,
                             Count = 1
                         };

        Reduce = results => from result in results
                            group result by new {
                                 result.Year,
                                 result.Month
                            }
                            into agg
                            select new
                                       {                                               
                                           Year = agg.Key.Year,
                                           Month = agg.Key.Month,
                                           Count = agg.Sum(x => x.Count)
                                       };
    }
}

在工作室中,我有下一个 map 和 reduce 函数:

地图:

docs.Posts.Select(post => new {Year = post.PublishedOn.Year, Month = post.PublishedOn.Month, Count = 1})

减少:

results
.GroupBy(result => new {Year = result.Year, Month = result.Month})
.Select(agg => new {Year = agg.Key.Year, Month = agg.Key.Month, Count = agg.Sum(x => ((System.Int32)(x.Count)))})

但问题是我总是得到一个空值的年份和月份属性:

{
   "Year": null,
   "Month": null,
   "Count": "1"
}

谁能帮我解决我的代码问题?谢谢!

【问题讨论】:

    标签: mapreduce ravendb


    【解决方案1】:

    您的代码看起来不错。我对其进行了测试,它在当前不稳定的版本 1.2.2096 中工作。最近在 RavenDB google group 上对此进行了一些讨论,所以它之前可能被破坏了。使用当前版本重试,看看它现在是否适合您。

    【讨论】:

    • hmm,上面的索引看起来不错,但我们不需要为每个项目定义 SortOption 吗?比如Sort(x =&gt; x.Year, SortOptions.Int); Sort(x =&gt; x.Month, SortOptions.Int); Sort(x =&gt; x.Count, SortOptions.Int);?否则,我们不会得到排序不一致的结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多