简体   繁体   中英

RavenDB Query Aggregate Properties in Map/Reduce Index

I have a map/reduce index that looks like this (as defined on the server).

Map:

from request in docs.Master_Requests
    select new
    {
        DocumentID = Guid.NewGuid(),
        Year = request.Timestamp.Year,
        Month = request.Timestamp.Month,
        Day = request.Timestamp.Day,
        Hour = request.Timestamp.Hour,
        Minute = request.Timestamp.Minute,
        RequestBytes = request.RequestBytes,
        ResponseBytes = request.ResponseBytes,
        TotalRequests = 1
    }

Reduce:

from result in results
group result by new { result.Year, result.Month, result.Day, result.Hour, result.Minute }
    into g
    select new
    {
       DocumentID = Guid.NewGuid(),
       Year = g.Key.Year,
       Month = g.Key.Month,
       Day = g.Key.Day,
       Hour = g.Key.Hour,
       Minute = g.Key.Minute,
       RequestBytes = g.Sum(r => r.RequestBytes),
       ResponseBytes = g.Sum(r => r.ResponseBytes),
       TotalRequests = g.Sum(r => r.TotalRequests)
    }

I'm trying to query on the results of the map/reduce index like so:

session.Query<UsageAggregate>("RequestAggregateByMinute", true)
                                .Where(a => a.TotalRequests >= 1);

However, I am getting no results back when there are many results (as shown in the studio) that have a TotalRequests property greater than 1. I get results when I don't include a where predicate and I also get results when I filter on any other property than the aggregated properties (RequestBytes, ResponseBytes, and TotalRequests).

Can someone help me understand what I'm doing wrong?

Ok, I figured it out. For whatever reason, I needed to cast the result of the Sum() like so. This must have to do with the dynamic compilation once it's sent to the raven server.

from result in results
group result by new { result.Year, result.Month, result.Day, result.Hour, result.Minute }
    into g
    select new
    {
       DocumentID = Guid.NewGuid(),
       Year = g.Key.Year,
       Month = g.Key.Month,
       Day = g.Key.Day,
       Hour = g.Key.Hour,
       Minute = g.Key.Minute,
       RequestBytes = (Int32)g.Sum(r => r.RequestBytes),
       ResponseBytes = (Int32)g.Sum(r => r.ResponseBytes),
       TotalRequests = (Int32)g.Sum(r => r.TotalRequests)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM