using Raven.Abstractions.Indexing;
using Raven.Client.Indexes;
public class CategoryProductCount : AbstractIndexCreationTask<Product, CategoryCount>
{
public CategoryProductCount()
{
Map = docs => from doc in docs
select new
{
CategoryId = doc.CategoryId,
Count = 1
};
Reduce = results => from result in results
group result by result.CategoryId
into g select new { CategoryId = g.Key, Count = g.Sum(x => x.Count) };
Index(x => x.CategoryId, FieldIndexing.Analyzed);
}
}
public class CategoryCount
{
public int CategoryId { get; set; }
public int Count { get; set; }
}
I wanted to test this out in code as follows:-
var results = _session.Query<Product>("CategoryProductCount").Where(x => x.CategoryId == "7").ToList();
I know there are 3 records with a CategoryId of 7 but I always get 1 result, doing something silly but cant see what, any help would be appreciated.