Created
May 13, 2012 19:22
-
-
Save lanceharper/2689846 to your computer and use it in GitHub Desktop.
Demo of a RavenDB index used to find entities with the existence of children objects where at least one child is active.
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
namespace ChildConditionDemo | |
{ | |
public class ChildWithCondition | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public bool Active { get; set; } | |
} | |
} |
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
namespace ChildConditionDemo | |
{ | |
public class Entity | |
{ | |
public string Id { get; set; } | |
public string Content { get; set; } | |
public Collection<string> Children { get; set; } | |
public Entity() | |
{ | |
Children = new Collection<string>() { }; | |
} | |
} | |
} |
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
namespace ChildConditionDemo | |
{ | |
public class EntityWithActiveChildCondition : AbstractIndexCreationTask<Entity> | |
{ | |
public EntityWithActiveChildCondition() | |
{ | |
Map = entries => from entry in entries | |
select new { entry.Id, entry.Content }; | |
TransformResults = (database, entries) => from entry in entries | |
from childId in entry.Children // flatten | |
let loadedChild = database.Load<ChildWithCondition>(childId) | |
where entry.Children.Any(f => f == loadedChild.Id && loadedChild.Active) | |
group entry by new { entry.Id, entry.Content } into g | |
select new { g.Key.Id, g.Key.Content }; | |
Index(x => x.Id, FieldIndexing.Analyzed); | |
Index(x => x.Content, FieldIndexing.Analyzed); | |
} | |
} | |
} |
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
namespace ChildConditionDemo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var documentStore = new EmbeddableDocumentStore { RunInMemory = true }.Initialize()) | |
{ | |
documentStore.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites; | |
using (var session = documentStore.OpenSession()) | |
{ | |
var children = Enumerable.Range(1, 100).Select(f => new ChildWithCondition() { Id = "Child/" + f.ToString(), Active = f > 15 }); // Any child with id > 15 is true, false otherwise | |
Console.WriteLine("Storing children..."); | |
foreach (var child in children) | |
{ | |
session.Store(child); | |
} | |
Console.WriteLine("Storing entities..."); | |
session.Store(new Entity { Content = "entity with no active children", Children = new Collection<string>(children.Take(5).Select(f => f.Id).ToList()) }); // None active. | |
session.Store(new Entity { Content = "entity with some active and inactive children", Children = new Collection<string>(children.Skip(5).Take(20).Select(f => f.Id).ToList()) }); // Some active | |
session.Store(new Entity { Content = "entity with all active children", Children = new Collection<string>(children.Skip(20).Take(10).Select(f => f.Id).ToList()) }); // All active | |
session.SaveChanges(); | |
} | |
Console.WriteLine("Executing index..."); | |
new EntityWithActiveChildCondition().Execute(documentStore); | |
using (var session = documentStore.OpenSession()) | |
{ | |
var activeEntities = session.Query<Entity, EntityWithActiveChildCondition>().ToList(); | |
foreach (var activeEntity in activeEntities) | |
{ | |
Console.WriteLine(activeEntity.Content); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment