【发布时间】:2011-10-21 10:17:35
【问题描述】:
我有以下名为 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();
请注意,我不希望获得预订文件列表,而是 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-clause x.EmployeeId == "employees/1"。
总结:需要的索引是多少?索引是否只需要一个 Map 子句或 Reduce 或 TransformResults?如何用 C# 编写查询?
【问题讨论】: