RavenDB version 2.5. Other versions:

Results Transformers

Results Transformers have been introduced in RavenDB 2.5 to give the user the ability to do a server side projections (with possibility to load data from other documents).

Result Transformers are substituting the index TransformResults feature, marking it as obsolete.

Main features of the Result Transformers are:

I. Stand-alone, separated from Index.

    public class Order
    {
    	public DateTime OrderedAt { get; set; }
    
    	public Status Status { get; set; }
    
    	public string CustomerId { get; set; }
    
    	public IList<OrderLine> Lines { get; set; }
    }
    
    
    public class OrderStatisticsTransformer : AbstractTransformerCreationTask<Order>
    {
    	public OrderStatisticsTransformer()
    	{
    		TransformResults = orders => from order in orders
    									 select new
    											{
    												order.OrderedAt,
    												order.Status,
    												order.CustomerId,
    												CustomerName = LoadDocument<Customer>(order.CustomerId).Name,
    												LinesCount = order.Lines.Count
    											};
    	}
    }
    
    

II. User can use them on index results on demand.

What does it mean? It means if you want load whole Order you can do so by not using a transformer in the query:

    IList<Order> orders = session.Query<Order>()
    	.Where(x => x.CustomerId == "customers/1")
    	.ToList();
    
    

or if you want to get your transformed results then you can execute query as follows:

    public class OrderStatistics
    {
    	public DateTime OrderedAt { get; set; }
    
    	public Status Status { get; set; }
    
    	public string CustomerId { get; set; }
    
    	public string CustomerName { get; set; }
    
    	public int LinesCount { get; set; }
    }
    
    
    IList<OrderStatistics> statistics = session.Query<Order>()
    	.TransformWith<OrderStatisticsTransformer, OrderStatistics>()
    	.Where(x => x.CustomerId == "customers/1")
    	.ToList();
    
    

III. Can be used with automatic indexes.

    OrderStatistics statistic = session.Load<OrderStatisticsTransformer, OrderStatistics>("orders/1");
    
    
Comments add new comment

The comments section is for user feedback or community content. If you seek assistance or have any questions, please post them at our support forums.

Georgios Diamantopoulos
REPLY Posted by Georgios Diamantopoulos on

Is it possible to query an index from a transformer? Or would that make it too complicated?

Ernesto Angel
REPLY Posted by Ernesto Angel on

Is this included in the first 2.5 release (2.5.2666)?

Ernesto Angel
REPLY Posted by Ernesto Angel on

In this example Order does not have have an Id property. Assuming it did have one, how would we reference it in our result. Currently I am getting an error that reads "Could not read value for property Id"

Mike Schenk
REPLY Posted by Mike Schenk on

How can a result transformer be used with the .Net API to return polymorphic types?

In other words, how can I write a VehicleTransformer so that when I use .TransformWith<VehicleTransformer, Vehicle>(), how can I receive back an enumeration of Cars, Boats, and Motorcycles?

SUBMIT COMMENT