2011-12-26 45 views
0

我與RavenDB的多地圖奮力/減少的概念,最近問this question關於如何正確地寫多的map/reduce指數。IList <T>多圖/縮小結果?

我得到了這個問題的工作簡單的指標,但是,當我試圖使它有點複雜,我不能使它發揮作用。我想要做的是有指標的結果包含字符串列表,即:

class RootDocument { 
    public string Id { get; set; } 
    public string Foo { get; set; } 
    public string Bar { get; set; } 
    public IList<string> Items { 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 IList<string> Items { 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, 
      Items = default(IList<string>), 
      Value = child.Value 
     }); 
     AddMap<RootDocument>(
     roots => from root in roots 
      select new { 
      Id = root.Id, 
      Foo = root.Foo, 
      Bar = root.Bar, 
      Items = root.Items, 
      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).FirstOrDefault(), 
       Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(), 
       Items = g.Select(x => x.Items).Where(
       x => x != default(IList<string>).FirstOrDefault(), 
       Value = g.Sum(x => x.Value) 
       }; 
    } 
} 

基本上我想設置的項目屬性爲默認(IList的)映射ChildDocuments和價值時, RootDocument的Items屬性。然而,這不起作用。它提供錯誤消息

錯誤的請求無法理解查詢:

- 2號線山坳285:無效Expr的

- 2號線山坳324:無法解析雙.0.0

上傳索引時。我如何處理多地圖/縮小索引中的列表?

回答

3

不要在索引中使用List,而應使用數組。

AddMap<ChildDocument>(
    children => from child in children 
    select new { 
     Id = child.RootId, 
     Foo = (string)null, 
     Bar = (string)null, 
     Items = new string[0], 
     Value = child.Value 
    }); 
    AddMap<RootDocument>(
    roots => from root in roots 
     select new { 
     Id = root.Id, 
     Foo = root.Foo, 
     Bar = root.Bar, 
     Items = root.Items, 
     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).FirstOrDefault(), 
      Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(), 
      Items = g.SelectMany(x=>x.Items), 
      Value = g.Sum(x => x.Value) 
      }; 
2

大衛,你要明白,RavenDB存儲其指標與Lucene.NET。這意味着,您的索引中不能包含任何複雜的.net類型。

在你的榜樣,我建議你用一個簡單的string而不是IList<string>。然後,您可以加入您的字符串項目:

AddMap<ChildDocument>(
    children => from child in children 
    select new { 
     Id = child.RootId, 
     Foo = (string)null, 
     Bar = (string)null, 
     Items = (string)null, 
     Value = child.Value 
    }); 
    AddMap<RootDocument>(
    roots => from root in roots 
     select new { 
     Id = root.Id, 
     Foo = root.Foo, 
     Bar = root.Bar, 
     Items = string.Join(";", root.Items), 
     Value = 0 
    }); 
+0

好吧,我只是想我可以索引任何我可以存儲(並存儲IList 工作正常)。 – 2011-12-27 05:58:09