Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created June 8, 2012 09:22
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 mattwarren/2894675 to your computer and use it in GitHub Desktop.
Save mattwarren/2894675 to your computer and use it in GitHub Desktop.
Querying for missing fields in RavenDB
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Raven.Abstractions.Indexing;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Json.Linq;
namespace Raven.Tryouts
{
public class CustomerIndex : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinition
{
Map = @"from doc in docs.Users
select new {
Name = doc.Name,
StoppedBeatingTheWifeExists = doc.StoppedBeatingTheWife != null
//StoppedBeatingTheWifeExists = doc.Inner.ContainsKey(""StoppedBeatingTheWife"") ? true : false,
})"
};
}
}
public class UserV1
{
public string Id { get; set; }
public String Name { get; set; }
}
public class UserV2
{
public string Id { get; set; }
public String Name { get; set; }
public bool? StoppedBeatingTheWife { get; set; }
}
class QueryingOnMissingFields
{
public void CanFindDocsWithMissingFields()
{
using (var store = new EmbeddableDocumentStore
{
DataDirectory = @"D:\Source\__RavenDB__\ravendb\Raven.Tryouts\bin\missingfields",
UseEmbeddedHttpServer = true,
}.Initialize())
{
bool addData = true;
if (addData)
{
var metadata = RavenJObject.Parse("{'Raven-Clr-Type': 'Raven.Tryouts.UserV2', 'Raven-Entity-Name': 'Users' }");
store.DatabaseCommands.Put("users/1", null, RavenJObject.FromObject(new UserV1 {Name = "A"}), metadata);
store.DatabaseCommands.Put("users/2", null, RavenJObject.FromObject(new UserV2 {Name = "B", StoppedBeatingTheWife = false}), metadata);
store.DatabaseCommands.Put("users/3", null, RavenJObject.FromObject(new UserV2 {Name = "C", StoppedBeatingTheWife = true}), metadata);
store.DatabaseCommands.Put("users/4", null, RavenJObject.FromObject(new UserV2 {Name = "D", StoppedBeatingTheWife = null}), metadata);
}
var sp = Stopwatch.StartNew();
new CustomerIndex().Execute(store);
Console.WriteLine("indexing...");
while (store.DatabaseCommands.GetStatistics().StaleIndexes.Length > 0)
{
Console.Write("\r{0:#,#} - {1:#,#}", store.DatabaseCommands.GetStatistics().Indexes[0].IndexingAttempts,
store.DatabaseCommands.GetStatistics().Indexes[0].ReduceIndexingAttempts);
Thread.Sleep(100);
}
var stats = store.DatabaseCommands.GetStatistics();
Console.WriteLine("Indexed {0} docs in {1}", String.Join(", ", stats.Indexes.Select(x => new { x.Name, x.IndexingSuccesses, x.IndexingErrors })), sp.Elapsed);
using (var session = store.OpenSession())
{
var stopBeatingTheWifeExists = session.Advanced.LuceneQuery<UserV2>("CustomerIndex")
.WhereEquals("StoppedBeatingTheWifeExists", "true")
.ToList();
Console.WriteLine("StoppedBeatingTheWifeExists:true {0}", String.Join(", ", stopBeatingTheWifeExists.Select(x => x.Id)));
var stopBeatingTheWifeDoesNotExist = session.Advanced.LuceneQuery<UserV2>("CustomerIndex")
.WhereEquals("StoppedBeatingTheWifeExists", "false")
.ToList();
Console.WriteLine("StoppedBeatingTheWifeExists:false {0}", String.Join(", ", stopBeatingTheWifeDoesNotExist.Select(x => x.Id)));
}
Console.WriteLine("Finished, press <ENTER> to exit");
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment