我有以下名为 Reservation 的文件:
{
"CustomerId": 1,
"Items": [
{
"EmployeeId": "employees/1",
"StartTime": "2011-08-15T07:20:00.0000000+03:00",
"EndTime": "2011-08-15T07:40:00.0000000+03:00"
},
{
"EmployeeId": "employees/1",
"StartTime": "2011-08-15T07:40:00.0000000+03:00",
"EndTime": "2011-08-15T09:10:00.0000000+03:00"
},
{
"EmployeeId": "employees/3",
"StartTime": "2011-08-16T07:20:00.0000000+03:00",
"EndTime": "2011-08-16T11:35:00.0000000+03:00"
}
]
"ReservedAt": "2011-10-20T15:28:21.9941878+03:00"
}
此外,我还有以下投影类:
public class ReservationItemProjection
{
public string ReservationId { get; set; }
public string CustomerId { get; set; }
public string EmployeeId { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}
如果我想找到匹配我写什么样的索引和查询
预订项目预测?例如。:
// invalid example query:
var matches = docs.Query<ReservationItemProjection,
ReservationItemProjectionsIndex>()
.Where(x =>
x.EmployeeId == "employees/1" &&
x.StartTime >= minTime &&
x.EndTime <= maxTime)
.ToList();
请注意,我不希望获得预订文件 list ,而是希望获得一份
ReservationItemProjection 对象列表。 documentation说:
But while just getting the documents matching a particular query is useful, we can do better than that. Instead of getting the documents themselves, I want to get the values directly from the index, without getting the full document.
我已经尝试过使用这样的索引:
public class ReservationItemProjectionsIndex :
AbstractIndexCreationTask<Reservation, ReservationItemProjection>
{
public ReservationItemProjectionsIndex()
{
Map = reservations =>
from reservation in reservations
from item in reservation.Items
select new
{
ReservationId = reservation.Id,
CustomerId = reservation.CustomerId,
item.EmployeeId,
item.StartTime,
item.EndTime
};
Store(x => x.ReservationId, FieldStorage.Yes);
Store(x => x.CustomerId, FieldStorage.Yes);
Store(x => x.EmployeeId, FieldStorage.Yes);
Store(x => x.StartTime, FieldStorage.Yes);
Store(x => x.EndTime, FieldStorage.Yes);
}
}
不知何故我无法让查询和索引工作:它要么抛出一个关于
无法从 ReservationItemProjection 转换为 Reservation 或者,当我
已经能够获得 ReservationItemProjection 对象,他们将有
包括所有预订中的所有项目,即使有一个匹配的项目,甚至
虽然我的查询有 Where-clause x.EmployeeId == "employees/1"。
摘要:所需的索引是什么?索引是否只需要 Map 子句或还需要 Reduce 或 TransformResults?如何在 C# 中编写查询?
最佳答案
卡斯帕,
在 RavenDB 中,您正在查询文档。虽然技术上可以做你想做的事,但这样做通常毫无意义,因为转换的信息没有必要的上下文来处理它。
你想要做什么?
作为引用,索引将类似于:
from doc in docs.Items
from reservation in doc.Reservations
select new { reservation.EmployeeId, reservation.Start, reservation.End }
然后,将 EmployeeId、Start 和 End 标记为 Store。
现在,在您的查询中,发出:
session.Query<...,...>().AsProjection<ReservationProjection>().ToList();
AsProjection 调用将让 DB 知道您想要来自索引的值,而不是文档
关于indexing - 如何在 RavenDB 中查询嵌套信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7848015/