2012-09-05 90 views
3

我有一個需求,我需要獲取競賽日期排序的競賽列表。但是我也需要把我的朋友們跑過的比賽加到最上面。所以我試圖保持一個索引,以保持我的朋友在每場比賽中都能獲勝。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到只反映那些跑步者朋友名單中存在的人。

回答

1

我最終採取了不同的路徑。奧倫說,你不能過濾源數據,這是有道理的。現在,我只需索引誰在什麼樣的競賽中運行過,並在查詢過程中應用where過濾器,然後按照日期順序對所有競賽進行排序,並從以前的查詢中刪除這些競賽。