我是一名RavenDB新手,我正在尝试使用多重映射索引功能,但我不确定它是否是解决我的问题的最佳方法。所以我有三个文档:Unit、Car、People。
Car文档如下:
人员文档长这样:
在关系型数据库中,我只需要通过人和单位表的id使用两个连接,并且我的汽车表将是一个聚合实体。
这里是我拥有的索引定义:
当RavenDb运行此索引时,我发现在尝试运行我提供的Reduce函数时出现错误。当我尝试匹配一个人名和姓氏都存在的记录时,它会抛出错误,单位也是如此。
Car文档如下:
{
Id: "cars/123",
PersonId: "people/1235",
UnitId: "units/4321",
Make: "Toyota",
Model: "Prius"
}
人员文档长这样:
{
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"
}
在关系型数据库中,我只需要通过人和单位表的id使用两个连接,并且我的汽车表将是一个聚合实体。
这里是我拥有的索引定义:
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函数时出现错误。当我尝试匹配一个人名和姓氏都存在的记录时,它会抛出错误,单位也是如此。