RavenDB:如何创建MapReduce索引以返回已过滤的子对象列表

时间:2012-02-27 16:56:33

标签: ravendb indexing

关注this question

我有以下文档结构:

Game 
 - Id
 - Teams
   - Team 1
     - list of players
   - Team 2 
     - list of players
 - Events
   - Event 1
     - type: Pass
     - teamId
     - PlayerId
   - Event 2
     - type: Goal
     - teamId
     - PlayerId

如何构建一个索引,为我提供给定游戏的玩家所有事件?

这是我得到了多远,RavenDB说它无法理解我的查询?

public class Games_PlayerEvents : AbstractIndexCreationTask<Game, Games_PlayerEvents.ReduceResult>
    {
        public class ReduceResult
        {
            public string PlayerId { get; set; }
            public IEnumerable<GameEvent> Events { get; set; }
        }


        public Games_PlayerEvents()
        {
            Map = games => from game in games
                           select new
                           {
                               PlayerId = "",
                               Events = game.Events
                           };

            Reduce = results => from result in results
                                from @event in result.Events
                                group @event by @event.PlayerId into playerEvents
                                select new
                                {
                                    PlayerId = playerEvents.Key,
                                    Events = playerEvents.Select(g => g)
                                };


        }
    }

1 个答案:

答案 0 :(得分:3)

Marto,如果您真的想要以这种方式对其进行建模,则无需创建索引,因为您可以加载整个游戏文档并使用标准linq-to完成其余操作 - 对象聚合。

无论如何,听起来好像你想把游戏,玩家和事件作为独立的文件,因为这些是你的集合根或换句话说 - 它们本身就有意义。