2012-04-12 81 views
1

在我開始之前讓我說我是RavenDB的新手。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

得到+++++++++++++++++++++++++++++++ +++++++++++++++++++++++++ 用戶的當前列表爲:

尋找用戶名稱以「保」

簡單的開始與 ·保羅·瓊斯,

複雜打頭 ·保羅·瓊斯,

雖然這是我的輸出用的Thread.Sleep得到

用戶的當前列表爲:

用戶名:保羅黑色, 用戶名:保羅·本森, 用戶名:保羅·瓊斯, 用戶名:彼得·布萊克, 用戶名:保羅·西蒙斯,

+++++++++++++++++++++++++++++++++++++ +++++++++++++++++++

正在尋找名字以'Paul'開頭的用戶

簡單的開始與 保羅黑色, 保羅·本森, 保羅·瓊斯, 保羅·西蒙斯,

情結開始與 保羅黑色, 保羅·本森, 保羅·瓊斯, 保羅·西蒙斯,

我究竟做錯了什麼。我在其他地方有其他代碼插入一堆用戶,並立即獲得它們,這似乎工作正常。

任何線索有人嗎?

+0

西蒙是正確的,添加行.Customize(X => x.WaitForNonStaleResults(TimeSpan.FromSeconds(5)))來查詢,以允許它等待最多5秒刷新索引,這是Raven遵循的'默認安全'範式的一部分,更好地爲陳舊數據提供比無數據更好的服務 – JonVD 2012-04-12 15:13:21

+0

酷得到它謝謝 – sacha 2012-04-12 15:25:48

回答

5

用戶正在保留,但索引在後臺更新,並且您正在查詢索引(這是通過設計)。請參閱:http://ravendb.net/docs/client-api/querying

您可以告訴Raven等待所有陳舊的數據在查詢中。

請參閱本頁:http://ravendb.net/docs/client-api/querying/stale-indexes瞭解如何等待索引更新的示例。(Specifially,你需要改變你的方法:

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"); 
    } 
+0

公平競爭,完全排除了我。這是一個「EXPERT」級別的東西,不應該在單元測試之外使用。我試圖做的真的很奇怪嗎? – sacha 2012-04-12 15:22:41

+0

實際上,你在評估程序中做什麼*是一個單元測試:-)在現實中,您多久保存一些數據,然後立即重新載入它? – Simon 2012-04-12 15:24:04

+0

是的,很公平。謝謝你的回答。 – sacha 2012-04-12 15:25:37