Skip to content

Instantly share code, notes, and snippets.

@ricardobrandao
Created October 26, 2016 13:58
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 ricardobrandao/7c85ace05e43a2c1190c09f89343e39d to your computer and use it in GitHub Desktop.
Save ricardobrandao/7c85ace05e43a2c1190c09f89343e39d to your computer and use it in GitHub Desktop.
RavenDb projection of empty lists
using System.Collections.Generic;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Indexes;
namespace TestProjections.Console
{
class Program
{
static void Main(string[] args)
{
var store = new DocumentStore
{
Url = "http://localhost.fiddler:8080",
DefaultDatabase = "Hello"
}.Initialize(true);
new Mock_SearchIndex().Execute(store);
using (var session = store.OpenSession())
{
//session.Store(new MockPage
//{
// Title = "Javascript page",
// Tags = new List<string> { "javascript", "webdevelopment" }
//});
//session.Store(new MockPage
//{
// Title = "C# page",
// Tags = new List<string> { "dotnet", "csharp" }
//});
//session.Store(new MockBlogPost
//{
// Title = "A blogpost about C#",
// Tags = new List<string> { "dotnet", "csharp" }
//});
var results = session.Query<Mock_SearchIndex.SearchResult, Mock_SearchIndex>()
.ProjectFromIndexFieldsInto<Mock_SearchIndex.SearchResult>()
.Search(x => x.Title, "C#")
.Skip(0)
.Take(5)
.ToList();
}
}
public class MockPage
{
public string Id { get; set; }
public string Title { get; set; }
public IEnumerable<string> Tags { get; set; }
}
public class MockBlogPost
{
public string Id { get; set; }
public string Title { get; set; }
public IEnumerable<string> Tags { get; set; }
}
public class Mock_SearchIndex : AbstractMultiMapIndexCreationTask<Mock_SearchIndex.SearchResult>
{
public class SearchResult
{
public string Id { get; set; }
public string Title { get; set; }
public IEnumerable<string> Tags { get; set; }
public string ContentType { get; set; }
}
public Mock_SearchIndex()
{
AddMap<MockBlogPost>(posts => from post in posts
select new SearchResult
{
Id = post.Id,
Title = post.Title,
ContentType = "blogPost",
Tags = post.Tags
});
AddMap<MockPage>(pages => from page in pages
select new SearchResult
{
Id = page.Id,
Title = page.Title,
ContentType = "page",
// the problem is that we are return a empty list here
Tags = new string[] { }
});
Index(r => r.Title, FieldIndexing.Analyzed);
StoreAllFields(FieldStorage.Yes);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment