【问题标题】:Does multi map/reduce work in RavenDb?多映射/减少在 RavenDb 中有效吗?
【发布时间】:2011-12-23 12:26:02
【问题描述】:

我已经阅读了 RavenDB 的 Ayende 的 blog post on the multi map feature 并尝试实现它。我无法让它通过。我所拥有的与博文中的示例基本相同:

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

查询索引总是给我 Value 属性的值 0。稍微摆弄一下索引会使 ChildDocument 的映射似乎从不检索任何文档。

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

【问题讨论】:

    标签: c# mapreduce ravendb


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多