Skip to content

Instantly share code, notes, and snippets.

@mattjohnsonpint
Created January 10, 2013 05:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattjohnsonpint/4499724 to your computer and use it in GitHub Desktop.
Save mattjohnsonpint/4499724 to your computer and use it in GitHub Desktop.
Example for SO Q14228793
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Tests.Helpers;
using Xunit;
namespace RavenTests.StackOverflow
{
public class Q14228793 : RavenTestBase
{
[Fact]
public void Test()
{
using (var documentStore = NewDocumentStore())
{
documentStore.ExecuteIndex(new Photos_ByTag());
using (var session = documentStore.OpenSession())
{
session.Store(new Album { Id = "albums/1", Name = "Album 1" });
session.Store(new Photo { Id = "photos/1", AlbumId = "albums/1", Tags = new[] { "alice", "bob", "charlie" } });
session.Store(new Photo { Id = "photos/2", AlbumId = "albums/1", Tags = new[] { "alice", "david" } });
session.Store(new Photo { Id = "photos/3", AlbumId = "albums/1", Tags = new[] { "bob", "david" } });
session.Store(new Album { Id = "albums/2", Name = "Album 2" });
session.Store(new Photo { Id = "photos/4", AlbumId = "albums/2", Tags = new[] { "bob", "charlie" } });
session.Store(new Photo { Id = "photos/5", AlbumId = "albums/2", Tags = new[] { "charlie", "david" } });
session.Store(new Photo { Id = "photos/6", AlbumId = "albums/2", Tags = new[] { "bob", "alice" } });
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
var tagsToSearch = new[] { "charlie", "bob" };
var photos = session.Query<Photos_ByTag.IndexEntry, Photos_ByTag>()
.Customize(x => x.WaitForNonStaleResults()) // only in testing
.Where(x => x.Tag.In(tagsToSearch))
.As<Photo>()
.ToList();
Assert.Equal(5, photos.Count);
Assert.DoesNotContain("photos/5", photos.Select(x => x.Id));
}
}
}
public class Album
{
public string Id { get; set; }
public string Name { get; set; }
}
public class Photo
{
public string Id { get; set; }
public string AlbumId { get; set; }
public IList<string> Tags { get; set; }
}
public class Photos_ByTag : AbstractIndexCreationTask<Photo, Photos_ByTag.IndexEntry>
{
public class IndexEntry
{
public string Tag { get; set; }
}
public Photos_ByTag()
{
Map = photos => from photo in photos
from tag in photo.Tags
select new {
Tag = tag
};
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment