我是RavenDB的新手。我正在尝试使用多地图索引功能,尽管我不确定这是否是解决我的问题的最佳方法。所以我有三个文档:单位,汽车,人。
汽车文档看起来像这样:
{
Id: "cars/123",
PersonId: "people/1235",
UnitId: "units/4321",
Make: "Toyota",
Model: "Prius"
}People文档如下所示:
{
Id: "people/1235",
FirstName: "test",
LastName: "test"
}和单元文档:
{
Id: "units/4321",
Address: "blah blah"
}这是一个简短的例子,在我的真实应用程序中有更多的字段,所以数据反规范化将是我最后的手段。
我需要创建和索引,将所有这三个文档连接到一个文档中。如下所示:
{
CarId: "cars/123",
PersonId: "people/1235",
UnitId: "units/4321",
Make: "Toyota",
Model: "Prius"
FirstName: "test",
LastName: "test"
Address: "blah blah"
}
// same unit different person owns a different car
{
CarId: "cars/122",
PersonId: "people/1236",
UnitId: "units/4321",
Make: "Toyota",
Model: "4runner"
FirstName: "test",
LastName: "test"
Address: "blah blah"
}在关系数据库中,我只使用两个连接到通过In的People和Unit表,而我的car表将是一个聚合实体。
下面是我的索引定义:
public class MyMultiIndex : AbstractMultiMapIndexCreationTask<JoinedDocument>
{
public MyMultiIndex()
{
// creating maps
AddMap<Car>(cars => cars.Select(e => new { e.CarId, e.Make, e.Model, PersonId = e.PersonId, UnitId = e.UnitId, FirstName = (null)string, LastName = (null)string, Address = (nul)string }));
AddMap<People>(people => people.Select(e => new { CarId = (string)null, Make = (string)null, Model = (string)null, PersonId = e.Id, UnitId = (null)string, FirstName = e.FirstName, LastName = e.LastName, Address = (nul)string }));
AddMap<Unit>(people => people.Select(e => new { CarId = (string)null, Make = (string)null, Model = (string)null, PersonId = (null)string, UnitId = e.null, FirstName = (nul)string , LastName = (nul)string , Address = e.Address }));
Reduce = results => from result in results
group result by result.CarId
into g
select new JoinedDocument
{
CarId = g.Key,
PersonId = g.First(e => e.CarId == g.Key).PersonId,
UnitId = g.First(e => e.CarId == g.Key).UnitId,
Model = g.First(e => e.CarId == g.Key).Model,
Make = g.First(e => e.CarId == g.Key).Make,
**// this never works. It is like result set does not contain anything with this personId. It looks like AddMap for people document did not work.**
FirstName = results.First(e => e.PersonId == g.First(ie => ie.CarId == g.Key).PersonId).FirstName,
**// this never works. It is like result set does not contain anything with this personId. It looks like AddMap for people document did not work.**
LastName = results.First(e => e.PersonId == g.First(ie => ie.CarId == g.Key).PersonId).LastName,
**// this never works. It is like result set does not contain anything with this personId. It looks like AddMap for unit document did not work.**
UnitAddress = results.First(e => e.UnitId == g.First(ie => ie.CarId == g.Key).UnitId).LastName,
};
Index(map => map.Model, FieldIndexing.Analyzed);
Index(map => map.Make, FieldIndexing.Analyzed);
Index(map => map.LastName, FieldIndexing.Analyzed);
Index(map => map.FirstName, FieldIndexing.Analyzed);
Index(map => map.Make, FieldIndexing.Analyzed);
Index(map => map.UnitAddress, FieldIndexing.Analyzed);
}
}当RavenDb运行这个索引时,当它试图运行我提供的Reduce函数时,我看到了错误。当我试图匹配一个存在名字和姓氏的记录时,它抛出了错误,同样的情况也发生在单元上。
发布于 2012-04-19 09:42:01
看起来您正在尝试将文档数据库与具有关系的对象模型相匹配。这个博客可能会对你有所帮助:
Keeping a Domain Model Pure with RavenDB
请记住,这不是RavenDB的推荐用法,但有时它是必要的,这是处理它的好方法。
发布于 2012-04-28 15:38:20
你能拥有一辆没有车主的车吗?或者一个没有居民的地址?如果在这两种情况下都是假的,我会将汽车和单元建模为嵌入到一个人的体内。因此person成为您的聚合根,要到达一辆汽车或一个单元,您必须通过person。
https://stackoverflow.com/questions/10219183
复制相似问题