--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yeah. I think you need two m/r indexes.
--
Map = docs => from doc in docs
select new PointDetailReduce
{ AccountId = doc.AccountId, RetailerId = doc.RetailerId,
Count = 1, CreateDt = doc.CreateDt,
TotalPoints = doc.Points }; Reduce = results => results.GroupBy (r => new { r.AccountId, r.RetailerId }) .Select (r => new PointDetailReduce { AccountId = r.Key.AccountId, RetailerId=r.Key.RetailerId,
Count=r.Sum (x => x.Count), CreateDt = r.Max(x=>x.CreateDt), TotalPoints = r.Sum(x=>x.TotalPoints) } );
from result in results
group result by result.RetailerId into r
let high = r.GroupBy (p => p.AccountId).Select (p => p.Sum (x => x.TotalPoints)).OrderByDescending (p => p).FirstOrDefault ( )
select new
{
RetailerId=r.Key,
HighPoints = high,
Count = r.Sum (x => x.Count),
TotalPoints = r.Sum (x => x.TotalPoints),
AveragePoints = r.Sum (x => x.TotalPoints)/r.Sum (x => x.Count)
}
public class GroupByRetailerTransformer : AbstractTransformerCreationTask<PointDetailReduce>
{
public GroupByRetailerTransformer()
{
TransformResults = results => from result in results
group result by result.RetailerId into r
select new
{
RetailerId=r.Key,
HighPoints = r.Max(x=>x.TotalPoints),
Count = r.Sum (x => x.Count),
TotalPoints = r.Sum (x => x.TotalPoints),
AveragePoints = (double)r.Sum (x => x.TotalPoints)/(double)r.Sum (x => x.Count)
};
}
}
var q = MvcApplication.CurrentSession.Query<BrandPointReduce>("PointDetail/ByAccountGroupedByRetailer")
.TransformWith<GroupByRetailerTransformer, BrandPointReduce>()
.Where(r => r.RetailerId.In(likes));
var brandstats = new List<BrandPointReduce>();
using (var enumerator = MvcApplication.CurrentSession.Advanced.Stream<BrandPointReduce>(q))
{
while (enumerator.MoveNext())
{
BrandPointReduce pd = enumerator.Current.Document;
brandstats.Add(pd);
//storepts(acct);
}
}
Great!
--
var q = MvcApplication.CurrentSession.Query<BrandPointReduce>("PointDetail/ByAccountGroupedByRetailer") .TransformWith<GroupByRetailerTransformer, BrandPointReduce>()
.Where(r => r.RetailerId.In(new List<string>{"xxxxx","xxxxx","xxxxx","xxxxxx"}));
using (var enumerator = MvcApplication.CurrentSession.Advanced.Stream<BrandPointReduce>(q)) {
while (enumerator.MoveNext()) { BrandPointReduce pd = enumerator.Current.Document; } }
The streaming API....streams in batches. There may be a parameter to tune the batch size.
--
--
var q = MvcApplication.CurrentSession.Query<BrandPointReduce>("PointDetail/ByAccountGroupedByRetailer")
.TransformWith<GroupByRetailerTransformer, BrandPointReduce>()
using (var enumerator = MvcApplication.CurrentSession.Advanced.Stream<BrandPointReduce>(q))
{
while (enumerator.MoveNext())
{
BrandPointReduce pd = enumerator.Current.Document;
brandstats.Add(pd);
}
}
--