我必须创建一个查询以获取每年/每月发布的统计信息,例如按日期分组。我创建了一个索引:
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 和 Month 属性的空值:
{
"Year": null,
"Month": null,
"Count": "1"
}
有人可以帮我解决我的代码问题吗?谢谢你!