2011-12-22 58 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(),