【发布时间】:2012-10-22 17:46:54
【问题描述】:
以 Stack Overflow 标记系统为例,如何获取标记对象及其计数?
鉴于这些实体:
// Represents the data from the TagCount index
public class TagCount
{
public string Tag { get; set; }
public int Count { get; set; }
}
// Represents the data used to populate a tag-wiki
public class Tag
{
public string Tag { get; set; }
public DateTime Created { get; set; }
public string Description { get; set; }
}
public class Question
{
// elided
public List<string> Tags { get; set; }
// elided
}
以及下面对TagCount索引的定义
// Map
from question in docs.Questions
from Tag in question.Tags
select new { Tag, Count = 1 }
// Reduce
from result in results
group result by result.Tag into g
select new
{
Tag = g.Key,
Count = g.Sum(x=>x.Count)
}
在标签页面(例如https://stackoverflow.com/tags)上,我们需要显示标签集合和 TagCounts 索引的组合。也就是说,我们需要显示来自 Tags 集合的 Tag.Name 和 Tag.Description 以及来自 TagCount 索引的 TagCount.Count。在 SQL Server 中,这将通过连接来实现,但这显然是关系思维方式。实现此目的的惯用 RavenDB 方式是什么?
有没有办法将 Count 属性添加到 Tag 实体并让索引自动更新该属性?
【问题讨论】:
标签: ravendb