SiberianGuy
SiberianGuy

Reputation: 25312

RavenDb Multi-map/reduce index in case of no common property to group by

I have the following documents:

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

I need to create an index so that I could calculate posts count for each user among blogs in specific language (so I want to filter on Blog.Language property).

First I thought multimap/reduce index was a way to go:

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

But as you see there is no UserId property specified in Blog so I don't think I can make Reduce part work (as I was going to reduce on UserId property).

Upvotes: 0

Views: 105

Answers (1)

Thomas Freudenberg
Thomas Freudenberg

Reputation: 5078

As far as I understand you do not need the blogs in your index but their languages:

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

and then reduce by UserId and Language:

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

Upvotes: 1

Related Questions