I have the following index (simplified for this question):
public Jobs_Search()
{
Map = vacancies => from v in vacancies
from c in v.Campaigns
where c.Status == CampaignStatus.Active
select new
{
Id = v.Id,
DateSubmitted = v.DateSubmitted,
JobCategoryId = v.JobCategoryId,
Salary = v.Salary.From,
Location = new {
v.Location.TownCity,
v.Location.County
},
Keywords = new {
c.Title,
c.Keywords,
c.Summary,
c.Description
},
JobTypes = v.JobTypes,
IsFeatured = c.IsFeatured
};
Index(x => x.Location, Raven.Abstractions.Indexing.FieldIndexing.Analyzed);
Index(x => x.Keywords, Raven.Abstractions.Indexing.FieldIndexing.Analyzed);
TransformResults = (store, results) =>
// ommitted for brevity
select new
{
// view model class fields
};
}
public class Index
{
public string Id { get; set; }
public DateTime DateSubmitted { get; set; }
public string JobCategoryId { get; set; }
public decimal Salary { get; set; }
public string Location { get; set; }
public string Keywords { get; set; }
public string[] JobTypes { get; set; }
public bool IsFeatured { get; set; }
}
}
Within "TransformResults" we retrieve some additional information from the database and build up our ViewModel class (JobSummary).
When querying this index we do the following:
var jobs = session.Query<JobSummary, Jobs_Search>()
.Statistics(out stats)
.OrderBy(j => j.DateSubmitted)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToList();
Notice here that we are asking raven to return "JobSummary" (our ViewModel) rather than the Jobs_Search.Index class.
We found a bug in the index (our fault) and needed to add criteria for two additional fields. To do this we would need to add the fields to both the Jobs_Search.Index class and the JobSummary ViewModel class.
I don't like this since we don't care about presenting these fields we only want them for querying.
So my question.
Does "AsProjection<T>" achieve the same thing? This way I can just do:
session.Query<Jobs_Search.Index, Jobs_Search>()
and then call
.AsProjection<JobSummary>()
at the very end.
I'm using similar code in a number of places so wanted to verify this behaviour before I swap it out.
Thanks,
Ben