Created
May 9, 2010 02:13
-
-
Save ayende/394898 to your computer and use it in GitHub Desktop.
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
[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