Reputation: 2333
Before I start let me say I am new to RavenDB.
I am just evaluating it at the moment, and am using the RavenDB-Build-616 build. I have the server running, that is I have manually started "Raven.Server.exe", and have the following test code
public class RavenFullTextSearchDemo
{
private DocumentStore documentStore;
private List<string> firstNames = new List<string>() { "John", "Peter", "Paul", "Sam", "Brendon" };
private List<string> lastNames = new List<string>() { "Simons", "Black", "Benson", "Jones", "Breckwell" };
private Random rand = new Random();
public RavenFullTextSearchDemo(DocumentStore documentStore)
{
this.documentStore = documentStore;
}
public void Run()
{
IndexCreation.CreateIndexes(typeof(RavenFullTextSearchDemo).Assembly, this.documentStore);
using (IDocumentSession session = documentStore.OpenSession())
{
//add some random Users
for (int i = 0; i < 5; i++)
{
string name = string.Format("{0} {1},",
firstNames[rand.Next(firstNames.Count)], lastNames[rand.Next(lastNames.Count)]);
User newUser = new User { Name = name };
session.Store(newUser);
}
session.SaveChanges();
//Seem to have to give it some time to persist??? Seems very odd
//If I take the following line out, I dont get any users back at all
Thread.Sleep(3000);
PrintCurrentUsers(session);
var searchName = firstNames[rand.Next(firstNames.Count)];
Console.WriteLine(string.Format("Looking for users with Name starting with '{0}'\r\n", searchName));
Console.WriteLine("Simple starts with");
foreach (var person in Queryable.Where(session.Query<User, User_ByName_FullTextSearch>(), x => x.Name.StartsWith(searchName)))
{
Console.WriteLine(person.Name);
}
Console.WriteLine("\r\n");
Console.WriteLine("Complex starts with");
IQueryable<User> query = session.Query<User, User_ByName_FullTextSearch>();
query = searchName.Split().Aggregate(query, (current, part) => current.Where(x => x.Name.StartsWith(part)));
foreach (var person in query)
{
Console.WriteLine(person.Name);
}
}
}
private void PrintCurrentUsers(IDocumentSession session)
{
IList<User> users = new List<User>();
Console.WriteLine("The current list of users is :\r\n");
foreach (User user in session.Query<User>().ToList())
{
Console.WriteLine(string.Format("UserName : {0}", user.Name));
}
Console.WriteLine("\r\n\r\n");
}
}
public class User_ByName_FullTextSearch : AbstractIndexCreationTask<User>
{
public User_ByName_FullTextSearch()
{
Map = users => from user in users
select new { user.Name };
Index(x => x.Name, FieldIndexing.Analyzed);
}
}
Where this gets called like this
using (var documentStore = new DocumentStore { Url = documentStoreLocation, DefaultDatabase = "ravenTest-" + DateTime.Now.Ticks })
{
documentStore.Initialize();
RavenFullTextSearchDemo ravenFullTextSearchMessAround = new RavenFullTextSearchDemo(documentStore);
ravenFullTextSearchMessAround.Run();
}
There is something weird going on, as I seem to need the Thread.Sleep in order for the new User objects to be persisted to disk. If I do not have that Thread.Sleep in there, I never see anything printed out in the "PrintCurrentUsers" method call.
This is the output I get with no Thread.Sleep
++++++++++++++++++++++++++++++++++++++++++++++++++++++++ The current list of users is :
Looking for users with Name starting with 'Paul'
Simple starts with Paul Jones,
Complex starts with Paul Jones,
Whilst this is the output I get with the Thread.Sleep
The current list of users is :
UserName : Paul Black, UserName : Paul Benson, UserName : Paul Jones, UserName : Peter Black, UserName : Paul Simons,
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Looking for users with Name starting with 'Paul'
Simple starts with Paul Black, Paul Benson, Paul Jones, Paul Simons,
Complex starts with Paul Black, Paul Benson, Paul Jones, Paul Simons,
What am I doing wrong. I have some other code elsewhere that inserts a bunch of Users and gets them straight away, and that seems to work ok.
Any clues anyone?
Upvotes: 1
Views: 449
Reputation: 5493
The Users are being persisted, but the index is updated in the background, and you are querying against the index (this is by-design). See: http://ravendb.net/docs/client-api/querying
You can tell Raven to wait until all stale data is in the query.
See this page: http://ravendb.net/docs/client-api/querying/stale-indexes for an example of how to wait until the index is up to date. (Specifially, you need to change your method to:
private void PrintCurrentUsers(IDocumentSession session)
{
IList<User> users = new List<User>();
Console.WriteLine("The current list of users is :\r\n");
foreach (User user in session.Query<User>()
.Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(5)))
.ToList())
{
Console.WriteLine(string.Format("UserName : {0}", user.Name));
}
Console.WriteLine("\r\n\r\n");
}
Upvotes: 5