RavenDb在没有共同属性的情况下多重映射/减少索引

时间:2015-03-01 19:49:14

标签: ravendb

我有以下文件:

public class Post
{
   public string Id {get;set;}
   public string BlogId {get;set;}
   public string AuthorId {get;set;}
   public string Content {get;set;}
}

public class Blog
{
  public string Id {get;set;}
  public string Language {get;set;}
}

我需要创建一个索引,以便我可以用特定语言计算博客中每个用户的帖子数(所以我想过滤Blog.Language属性)。

首先我认为multimap / reduce索引是一种方法:

AddMap<Post>((posts) => from post in posts
                        select new 
                        {
                          UserId = post.AuthorId,
                          Count = 1,
                          Language = Language.None
                        });
AddMap<Blog>((blogs) => from blog in blogs
                        select new
                        {
                          UserId = null,
                          Count = 0,
                          Language = blog.Language 
                        });

但是你看到Blog中没有指定UserId属性,所以我认为我不能让Reduce部分工作(因为我要减少UserId属性)。

1 个答案:

答案 0 :(得分:1)

据我了解,您不需要索引中的博客,而是他们的语言:

AddMap<Post>((posts) => from post in posts
                        select new 
                        {
                          UserId = post.AuthorId,
                          Count = 1,
                          Language = LoadDocument<Blog>(post.BlogId).Language
                        });

然后按UserIdLanguage缩小:

Reduce = results => from result in results 
                    group result by new { result.UserId, result.Language }
                    into g 
                    select new { 
                        UserId = g.Key.UserId, 
                        Count = g.Sum(x => x.Count),
                        Language = g.Key.Language
                    };