1

Taking the Stack Overflow tagging system as the canonical example, how can I get back a tag object with its count?

Given these entities:

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

And the following definition of the TagCount index

// 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)
}

On the tags page (e.g. https://stackoverflow.com/tags) we need to display a combination of the Tags collection and the TagCounts index. That is, we need to display the Tag.Name and Tag.Description from the Tags collection and TagCount.Count from the TagCount index. In SQL Server this would be achieved by a join, but this is obviously the relational way of thinking. What is the idiomatic RavenDB way of achieving this?

Is there a way to add a Count property to the Tag entity and have the index automatically update that property?

4

1 回答 1

2

您可以通过修补来做到这一点,在 API 中也称为 UpdateByIndex。你可以在这里看到这个:http: //blog.hibernatingrhinos.com/12705/new-option-in-the-ravendb-studiondash-patching

于 2012-10-22T19:15:36.250 回答