Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created May 7, 2010 21:17
Show Gist options
  • Save mattwarren/394007 to your computer and use it in GitHub Desktop.
Save mattwarren/394007 to your computer and use it in GitHub Desktop.
using (var documentStore = new DocumentStore { Url = "http://localhost:8080" })
{
documentStore.Initialise();
using (var session = documentStore.OpenSession())
{
//Only add the index if there are not posts in the database, i.e. the 1st time this is run!!
if (session.Query<Post>().Count() == 0)
{
Console.WriteLine("First time usage, creating indexes");
documentStore.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) }"
});
}
session.Store(new Post { Title = "Title 1", Content = "something", PostedAt = RandomDateTime(), Tags = CreateTags("C#", ".NET") });
session.Store(new Post { Title = "Title 2", Content = "blah blah", PostedAt = RandomDateTime(), Tags = CreateTags("C#", ".NET") });
session.SaveChanges();
var tagCloud = session.Query<TagCloud>("TagCloud").WaitForNonStaleResults().ToArray();
Console.WriteLine("\n\nBEFORE: TagCloud has " + tagCloud.Count() + " items :");
foreach (var tagCount in tagCloud)
Console.WriteLine(" " + tagCount);
session.Store(new Post { Title = "Title 1", Content = "something", PostedAt = RandomDateTime(), Tags = CreateTags("C#", ".NET") });
session.Store(new Post { Title = "Title 2", Content = "blah blah", PostedAt = RandomDateTime(), Tags = CreateTags("C#", ".NET") });
session.SaveChanges();
tagCloud = session.Query<TagCloud>("TagCloud").WaitForNonStaleResults().ToArray();
Console.WriteLine("\n\nAFTER: TagCloud has " + tagCloud.Count() + " items :");
foreach (var tagCount in tagCloud)
Console.WriteLine(" " + tagCount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment