gpt4 book ai didi

RavenDB MultiMap 索引

转载 作者:行者123 更新时间:2023-12-04 21:22:17 27 4
gpt4 key购买 nike

我是 RavenDB 的新手。我正在尝试使用多 map 索引功能,但我不确定这是否是解决我的问题的最佳方法。所以我有三个文件: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 表
我的汽车 table 将是一个聚合实体。

这是我拥有的索引定义:
 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 函数时看到错误。当我试图匹配一个人的名字和姓氏存在的记录时,它会抛出错误,同样发生在单元中。

最佳答案

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

Keeping a Domain Model Pure with RavenDB

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

关于RavenDB MultiMap 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10219183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com