Reputation: 878
I am an absolute beginner to RavenDb, but very experienced in EF and repository pattern using SQL. Finally getting around to trying it out, so I created a simple console application using RavenDB.Embedded with the intention of replacing an aging SQLLite EF implementation on another application.
Anyhow I am attempting to write 1000+ records to the store, and simply read it out with a stream. The following is some sample code:
var store = new EmbeddableDocumentStore
{
DataDirectory = "data"
};
store.Initialize();
using (IDocumentSession session = store.OpenSession())
{
for (var t = 1; t < 1000; t++)
{
var subtest = new Test
{
Id = new Guid(),
SubTest = new SubTest
{
Name = "NewTest",
Id = new Guid()
}
};
session.Store(subtest);
}
session.SaveChanges();
}
store
.DatabaseCommands
.PutIndex("SubTest/All",
new IndexDefinitionBuilder<SubTest>()
{
Map = tests => tests
.Select(test => new
{
SubTest = test.SubTest,
Id = test.Id
})
});
using (IDocumentSession session = store.OpenSession())
{
var query = session.Query<Test>("SubTest/All");
var listt = query.ToList();
using (var enumerator = session.Advanced.Stream(query))
{
while (enumerator.MoveNext())
{
var t = enumerator.Current.Document;
System.Console.WriteLine(t.Id);
}
};
}
System.Console.ReadLine();
I realize this is pretty rudimentary, and I not so much looking for somebody to correct my code. Just hoping somebody can help me understand how the index thing is supposed to work with a stream and maybe explain why the enumerator has no results. If I use .Load in stead of .Query I get results (128 of them) so I'm pretty sure the rest of it is working.
I tried the PutIndex thing above but I also tried AbstractIndexCreationTask with similarly confusing results. I cant really find a whole lot of info about this online strangely enough, is this really that uncommon?
Upvotes: 0
Views: 174
Reputation: 878
So I finally figured this out. Seems the documentation is lacking a few key details in regards to the Streaming API. Basically I was on the right track, I just had to actually create the index earlier in the code. I ended up using the AbstractIndexCreationTask, which I like better than the PutIndex method, although Im not sure if they are doing the same thing or not. In any case, this works:
var store = new EmbeddableDocumentStore
{
DataDirectory = "data"
};
store.Initialize();
**IndexCreation.CreateIndexes(typeof(TestIndex).Assembly, store);**
using (IDocumentSession session = store.OpenSession())
{
for (var t = 1; t < 100; t++)
{
var subtest = new Test
{
Id = new Guid(),
SubTest = new SubTest
{
Name = "NewTest",
Id = new Guid()
}
};
session.Store(subtest);
}
session.SaveChanges();
}
using (IDocumentSession session = store.OpenSession())
{
IQueryable<Test> query = session.Query<Test, TestIndex>();
var enumerator = session.Advanced.Stream(query);
while (enumerator.MoveNext())
{
var t = enumerator.Current.Document;
System.Console.WriteLine(t.Id);
}
}
And My Index:
public class TestIndex : AbstractIndexCreationTask<Test>
{
public TestIndex()
{
this.Map = tests =>
from t in tests
select new
{
t.Id,
t.SubTest
};
}
}
Upvotes: 1