Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created October 5, 2011 20:59
Show Gist options
  • Save mattwarren/1265715 to your computer and use it in GitHub Desktop.
Save mattwarren/1265715 to your computer and use it in GitHub Desktop.
Facets and analyzed test
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Linq;
using Raven.Client.Indexes;
using Xunit;
using Lucene.Net.Analysis.Standard;
namespace Raven.Tests.Faceted
{
public class AnalyzedWordCount : RavenTest
{
public void AnalyzedWordCountTest()
{
using (var store = NewDocumentStore())
{
ExecuteTest(store);
}
}
private void ExecuteTest(IDocumentStore store)
{
using (var s = store.OpenSession())
{
s.Store(new FacetSetup
{
Id = "facets/TextFacets",
Facets = new List<Facet> { new Facet { Name = "Text" } }
});
s.SaveChanges();
store.DatabaseCommands.PutIndex("testIndex",
new IndexDefinitionBuilder<TestDoc>
{
Map = docs => from doc in docs select new { doc.Text },
Analyzers = { { x => x.Text, typeof(StandardAnalyzer).AssemblyQualifiedName } }
});
s.Store(new TestDoc { Text = "The Quick Brown Fox Jumped Over The Moon" });
s.Store(new TestDoc { Text = "The Brown Book" });
s.Store(new TestDoc { Text = "Button Moon" });
s.SaveChanges();
// Issue a query to make sure the index isn't stale
var result = s.Query<TestDoc>("testIndex")
.Customize(x => x.WaitForNonStaleResults())
.ToList();
//WaitForUserToContinueTheTest(store as EmbeddableDocumentStore);
var facetResults = s.Query<TestDoc>("testIndex")
.ToFacets("facets/TextFacets");
var textFacets = facetResults["Text"];
foreach (var facet in textFacets)
{
Console.WriteLine("{0} - {1}", facet.Range, facet.Count);
}
Assert.Equal(8, textFacets.Count());
Assert.Equal(2, textFacets.Single(x => x.Range == "moon").Count);
Assert.Equal(2, textFacets.Single(x => x.Range == "brown").Count);
Assert.Equal(1, textFacets.Single(x => x.Range == "fox").Count);
Assert.Equal(1, textFacets.Single(x => x.Range == "quick").Count);
// etc.....
//There shouldn't be any facet results for "the"
Assert.Equal(0, textFacets.Where(x => x.Range == "the").Count());
}
}
}
class TestDoc
{
public string Id { get; set; }
public string Text { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment