3

Just learning Map/Reduce and I'm missing a step. I've read this post ( RavenDB Map-Reduce Example using .NET Client ) but can't quite make the jump to what I need.

I have an object:

public class User : IIdentifiable
{
    public User(string username)
    {
        Id = String.Format(@"users/{0}", username);
        Favorites = new List<string>();
    }

    public IList<string> Favorites { get; protected set; }

    public string Id { get; set; }
}

What I want to do is get Map/Reduce the Favorites property across all Users. Something like this (but this obviously doesn't work):

 Map = users => from user in users
                from oil in user.Favorites
                select new { OilId = oil, Count = 1 };
 Reduce = results => from result in results
                     group result by result.OilId into g
                     select new { OilId = g.Key, Count = g.Sum(x => x.Count) };

For example, if User1 has favorites 1, 2, 3 and User 2 has favorites 1,2, then this should return back {{OilId=3, Count =1}, {OilId=2, Count = 2}, {OilId=1,Count = 2}}

The current code produces an exception: System.NotSupportedException : Node not supported: Call

I feel like I'm close. Any help?

Community
  • 1
  • 1
Matthew Bonig
  • 2,001
  • 2
  • 20
  • 40
  • What results do you get back, if you query against that Map/Reduce index where "Count > 0" what values do you get back? – Matt Warren Jul 28 '11 at 22:45
  • The code supplied for the Map/Reduce produces an error when run: System.NotSupportedException : Node not supported: Call – Matthew Bonig Jul 29 '11 at 12:24

2 Answers2

4

I wrote a small application replicating your code, but I don't see the exception thrown. See my code here: http://pastie.org/2308175. The output is

Favorite: 1, Count: 2

Favorite: 2, Count: 2

Favorite: 3, Count: 1

which is what I would expect.

Community
  • 1
  • 1
Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
  • Yes, after looking it over we've got the same code. The problem I had was that I had a Select added to my RavenDB IQueryable object. I called a ToList first and then called the select and all works. Thanks! – Matthew Bonig Aug 03 '11 at 04:58
3

MBonig, Map/Reduce is only useful for doing aggregation across documents. For something like this, you would be served far better by doing something like:

  session.Query<User>().Select(u=>u.Favorites).ToList()
Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • I thought that is what I was trying to do. I guess I wasn't clear in the OP. I want to aggregate and find out what values in all the Users' Favorites list are the most selected. – Matthew Bonig Jul 28 '11 at 12:45