2011-12-22 56 views
4

我读过RavenDB的Ayende的blog post on the multi map feature,并试图实现它。我无法让它通过。我有什么是基本相同的博客文章的例子:在RavenDb中有多重映射/减少工作吗?

class RootDocument { 
    public string Id { get; set; } 

    public string Foo { get; set; } 
    public string Bar { get; set; } 
} 

public class ChildDocument { 
    public string Id { get; set; } 

    public string RootId { get; set; } 
    public int Value { get; set; } 
} 

class RootsByIdIndex: AbstractMultiMapIndexCreationTask<RootsByIdIndex.Result> { 
    public class Result { 
     public string Id { get; set; } 
     public string Foo { get; set; } 
     public string Bar { get; set; } 
     public int Value { get; set; } 
    } 

    public RootsByIdIndex() { 
     AddMap<ChildDocument>(children => from child in children 
              select new { 
               Id = child.RootId, 
               Foo = (string)null, 
               Bar = (string)null, 
               Value = child.Value 
              }); 
     AddMap<RootDocument>(roots => from root in roots 
             select new { 
              Id = root.Id, 
              Foo = root.Foo, 
              Bar = root.Bar, 
              Value = 0 
             }); 
     Reduce = results => from result in results 
          group result by result.Id into g 
          select new { 
           Id = g.Key, 
           Foo = g.Select(x => x.Foo).Where(x => x != null).First(), 
           Bar = g.Select(x => x.Bar).Where(x => x != null).First(), 
           Value = g.Sum(x => x.Value) 
          }; 
    } 
} 

查询在索引总是给我的价值属性的值0。有一点摆弄索引使得看起来ChildDocument的地图从不检索任何文档。

这个工作应该在当前稳定的RavenDB构建(1.0.573)中吗?还是我做错了?

回答

4

索引的缩小部分在字段Foo和Bar中是错误的。

在第一个Map函数中,您将Foo和Boo设置为null,因为所有Map函数输出的结构在MultiMap Index中必须完全相同。您必须使用FirstOrDefault()代替First()

Foo = g.Select(x => x.Foo).Where(x => x != null).FirstOrDefault(), 
Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(),