I have the following document called 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" }
In addition, I have the following design class:
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; } }
What index and query do I write if I want to find a ReservationItemProjections match? For instance:.
// invalid example query: var matches = docs.Query<ReservationItemProjection, ReservationItemProjectionsIndex>() .Where(x => x.EmployeeId == "employees/1" && x.StartTime >= minTime && x.EndTime <= maxTime) .ToList();
Please note that I do not want to receive a list of documents for reservation, but a list of ReservationItemProjection objects. The documentation says:
But, while getting documents matching a specific request is helpful, we can do better. Instead of getting the documents myself, I want to get the values directly from the index without getting the full document.
I have already tried using an index like this:
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); } }
Somehow I can’t get the query and the index to work: it either throws an exception from not being able to throw from the ReservationItemProjection into the reservation or, when I could get the ReservationItemProjection objects, they will have included all the objects in all the reservations that even have one a suitable item, even though there is a Where-where x.EmployeeId == "employees / 1" parameter in my request.
Summary: what is the required index? Does an index require only a Map clause, as well as a Reduce or TransformResults? How to write a query in C #?