This post goes some way to answering this question (I’ll include the answer later), but I was hoping for some further details.
We have a number of applications that each need to access/manipulate data from Raven in their own way. Data is only written via the main web application. Other apps include batch-style tasks, reporting etc. In an attempt to keep each of these as de-coupled as possible, they are separate solutions.
That being the case, how can I, from the reporting application, create indexes over the existing data, using my locally defined types?
The answer from the linked question states
As long as the structure of the classes you are deserializing into partially matches the structure of the data, it shouldn’t make a difference.
The RavenDB server doesn’t care at all what classes you use in the client. You certainly could share a dll, or even share a portable dll if you are targeting a different platform. But you are correct that it is not necessary.
However, you should be aware of the Raven-Clr-Type metadata value. The RavenDB client sets this when storing the original document. It is consumed back by the client to assist with deserialization, but it is not fully enforced
It’s the first part of that that I wanted clarification on. Do the object graphs for the docs on the server and types in my application have to match exactly? If the Click document on the server is
{
"Visit": {
"Version": "0",
"Domain": "www.mydomain.com",
"Page": "/index",
"QueryString": "",
"IPAddress": "127.0.0.1",
"Guid": "10cb6886-cb5c-46f8-94ed-4b0d45a5e9ca",
"MetaData": {
"Version": "1",
"CreatedDate": "2012-11-09T15:11:03.5669038Z",
"UpdatedDate": "2012-11-09T15:11:03.5669038Z",
"DeletedDate": null
}
},
"ResultId": "Results/1",
"ProductCode": "280",
"MetaData": {
"Version": "1",
"CreatedDate": "2012-11-09T15:14:26.1332596Z",
"UpdatedDate": "2012-11-09T15:14:26.1332596Z",
"DeletedDate": null
}
}
Is it possible (and if so, how?), to create a Map index from my application, which defines the Click class as follows?
class Click
{
public Guid Guid {get;set;}
public int ProductCode {get;set;}
public DateTime CreatedDate {get;set;}
}
Or would my class have to be look like this? (where the custom types are defined as a sub-set of the properties on the document above, with matching property names)
class Click
{
public Visit Visit {get;set;}
public int ProductCode {get;set;}
public MetaData MetaData {get;set;}
}
UPDATE
Following on from the answer below, here’s the code I managed to get working.
Index
public class Clicks_ByVisitGuidAndProductCode : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinition
{
Map =
"from click in docs.Clicks select new {Guid = click.Visit.Guid, ProductCode = click.ProductCode, CreatedDate = click.MetaData.CreatedDate}",
TransformResults =
"results.Select(click => new {Guid = click.Visit.Guid, ProductCode = click.ProductCode, CreatedDate = click.MetaData.CreatedDate})"
};
}
}
Query
var query = _documentSession.Query<ReportClick, Clicks_ByVisitGuidAndProductCode>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.Where(x => x.CreatedDate >= start.Date && x.CreatedDate < end.Date);
where Click is
public class Click
{
public Guid Guid { get; set; }
public int ProductCode { get; set; }
public DateTime CreatedDate { get; set; }
}
Many thanks @MattJohnson.
The shape is a partial match, then it will fill in where it can, so your second example would work ok.
You can, however, create an index that projects the results you’re showing in your first example. You would map by whatever you actually were going to filter or sort by, and then you would add a TransformResults section:
When you query this index, it will come out in the shape you specified in the transform. This is a features called “Live Projections”, which you can read more about here. (You won’t need an
.As()call, just use.Query<Click, YourIndex>()and it should work fine.)Separately – what are you doing with MetaData is extraneous. Raven keeps metadata separated from the document. Read more on metadata here.
It looks like you have versioning concerns. If you are just keeping an audit trail, you should look at Raven’s standard Versioning Bundle. If you have temporal effectivity concerns, consider using my new Temporal Versioning Bundle.