RavenDB - Can I create/query an index, from an application that doesn't share the CLR objects that were persisted?

Posted by Jaz Lalli on Stack Overflow See other posts from Stack Overflow or by Jaz Lalli
Published on 2012-12-10T18:54:08Z Indexed on 2012/12/11 17:03 UTC
Read the original article Hit count: 224

Filed under:

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.

© Stack Overflow or respective owner

Related posts about ravendb