RavenDB 保存到磁盘查询 [英] RavenDB Saving to disk query

查看:41
本文介绍了RavenDB 保存到磁盘查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,让我说我是 RavenDB 的新手.

Before I start let me say I am new to RavenDB.

我目前只是在评估它,并且正在使用 RavenDB-Build-616 版本.我已经运行了服务器,即我已经手动启动了Raven.Server.exe",并有以下测试代码

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

哪里会像这样调用

    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"方法调用中看到任何打印出来.

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.

这是我在没有 Thread.Sleep 的情况下得到的输出

This is the output I get with no Thread.Sleep

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

++++++++++++++++++++++++++++++++++++++++++++++++++++++++ The current list of users is :

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

Looking for users with Name starting with 'Paul'

简单从保罗·琼斯,

复杂的开头保罗·琼斯,

Complex starts with Paul Jones,

虽然这是我使用 Thread.Sleep 获得的输出

Whilst this is the output I get with the Thread.Sleep

当前用户列表是:

用户名:保罗·布莱克,用户名:保罗·本森,用户名:保罗琼斯,用户名:彼得布莱克,用户名:保罗西蒙斯,

UserName : Paul Black, UserName : Paul Benson, UserName : Paul Jones, UserName : Peter Black, UserName : Paul Simons,

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

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

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

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.

有什么线索吗?

推荐答案

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

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

您可以告诉 Raven 等到所有陈旧数据都在查询中.

You can tell Raven to wait until all stale data is in the query.

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

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

这篇关于RavenDB 保存到磁盘查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆