我有以下文件叫做预订:
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
另外我有以下投影类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
如果我想找到匹配的ReservationItemProjections,我会写什么样的索引和查询?例如:
// invalid example query:
var matches = docs.Query<ReservationItemProjection,
ReservationItemProjectionsIndex>()
.Where(x =>
x.EmployeeId == "employees/1" &&
x.StartTime >= minTime &&
x.EndTime <= maxTime)
.ToList();
Run Code Online (Sandbox Code Playgroud)
请注意,我不希望获得Reservation文档列表,而是获取ReservationItemProjection对象列表.该文件说:
但是,只是获取匹配特定查询的文档是有用的,我们可以做得更好.我想直接从索引中获取值而不是获取完整的文档,而不是自己获取文档.
我已经尝试使用这样的索引:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
不知怎的,我无法使查询和索引工作:它抛出一个异常,关于无法从ReservationItemProjection转换为Reservation,或者当我能够获取ReservationItemProjection对象时,它们将包含所有预留中的所有项目甚至有一个匹配的项目,即使我的查询具有Where子句x.EmployeeId =="employees/1".
总结:所需索引是什么?索引是仅需要Map子句还是Reduce或TransformResults?如何在C#中编写查询?
Kasper,在RavenDB中,您正在查询文档.虽然技术上可以做你想做的事情,但这样做通常没有意义,因为预测的信息没有必要的上下文来做它.
你想做什么?
作为参考,索引将是这样的:
from doc in docs.Items
from reservation in doc.Reservations
select new { reservation.EmployeeId, reservation.Start, reservation.End }
Run Code Online (Sandbox Code Playgroud)
然后,将EmployeeId,Start和End标记为Store.
现在,在您的查询中,发出:
session.Query<...,...>().AsProjection<ReservationProjection>().ToList();
Run Code Online (Sandbox Code Playgroud)
AsProjection调用将让DB知道您想要索引的值,而不是文档
| 归档时间: |
|
| 查看次数: |
1975 次 |
| 最近记录: |