Skip to content

Instantly share code, notes, and snippets.

@ayende
Created May 9, 2010 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayende/394898 to your computer and use it in GitHub Desktop.
Save ayende/394898 to your computer and use it in GitHub Desktop.
[Fact]
public void CanQueryMapReduceIndex_WithUpdates()
{
using (var store = NewDocumentStore())
{
store.DatabaseCommands.PutIndex("TagCloud",
new IndexDefinition
{
Map =
@"
from post in docs.Posts
from Tag in post.Tags
select new { Tag, Count = 1 }",
Reduce =
@"
from result in results
group result by result.Tag into g
select new { Tag = g.Key, Count = g.Sum(x => (long)x.Count) }"
});
using (var session = store.OpenSession())
{
session.Store(new Post
{
PostedAt = DateTime.Now,
Tags = new List<string> { "C#", "Programming", "NoSql" }
});
session.Store(new Post
{
PostedAt = DateTime.Now,
Tags = new List<string> { "Database", "NoSql" }
});
session.SaveChanges();
var tagAndCounts = session.Query<TagAndCount>("TagCloud").WaitForNonStaleResults()
.ToArray();
Assert.Equal(1, tagAndCounts.Single(x => x.Tag == "C#").Count);
Assert.Equal(1, tagAndCounts.Single(x => x.Tag == "Database").Count);
Assert.Equal(2, tagAndCounts.Single(x => x.Tag == "NoSql").Count);
Assert.Equal(1, tagAndCounts.Single(x => x.Tag == "Programming").Count);
session.Store(new Post
{
PostedAt = DateTime.Now,
Tags = new List<string> { "C#", "Programming", "NoSql" }
});
session.Store(new Post
{
PostedAt = DateTime.Now,
Tags = new List<string> { "Database", "NoSql" }
});
session.SaveChanges();
tagAndCounts = session.Query<TagAndCount>("TagCloud").WaitForNonStaleResults()
.ToArray();
Assert.Equal(2, tagAndCounts.Single(x => x.Tag == "C#").Count);
Assert.Equal(2, tagAndCounts.Single(x => x.Tag == "Database").Count);
Assert.Equal(4, tagAndCounts.Single(x => x.Tag == "NoSql").Count);
Assert.Equal(2, tagAndCounts.Single(x => x.Tag == "Programming").Count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment