RavenDB complex MapReduce index missing output
This is a continuation of the project from this post.
I have the following model:
public class Product
{
public string Id { get; set; }
public int CategoryId { get; set; }
public Dictionary<string, string> Specs { get; set; }
}
And I would like to create a map reduce index which groups products by CategoryId and then aggregates all the spec name-value pairs. The output of the reduce function should be this:
public class CategorySpecGroups
{
public int CategoryId { get; set; }
public SpecGroup[] SpecGroups { get; set; }
}
public class SpecGroup
{
public string Name { get; set; }
public SpecGroupValue[] Values { get; set; }
}
public class SpecGroupValue
{
public string Value { get; set; }
public int Count { get; set; }
}
This is to supported faceted search over the collection of products. The output is similar to the FacetSetup document from the RavenDB faceted search feature. The following is the index definition:
public class SpecGroups_ByCategoryId : AbstractIndexCreationTask<Product, CategorySpecGroups>
{
public SpecGroups_ByCategoryId()
{
this.Map = products => from product in products
where product.Specs != null
select new
{
CategoryId = product.CategoryId,
SpecGroups = from spec in product.Specs
select new
{
Name = spec.Key,
Values = new dynamic[] { new { Value = spec.Value, Count = 1 } }
}
};
this.Reduce = results => from categorySpecGroups in results
group categorySpecGroups by categorySpecGroups.CategoryId into g
select new
{
CategoryId = g.Key,
SpecGroups = from categorySpecGroups in g
from specGroup in categorySpecGroups.SpecGroups
group specGroup by specGroup.Name into g2
select new
{
Name = g2.Key,
Values = from specGroup in g2
from specGroupValue in specGroup.Values
group specGroupValue by specGroupValue.Value into g3
select new
{
Value = g3.Key,
Count = g3.Sum(x => x.Count)
}
}
};
}
}
After saving this index, the indexing process seems to run however the output is missing the "SpecGroups" property:
{
"CategoryId": "123"
}
My intention with this index is to optimize the faceted search feature. When querying by only category ID I can use the output of this index to return facet results, then when filtering by additional facets, this index can be used to obtain the term开发者_高级运维s to use to calculate facets in the filtered result set.
I am using RavenDB Build 495.
I was able to make this work by changing the model a bit:
public class Product
{
public string Id { get; set; }
public int CategoryId { get; set; }
public ProductSpec[] Specs { get; set; }
}
public class ProductSpec
{
public string Key { get; set; }
public string Value { get; set; }
}
With this model, the MapReduce index as declared in the question works as expected, and is quick too! I think the underlying issue is with the way dictionaries are handled after the map definition has been compiled.
精彩评论