Created
October 6, 2012 07:27
-
-
Save jesuslpm/3844317 to your computer and use it in GitHub Desktop.
RavenDB Simple join with multimap reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Raven.Client.Indexes; | |
using Raven.Client; | |
using Raven.Client.Embedded; | |
using Raven.Json.Linq; | |
namespace SimpleJoinWithMultimapReduce | |
{ | |
public class Product | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
} | |
public class ProductStats | |
{ | |
public string Id { get; set; } // Products/1/Stats | |
public int ViewCount { get; set; } | |
} | |
public class ProductWithStats | |
{ | |
public string ProductId { get; set; } | |
public string Name { get; set; } | |
public int? ViewCount { get; set; } | |
} | |
public class Products_WithStats : AbstractMultiMapIndexCreationTask<ProductWithStats> | |
{ | |
public Products_WithStats() | |
{ | |
AddMap<ProductStats>(stats => from s in stats | |
select new | |
{ | |
ProductId = s.Id.Replace("/Stats", string.Empty), | |
Name = (string) null, | |
ViewCount = (int?) s.ViewCount | |
}); | |
AddMap<Product>( products => from p in products | |
select new | |
{ | |
ProductId = p.Id, | |
Name = p.Name, | |
ViewCount = (int?) null | |
}); | |
Reduce = results => from result in results | |
group result by result.ProductId into g | |
select new | |
{ | |
ProductId = g.Key, | |
Name = g.Select(x => x.Name).FirstOrDefault(x => x != null), | |
ViewCount = g.Select(x => x.ViewCount).FirstOrDefault(x => x != null) | |
}; | |
Index("Name", Raven.Abstractions.Indexing.FieldIndexing.Analyzed); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var store = new EmbeddableDocumentStore { RunInMemory = true }) | |
{ | |
store.Initialize(); | |
new Products_WithStats().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
session.Store ( new Product { Id = "Products/1", Name = "Gazpacho andaluz" } ); | |
session.Store ( new ProductStats { Id = "Products/1/Stats", ViewCount = 2 } ); | |
session.SaveChanges(); | |
var query = session.Query<ProductWithStats, Products_WithStats>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.Search(x => x.Name, "Gazp*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard); | |
Console.WriteLine(query); | |
var productsWithStats = query.ToList(); | |
var statistics = store.DatabaseCommands.GetStatistics(); | |
foreach (var pws in productsWithStats) | |
{ | |
Console.WriteLine ( RavenJObject.FromObject(pws).ToString()); | |
} | |
Console.WriteLine("Press enter..."); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment