RavenDb - 按结果筛选组的Multimap索引

时间:2012-09-05 22:03:04

标签: c# .net nosql ravendb

我有一个要求,我需要获得比赛日期前订购的比赛列表。但是我还需要将我朋友们的比赛添加到顶部。因此,我正在尝试维护一个索引,该索引将记录每​​个种族运行的朋友的数量。

我有以下对象:

public class Race {
    public string Id { get; set; }
    public string Name { get; set; }
    public string Distance { get; set; }
    public DateTime DateOfEvent { get; set; }
    public string Location { get; set; }

}
    public class Runner {
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string Hometown { get; set; }

    public List<string> Friends { get; set; }
    public List<string> RacesRun { get; set; }

}

到目前为止,这就是我对索引的看法:

public class RacesByFriends : AbstractMultiMapIndexCreationTask<RacesByFriends.ReduceResult> {

    public class ReduceResult {
        public string RunnerId { get; set; }
        public string RaceId { get; set; }
        public int Count { get; set; }
    }

    public RacesByFriends() {

        AddMap<Runner>(runners => from runner in runners
                                  from race in runner.RacesRun
                                  select new {
                                      RunnerId = runner.Id,
                                      RaceId = race,
                                      Count = 1,
                                  });

        AddMap<Race>(races => from race in races
                              select new {
                                  RunnerId = (string)null,
                                  RaceId = race.Id,
                                  Count = 0,
                              });

        Reduce = results => from result in results
                            group result by new {
                                result.RunnerId,
                                result.RaceId
                            } into g
                            select new {
                                RunnerId = g.Select(x => x.RunnerId).FirstOrDefault(),
                                RaceId = g.Select(x => x.RaceId).FirstOrDefault(),
                                Count = g.Sum(x => x.Count)
                            };

    }
}

我的问题是,如何让ReduceResult中的Count只反映跑步者朋友列表中存在该种族的人。

1 个答案:

答案 0 :(得分:1)

我最终把它带到另一条道路上。 Oren表示你无法过滤源数据,这是有道理的。现在,我只是索引谁运行了什么种族并在查询中应用过滤器中的位置,然后按日期顺序排序所有种族并从之前的查询中删除它们。