5

我是 RavenDB 的新手。我正在尝试使用多地图索引功能,但我不确定它是否是解决我的问题的最佳方法。所以我有三个文件:Unit、Car、People。

汽车文件如下所示:

{
 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 对 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 函数时看到错误。当我尝试匹配存在人名和姓氏的记录时,它会引发错误,单位也会发生同样的情况。

4

2 回答 2

1

你能有一辆没有车主的车吗?还是没有居民的地址?如果在这两种情况下都是错误的,我会将汽车和单元建模为嵌入人体内。所以这个人成为你的总根,要到达汽车或单位,你必须经过一个人。

于 2012-04-28T07:38:20.297 回答
1

看起来您正在尝试使用具有关系的对象模型来拟合文档数据库。这个博客可以帮助你:

使用 RavenDB 保持领域模型纯净

请记住,这不是 RavenDB 的推荐使用,但有时它是必要的,这是处理它的好方法。

于 2012-04-19T01:42:00.837 回答