Last active
December 11, 2015 02:48
-
-
Save mattjohnsonpint/4532842 to your computer and use it in GitHub Desktop.
Example for SO Q14322959
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.Linq; | |
using Raven.Client; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace RavenTests.StackOverflow | |
{ | |
public class Q14322959 : RavenTestBase | |
{ | |
public class EntityA | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string[] Tags { get; set; } | |
} | |
public class EntityB | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string[] Tags { get; set; } | |
} | |
[Fact] | |
public void Test() | |
{ | |
using (var documentStore = NewDocumentStore()) | |
{ | |
documentStore.ExecuteIndex(new TestIndex()); | |
using (var session = documentStore.OpenSession()) | |
{ | |
session.Store(new EntityA { Name = "Alice", Tags = new[] { "A", "B", "C" } }); | |
session.Store(new EntityA { Name = "Bob", Tags = new[] { "D", "E", "F" } }); | |
session.Store(new EntityA { Name = "Charlie", Tags = new string[0] }); | |
session.Store(new EntityB { Name = "Smith", Tags = new[] { "A", "B", "C" } }); | |
session.Store(new EntityB { Name = "Jones", Tags = new[] { "D", "E", "F" } }); | |
session.SaveChanges(); | |
} | |
using (var session = documentStore.OpenSession()) | |
{ | |
var results1 = session.Query<TestIndex.Result, TestIndex>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.Where(x => x.Name == "Smith") | |
.As<EntityA>() | |
.ToList(); | |
Assert.Equal(1, results1.Count); | |
Assert.Equal("Alice", results1[0].Name); | |
var results2 = session.Query<TestIndex.Result, TestIndex>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.As<EntityA>() | |
.OrderBy(x=> x.Name) | |
.ToList(); | |
// ordering is problematic because it is by the associated EntityB.Name | |
Assert.Equal(3, results2.Count); | |
Assert.Equal("Charlie", results2[0].Name); | |
Assert.Equal("Bob", results2[1].Name); | |
Assert.Equal("Alice", results2[2].Name); | |
} | |
} | |
} | |
public class TestIndex : AbstractMultiMapIndexCreationTask<TestIndex.Result> | |
{ | |
public class Result | |
{ | |
public string[] Ids { get; set; } | |
public string Name { get; set; } | |
public string Tag { get; set; } | |
} | |
public TestIndex() | |
{ | |
AddMap<EntityA>(entities => from a in entities | |
from tag in a.Tags.DefaultIfEmpty("_") | |
select new { | |
Ids = new[] { a.Id }, | |
Name = (string) null, | |
Tag = tag | |
}); | |
AddMap<EntityB>(entities => from b in entities | |
from tag in b.Tags | |
select new { | |
Ids = new string[0], | |
b.Name, | |
Tag = tag | |
}); | |
Reduce = results => from result in results | |
group result by result.Tag | |
into g | |
select new { | |
Ids = g.SelectMany(x => x.Ids), | |
g.First(x => x.Name != null).Name, | |
Tag = g.Key | |
}; | |
TransformResults = (database, results) => results.SelectMany(x => x.Ids) | |
.Distinct() | |
.Select(x => database.Load<EntityA>(x)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result count will be 2 as Alice and Bob both have Tag C which is contained in Smith's Tags...