我有以下文件叫做预订:
{
"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; }
}
如果我想找到匹配项,我会写什么样的索引和查询 ReservationItemProjections? E.g:
// invalid example query:
var matches = docs.Query<ReservationItemProjection,
ReservationItemProjectionsIndex>()
.Where(x =>
x.EmployeeId == "employees/1" &&
x.StartTime >= minTime &&
x.EndTime <= maxTime)
.ToList();
请注意,我不希望获得预订文件清单,但是 ReservationItemProjection对象列表。 documentation说:
但是,只是让匹配特定查询的文档很有用,我们可以做得更好。我想直接从索引中获取值而不是获取完整的文档,而不是自己获取文档。
我已经尝试过使用这样的索引:
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子句x.EmployeeId ==“employees / 1”。
摘要:所需索引是什么?索引是仅需要Map子句还是Reduce或TransformResults?如何在C#中编写查询?
答案 0 :(得分:8)
卡斯帕, 在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知道您想要索引的值,而不是文档