RavenDb MapReduce的過濾器我有最簡單的MapReduce指數:憑身份證
public class LawDisadvantageRatingIndex : AbstractIndexCreationTask<LawDisadvantage, LawDisadvantageRatingIndex.IndexResult>
{
public class IndexResult
{
public string Id { get; set; }
public long Value { get; set; }
}
public LawDisadvantageRatingIndex()
{
Map = (lawDisadvantages => from lawDisadvantage in lawDisadvantages
from vote in lawDisadvantage.Votes
select
new
{
Id = lawDisadvantage.Id,
Value = vote.Value,
});
Reduce = (lawDisadvantages => from lawDisadvantage in lawDisadvantages
group lawDisadvantage by lawDisadvantage.Id
into agg
select new
{
Id = agg.Key,
Value = agg.Sum(x => x.Value)
});
}
}
這裏是典型的查詢:
_documentSession.Query<LawDisadvantageRatingIndex.IndexResult, LawDisadvantageRatingIndex>().Single(x => x.Id == lawDisadvantageId);
當我憑身份證我得到下面的異常與過濾查詢它:
「Url」: 「/ indexes/LawDisadvantageRatingIndex?query = __ document_id%253ALawDisadvantages%252F1 &開始= 0 &的pageSize = 2 &聚集=無 「 」錯誤「:」 System.ArgumentException:字段 '__document_id' 不是 索引,在未建立索引\ r \ n在 烏鴉字段無法查詢。 Database.Indexing.Index.IndexQueryOperation.AssertQueryDoesNotContainFieldsThatAreNotIndexes()
這question似乎有關。
P. S.我錯誤地刪除了我以前的類似問題 - 對不起。
更新。感謝馬特·沃倫建議我來實現,而不降低部分:
public class LawDisadvantagesForListIndex : AbstractIndexCreationTask<LawDisadvantage>
{
public class IndexResult
{
public string Id { get; set; }
}
public class LawDisadvantageForList
{
public string Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public long Rating { get; set; }
public long CommentsCount { get; set; }
}
public LawDisadvantagesForListIndex()
{
Map = lawDisadvantages => from lawDisadvantage in lawDisadvantages
select new { Id = lawDisadvantage.Id };
TransformResults = (database, lawDisadvantages) => from lawDisadvantage in lawDisadvantages
select new
{
Id = lawDisadvantage.Id,
Title = lawDisadvantage.Title,
Description = lawDisadvantage.Description,
Rating = lawDisadvantage.Votes.Sum(x => x.Value),
CommentsCount = lawDisadvantage.Comments.Count
};
}
}
這裏是一個典型的用法:
var lawDisadvantages =
_documentSession.Query<LawDisadvantagesForListIndex.IndexResult, LawDisadvantagesForListIndex>().
As<LawDisadvantagesForListIndex.LawDisadvantageForList>().
ToList();
我喜歡這個實現,但dissappointing部分是我必須計算等級和CommentsCount on the fly(我寧願將這些值存儲在索引中)。
P. S.我仍然認爲RavenDb中有一個與我的第一個索引相關的bug。
呃,現在我更困惑了。您正在設置和索引,只能在Id上查詢。您希望針對Id字段執行哪種類型的查詢,可能有更有效的方法來執行此操作? –
@MattWarren,我已經添加了一個典型的查詢問題 – SiberianGuy
@MattWarren,你說得對,在這種情況下我沒有通過Id查詢,但我不知道如何創建一個不包含任何字段的索引。嗯......看起來我肯定在這裏做錯了 – SiberianGuy