/ / RavenDb MapReduce index com três dos casos em Map - .net, nosql, ravendb

RavenDb MapReduce index with three from cases em Map - .net, nosql, ravendb

Eu tenho a seguinte entidade Post:

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;}
}

Para a lista de Posts, eu preciso recuperar o Id, o Text, o Rating (soma dos votos), o CommentsCount. Eu tentei criar o seguinte índice 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),
};
}
}

Parecia razoável para mim inicialmente. Mas parece que meu Mapa com três cláusulas de "won" não funciona. A única outra solução que vejo é usar o índice MultiMap com dois mapas (um para votos e outro para comentários). Mas parece um pouco estranho usar o índice MultiMap onde ambos indexes consulta o mesmo documento ... Existem outras soluções?

Respostas:

3 para resposta № 1

Idsa, não há necessidade de definir um índice aqui. Ambas as coleções, Votes e Comments fazem parte do documento, basta usá-los:

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
};

Atualizar: Eu você realmente quer pré-calcular os resultados, dê uma olhada aqui: http://daniellang.net/using-an-index-as-a-materialized-view-in-ravendb/