RavenDB复杂的MapReduce索引缺少输出

时间:2011-10-12 16:56:31

标签: nosql mapreduce ravendb

这是this post项目的延续。

我有以下型号:

public class Product
{
  public string Id { get; set; }
  public int CategoryId { get; set; }
  public Dictionary<string, string> Specs { get; set; }
}

我想创建一个map reduce索引,它按CategoryId对产品进行分组,然后聚合所有规范名称 - 值对。 reduce函数的输出应为:

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; }
}

这是为了支持对产品集合的分面搜索。输出类似于RavenDB faceted search feature中的FacetSetup文档。以下是索引定义:

    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)
                                                               }
                                                  }
                                 };
    }
}

保存此索引后,索引过程似乎运行但输出缺少“SpecGroups”属性:

{ 
  "CategoryId": "123"
}

我对此索引的意图是优化分面搜索功能。仅按类别ID查询时,我可以使用此索引的输出返回构面结果,然后在按其他构面过滤时,可以使用此索引获取用于计算过滤结果集中构面的术语。

我正在使用RavenDB Build 495.

1 个答案:

答案 0 :(得分:1)

我能够通过改变模型来完成这项工作:

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; }
}

使用此模型,问题中声明的MapReduce索引按预期工作,也很快!我认为根本问题在于编译映射定义后处理字典的方式。