That was the main issue. I also discovered that queries against
Visibility weren't working, but fixed that by changing it a string.
Now, I can't seam to get the query statistics to return the proper
TotalResults count. Here's my current index:
OwnerId = g.Select(x =>
x.OwnerId).Where(x => x != null).FirstOrDefault(),
Visibility = g.Select(x =>
x.Visibility).Where(x => x != null).FirstOrDefault(),
CreatedAt = g.Max(x =>
(DateTimeOffset)x.CreatedAt),
UpdatedAt = g.Max(x =>
(DateTimeOffset)x.UpdatedAt),
NumberOfFavorites = g.Sum(x =>
x.NumberOfFavorites),
NumberOfRatings = g.Sum(x =>
x.NumberOfRatings),
AverageRating = g.Sum(x =>
x.AverageRating) / (g.Sum(x => x.NumberOfRatings) > 0 ? g.Sum(x =>
x.NumberOfRatings) : 1)
};
}
}
And here's how it's being used:
public Lazy<Page<CharacterOverview>> ForUser(IUser user, int? limit,
int? offset) {
var query = session.Query<CharacterConcensus,
CharacterOpinion_Concensus>()
.Include<CharacterConcensus>(x => x.CharacterId)
.OrderByDescending(x => x.UpdatedAt)
.Where(x => x.OwnerId == user.Id)
.ToLazyPage(limit, offset);
return new Lazy<Page<CharacterOverview>>(() => {
var page = query.Value;
return new Page<CharacterOverview> {
Asc = page.Asc,
Limit = page.Limit,
Offset = page.Offset,
OrderBy = page.OrderBy,
TotalResults = page.TotalResults,
Items = page.Items.Select(x => new CharacterOverview {
Character = session.Load<Character>(x.CharacterId),
Concensus = x
}).ToList()
};
});
}
ToLazyPage is an extension method that looks like this:
public static Lazy<Page<T>> ToLazyPage<T>(this IRavenQueryable<T>
query, int? limit = 10, int? offset = 0, string orderby = null, bool?
asc = false) {
var paginationInfo = new PaginationInformation(limit, offset);
RavenQueryStatistics stats;
var pagedQuery = query
.Statistics(out stats)
.Skip(paginationInfo.Offset)
.Take(paginationInfo.Limit)
.Lazily();
return new Lazy<Page<T>>(() => {
return new Page<T> {
Items = pagedQuery.Value.ToList(),
Limit = paginationInfo.Limit,
Offset = paginationInfo.Offset,
TotalResults = stats.TotalResults,
OrderBy = orderby,
Asc = asc.GetValueOrDefault()
};
});
}
This is batched with some other queries. It's all working except the
statistics.
On Oct 4, 2:23 pm, Ayende Rahien <
aye...@ayende.com> wrote:
> The sequence error is probably because of the average, you have to be
> prepared for a set of results where the AverageRating is all zero
>