【发布时间】:2012-04-12 23:10:55
【问题描述】:
在我开始之前,让我说我是 RavenDB 的新手。
我目前正在评估它,并且正在使用 RavenDB-Build-616 构建。我的服务器正在运行,即我手动启动了“Raven.Server.exe”,并有以下测试代码
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);
}
}
这样调用的地方
using (var documentStore = new DocumentStore { Url = documentStoreLocation, DefaultDatabase = "ravenTest-" + DateTime.Now.Ticks })
{
documentStore.Initialize();
RavenFullTextSearchDemo ravenFullTextSearchMessAround = new RavenFullTextSearchDemo(documentStore);
ravenFullTextSearchMessAround.Run();
}
发生了一些奇怪的事情,因为我似乎需要 Thread.Sleep 才能将新的用户对象持久保存到磁盘。如果我没有那个 Thread.Sleep,我永远不会在“PrintCurrentUsers”方法调用中看到任何打印出来的东西。
这是我在没有 Thread.Sleep 的情况下得到的输出
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++ 当前用户列表是:
寻找姓名以“Paul”开头的用户
简单开头 保罗·琼斯,
复数以 保罗·琼斯,
虽然这是我使用 Thread.Sleep 得到的输出
当前用户列表为:
用户名:保罗·布莱克, 用户名:保罗·本森, 用户名:保罗·琼斯, 用户名:彼得·布莱克, 用户名:保罗·西蒙斯,
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++
寻找姓名以“Paul”开头的用户
简单开头 保罗·布莱克 保罗·本森 保罗·琼斯 保罗·西蒙斯,
复数以 保罗·布莱克 保罗·本森 保罗·琼斯 保罗·西蒙斯,
我做错了什么。我在其他地方有一些其他代码可以插入一堆用户并立即获取它们,这似乎可以正常工作。
有什么线索吗?
【问题讨论】:
-
Simon 是正确的,将 .Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(5))) 行添加到您的查询中,使其最多等待 5 秒以使索引刷新。这是 Raven 遵循的“默认安全”范式的一部分,提供陈旧数据比提供数据更好。
标签: ravendb