Try this:
public class DailyStats : AbstractIndexCreationTask<Incident, DateStat>
{
public DailyStats()
{
Map = docs =>
from doc in docs
select new
{
Date = doc.OccuredOn,
IncidentsByHour = new Dictionary<int, int> { { doc.OccuredOn.Hour, 1 } }
};
Reduce = mapped =>
from m in mapped
group m by new { m.Date.Date }
into g
select new
{
Date = g.Key,
IncidentsByHour = g.SelectMany(x => x.IncidentsByHour)
.GroupBy(x => x.Key)
.OrderBy(x => x.Key)
.ToDictionary(x => x.Key, x => x.Sum(y => y.Value))
};
}
}
The only difference here is that you won't get any items in your dictionary for hours that have no incidents.
There is indeed some kind of bug with Raven still. The map should be able to be written with this:
IncidentsByHour = Enumerable.Range(0, 24)
.ToDictionary(h => h, h => doc.OccuredOn.Hour == h ? 1 : 0)
But it fails for some strange reason. I'll report that as a bug.
And yes, it is usually better to have fewer larger indexes than many small ones.