Last active
December 15, 2015 01:19
-
-
Save kijanawoodard/5179364 to your computer and use it in GitHub Desktop.
Load First of Many
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.Collections.Generic; | |
using System.Linq; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace Testing | |
{ | |
public class FirstOfManyTests : RavenTestBase | |
{ | |
[Fact] | |
public void ShouldFindPlace() | |
{ | |
using (var store = NewDocumentStore()) | |
{ | |
new PlaceWithCoverPhotoIndex().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
session.Store(new Place() { Id = 1, Name = "Egypt", PhotoIds = new[] {10, 11, 12}}); | |
session.Store(new Photo() { Id = 10, Title = "Pyramid"}); | |
session.Store(new Photo() { Id = 11, Title = "Nile" }); | |
session.Store(new Photo() { Id = 12, Title = "Sphinx" }); | |
session.SaveChanges(); | |
} | |
WaitForIndexing(store); | |
using (var session = store.OpenSession()) | |
{ | |
var list = session | |
.Query<Place, PlaceWithCoverPhotoIndex>() | |
.ToList(); | |
Assert.Empty(store.DatabaseCommands.GetStatistics().Errors); | |
Assert.Equal(1, list.Count); | |
} | |
} | |
} | |
// documents: Place | |
public class Place | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public IList<int> PhotoIds { get; set; } | |
} | |
// documents: Photo | |
public class Photo | |
{ | |
public int Id { get; set; } | |
public string Title { get; set; } | |
} | |
// index definition | |
public class PlaceWithCoverPhotoIndex : AbstractIndexCreationTask<Place> | |
{ | |
public PlaceWithCoverPhotoIndex() | |
{ | |
Map = places => | |
from place in places | |
let firstPhotoId = place.PhotoIds.FirstOrDefault() | |
select new { CoverPhotoId = firstPhotoId }; | |
TransformResults = | |
(database, places) => | |
from place in places | |
let firstPhotoId = "photos/" + place.PhotoIds.FirstOrDefault() | |
let cover = database.Load<Photo>(firstPhotoId) | |
select new | |
{ | |
Id = place.Id, | |
Name = place.Name, | |
CoverPhoto = new | |
{ | |
PhotoId = cover.Id, | |
Title = cover.Title, | |
} | |
}; | |
} | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.Linq; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace Testing | |
{ | |
public class FirstOfManyTests : RavenTestBase | |
{ | |
[Fact] | |
public void ShouldFindPlace() | |
{ | |
using (var store = NewDocumentStore()) | |
{ | |
new PlaceWithCoverPhotoIndex().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
session.Store(new Place() { Id = "places/1", Name = "Egypt", PhotoIds = new[] { "photos/10", "photos/11", "photos/12" } }); | |
session.Store(new Photo() { Id = "photos/10", Title = "Pyramid" }); | |
session.Store(new Photo() { Id = "photos/11", Title = "Nile" }); | |
session.Store(new Photo() { Id = "photos/12", Title = "Sphinx" }); | |
session.SaveChanges(); | |
} | |
WaitForIndexing(store); | |
using (var session = store.OpenSession()) | |
{ | |
var list = session | |
.Query<Place, PlaceWithCoverPhotoIndex>() | |
.OfType<PlaceWithCoverPhotoIndex.Result>() | |
.ToList(); | |
Assert.Empty(store.DatabaseCommands.GetStatistics().Errors); | |
Assert.Equal(1, list.Count); | |
} | |
} | |
} | |
// documents: Place | |
public class Place | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public IList<string> PhotoIds { get; set; } | |
} | |
// documents: Photo | |
public class Photo | |
{ | |
public string Id { get; set; } | |
public string Title { get; set; } | |
} | |
// index definition | |
public class PlaceWithCoverPhotoIndex : AbstractIndexCreationTask<Place> | |
{ | |
public class Result | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public Photo CoverPhoto { get; set; } | |
} | |
public PlaceWithCoverPhotoIndex() | |
{ | |
Map = places => | |
from place in places | |
select new { place.Name }; | |
TransformResults = | |
(database, places) => | |
from place in places | |
let cover = place.PhotoIds.Select(x => database.Load<Photo>(x)).FirstOrDefault() | |
select new | |
{ | |
Id = place.Id, | |
Name = place.Name, | |
CoverPhoto = cover | |
}; | |
} | |
} | |
} | |
} |
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
session.Store(new Place() { Id = "places/1", Name = "Egypt", PhotoIds = new[] { "places/1/photos/10", "places/1/photos/11", "places/1/photos/12" } }); | |
session.Store(new Photo() { Id = "places/1/photos/10", Title = "Pyramid" }); | |
session.Store(new Photo() { Id = "places/1/photos/11", Title = "Nile" }); | |
session.Store(new Photo() { Id = "places/1/photos/12", Title = "Sphinx" }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment