.net RavenDb MapReduce索引,包含Map中的三个from案例
我拥有以下邮政实体:.net RavenDb MapReduce索引,包含Map中的三个from案例,.net,nosql,ravendb,.net,Nosql,Ravendb,我拥有以下邮政实体: public class Post { public string Id {get;set;} public string Text {get;set;} public IList<Vote> Votes {get;set;} public IList<Comment> Comments {get;set;} } 对于帖子列表,我需要检索Id、文本、评分、投票总数和评论。我尝试创建以下MapReduce索引: public
public class Post
{
public string Id {get;set;}
public string Text {get;set;}
public IList<Vote> Votes {get;set;}
public IList<Comment> Comments {get;set;}
}
对于帖子列表,我需要检索Id、文本、评分、投票总数和评论。我尝试创建以下MapReduce索引:
public class PostsForList: AbstractIndexCreationTask<Post, PostsForList.ReduceResult>
{
public class ReduceResult
{
public string Id { get; set; }
public string Text { get; set; }
public long Rating { get; set; }
public long CommentsCount { get; set; }
}
public PostsForList()
{
Map = posts => from post in posts
from comment in post.Comments
from vote in post.Votes
select
new
{
Id = post.Id,
Text = post.Text,
Rating = vote.Value /* 1 or -1 */
CommentsCount = 1,
};
Reduce = results => from result in results
group result by result.Id
into grouped
select
new
{
Id = grouped.Key,
Text = grouped.Select(x => x.Text).First(),
Rating = grouped.Sum(x => x.Rating)
CommentsCount = grouped.Sum(x => x.Rating),
};
}
}
起初对我来说,这似乎是合理的。但是看起来我的三个from子句的映射不起作用。我看到的唯一其他解决方案是使用带有两个贴图的MultiMap索引,一个用于投票,另一个用于评论。但是使用MultiMap索引,其中两个索引都查询同一个文档,这看起来有点奇怪。。。还有其他解决方案吗?Idsa,这里不需要定义索引。集合、投票和评论都是文档的一部分,因此只需使用它们:
var posts = documentSession.Query<Post>()
.Skip(CurrentPage*PageSize)
.Take(PageSize)
.ToList(); // here is the db-call! all following is just in-memory
var viewModelPosts = from post in posts
select new
{
post.Id,
post.Text,
Rating = post.Votes.Sum(x => x.Value),
CommentsCount = post.Comments.Count
};
更新:
如果您真的想预计算结果,请看这里:但我不想检索客户端的所有评论和投票。我想在服务器端计算评级和VotesCount OK,在这种特殊情况下这样做没有多大价值,但这当然是可能的,事实上非常简单。我写了一篇博文解释说:非常感谢你的帮助!请将您的博客帖子url放在回答中。博客帖子链接已关闭。有新的吗?不幸的是没有。不过,您可能可以在waybackmachine中找到它