Skip to content

Instantly share code, notes, and snippets.

@ayende
Forked from pdegenhardt/Nullable Index Fields Issue
Created February 24, 2013 07:51
Show Gist options
  • Save ayende/5023050 to your computer and use it in GitHub Desktop.
Save ayende/5023050 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Xunit;
namespace Raven.Tests.MailingList
{
public class NullableIssue : RavenTest
{
public class Person
{
public string Name { get; set; }
public string NickName { get; set; }
public DateTime? Birthday { get; set; }
}
public class PersonQuery
{
public string Query { get; set; }
}
public class PersonBrief
{
public string NickName { get; set; }
public DateTime? Birthday { get; set; }
}
public class Person_PersonBrief : AbstractIndexCreationTask<Person>
{
public Person_PersonBrief()
{
Map = docs => from doc in docs
select new {Query = new object[] {doc.Name, doc.NickName}};
TransformResults = (db, docs) =>
from doc in docs
select new {NickName = doc.NickName, Birthday = doc.Birthday};
Index("Query", FieldIndexing.Analyzed);
}
}
[Fact]
public void Main()
{
using (var store = NewDocumentStore())
{
store.ExecuteIndex(new Person_PersonBrief());
using (var session = store.OpenSession())
{
session.Store(new Person {Name = "Phil", NickName = "p1", Birthday = DateTime.Now.AddYears(-33)});
session.Store(new Person {Name = "John", NickName = "j1", Birthday = DateTime.Now.AddYears(-22)});
session.Store(new Person {Name = "Jamie", NickName = "j2"});
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var results = session.Query<PersonQuery, Person_PersonBrief>()
.Customize(x=>x.WaitForNonStaleResults())
.Where(x => x.Query.StartsWith("J"))
.AsProjection<PersonBrief>()
.ToList();
Assert.NotEmpty(results);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment