It is possible to index multiple types in the same index (so you can
count OpinionsOfFoo while tracking fields in Foo) but I do not think
it can be done with a linq-based index definition
There is a test that shows how to index multiple types in one index at
Raven.Tests.Bugs.MultiEntityIndex. Another example at
Raven.Tests.Bugs.Indexing.ComplexUsage shows how to actually merge the
fields from different types. I don't know if there is a simpler way.
FWIW I don't think I've tried this.
Ayende: I would have two concerns storing OpinionOfFoo with Foo, one
is the likelihood of concurrency problems the other is that the
documents will get too large if you have a lot of traffic.
*: One advantage of storing each OpinionOfFoo as a separate document
is that you can use the document identity to ensure only one opinion
per foo/user. So no need to read anything when someone saves a
rating.
Is there any drawback to storing OpinionOfFoo separately besides the
difficulty of defining an index that takes multiple types as input?
It seems to me the linq index definitions could be extended to support
this, though not trivial.
Matt, if I were try to support linq-based indexes that can map
multiple types, it might look like:
public class OverallOpinion : AbstractIndexCreationTask<?>
{
public OverallOpinion()
{
Map<Foo>(docs => from doc in docs select new { Id =
doc.Id, LastUpdated = doc.LastUpdated }
Map<OpinionOfFoo>(docs => from doc in docs select new
{ Id = Doc.DocId, Rating = doc.Rating, Count = 1}
Reduce = docs => from doc in docs
group doc by doc.Id into g
select new {
Id = g.Key,
LastUpdated = g.Values.Where(f =>
f.LastUpdated != null).FirstOrDefault(),
Rating = g.Values.Rating.Sum(),
Count = g.Values.Count.Sum()
}
// average rating is Rating / Count
}
}
It seems like some clever code could combine the different map
expressions into one.