RavenDB MultiMap索引

时间:2012-04-18 22:47:06

标签: ravendb

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

汽车文件如下:

{
 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"
}

这是一个缩写示例,在我的真实应用程序中有更多字段,因此数据去规范化将是我的最后手段。

我需要创建和索引,将所有这三个docuemnts连接在一个文档中。像这样:

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

2 个答案:

答案 0 :(得分:1)

看起来您正在尝试将文档数据库与具有关系的对象模型相匹配。这个博客可以帮助你:

Keeping a Domain Model Pure with RavenDB

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

答案 1 :(得分:1)

你有没有车主的车吗?或没有居民的地址?如果在两种情况下都是假的,我会将汽车和单元模型嵌入到一个人体内。因此,这个人成为你的集合根,到达汽车或单位,你必须通过一个人。