在我开始之前,让我说我是 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 才能将新的 User 对象持久保存到磁盘。如果我在那里没有那个 Thread.Sleep,我永远不会在“PrintCurrentUsers”方法调用中看到任何打印出来的东西。
这是我在没有 Thread.Sleep 的情况下得到的输出
++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++ 当前用户列表为:
寻找姓名以“Paul”开头的用户
简单从保罗琼斯开始,
复杂从保罗琼斯开始,
虽然这是我使用 Thread.Sleep 得到的输出
当前用户列表是:
用户名:Paul Black,用户名:Paul Benson,用户名:Paul Jones,用户名:Peter Black,用户名:Paul Simons,
++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++
寻找姓名以“Paul”开头的用户
简单从保罗·布莱克、保罗·本森、保罗·琼斯、保罗·西蒙斯开始,
复杂从保罗布莱克开始,保罗本森,保罗琼斯,保罗西蒙斯,
我究竟做错了什么。我在其他地方有一些其他代码可以插入一堆用户并立即获取它们,这似乎工作正常。
任何线索任何人?