RavenDB保存到磁盘查询

时间:2012-04-12 15:06:18

标签: 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以便将新的User对象持久化到磁盘。如果我没有那个Thread.Sleep,我从未在“PrintCurrentUsers”方法调用中看到任何打印出来。

这是我得到的没有Thread.Sleep

的输出

+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++ 当前用户列表是:

寻找姓名以'Paul'开头的用户

简单的开头 保罗琼斯,

复杂的开头 保罗琼斯,

虽然这是我用Thread.Sleep

得到的输出

当前的用户列表是:

用户名:Paul Black, 用户名:Paul Benson, 用户名:Paul Jones, 用户名:Peter Black, 用户名:Paul Simons,

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

寻找姓名以'Paul'开头的用户

简单的开头 保罗布莱克, 保罗本森, 保罗琼斯, 保罗西蒙斯,

复杂的开头 保罗布莱克, 保罗本森, 保罗琼斯, 保罗西蒙斯,

我做错了什么。我在其他地方有一些其他代码可以插入一堆用户并直接获取它们,这似乎可以正常工作。

有人提出任何线索吗?

1 个答案:

答案 0 :(得分:5)

用户正在持久化,但索引在后台更新,您正在查询索引(这是按设计)。请参阅:http://ravendb.net/docs/client-api/querying

你可以告诉Raven等到查询中的所有陈旧数据。

有关如何等待索引更新的示例,请参阅此页面:http://ravendb.net/docs/client-api/querying/stale-indexes。 (特别是,您需要将方法更改为:

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