从嵌入式 RavenDB 中的索引检索结果时出现问题

发布于 2024-12-06 21:41:45 字数 2741 浏览 1 评论 0原文

当我使用 .Net 客户端 API 和基于服务器的 RavenDb 进行查询时,我有一个索引,效果非常好。

但是,如果我将 RavenDb 更改为嵌入式类型,那么我无法直接查询索引,除非我首先查询索引使用的文档。

例如,如果我有以下文档对象,它们作为单独的集合驻留在 RavenDb 中:

private class TestParentDocument
        {
            public string Id { get { return GetType().Name + "/" + AggregateRootId; } }
            public Guid AggregateRootId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }

private class TestChildProductFlagDocument
        {
            public string TestParentDocumentId { get; set; }
            public short ProductFlagTypeId { get; set; }
        }

那么我有以下对象,它表示索引映射到的输出文档:

private class TestJoinIndexOutput
        {
            public string TestParentDocumentId { get; set; }
            public string Name { get; set; }
            public short ProductFlagTypeId { get; set; }
        }

这是索引定义:

private class TestJoinIndex : AbstractIndexCreationTask<TestChildProductFlagDocument, TestJoinIndexOutput>
        {
            public TestJoinIndex()
            {
                Map = docs => from doc in docs
                              select new
                              {
                                  TestParentDocumentId = doc.TestParentDocumentId,
                                  ProductFlagTypeId = doc.ProductFlagTypeId
                              };

                TransformResults = (database, results) =>
                    from result in results
                    let parentDoc = database.Load<TestParentDocument>(result.TestParentDocumentId)
                    select new
                    {
                        TestParentDocumentId = result.TestParentDocumentId,
                        ProductFlagTypeId = result.ProductFlagTypeId, 
                        Name = parentDoc.Name
                    };
            }

我调用索引的代码如下所示:

var theJoinIndexes = ravenSession.Query<TestJoinIndexOutput, TestJoinIndex>().ToList();

这几乎立即返回并失败,除非我执行以下操作:

var theParentDocuments = ravenSession.Query<TestParentDocument>().ToList();
var theJoinIndexes = ravenSession.Query<TestJoinIndexOutput, TestJoinIndex>().ToList();

我的 Ravendb 嵌入式定义如下所示:

docStore = new EmbeddableDocumentStore
                 {
                    UseEmbeddedHttpServer = false,
                    RunInMemory = true

                };
                docStore.Configuration.Port = 7777;
                docStore.Initialize();

                IndexCreation.CreateIndexes(typeof(TestJoinIndex).Assembly, docstore);

I have an index that works great when I query it using the .Net client API with a server based RavenDb.

However, if I change the RavenDb to an embedded type then I cannot query the index directly unless I first query the document that the index uses.

For instance if I have the following document objects which reside as separate collections in the RavenDb:

private class TestParentDocument
        {
            public string Id { get { return GetType().Name + "/" + AggregateRootId; } }
            public Guid AggregateRootId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }

private class TestChildProductFlagDocument
        {
            public string TestParentDocumentId { get; set; }
            public short ProductFlagTypeId { get; set; }
        }

Then I have the following object which represents the output document that the index maps to:

private class TestJoinIndexOutput
        {
            public string TestParentDocumentId { get; set; }
            public string Name { get; set; }
            public short ProductFlagTypeId { get; set; }
        }

Here is the index definition:

private class TestJoinIndex : AbstractIndexCreationTask<TestChildProductFlagDocument, TestJoinIndexOutput>
        {
            public TestJoinIndex()
            {
                Map = docs => from doc in docs
                              select new
                              {
                                  TestParentDocumentId = doc.TestParentDocumentId,
                                  ProductFlagTypeId = doc.ProductFlagTypeId
                              };

                TransformResults = (database, results) =>
                    from result in results
                    let parentDoc = database.Load<TestParentDocument>(result.TestParentDocumentId)
                    select new
                    {
                        TestParentDocumentId = result.TestParentDocumentId,
                        ProductFlagTypeId = result.ProductFlagTypeId, 
                        Name = parentDoc.Name
                    };
            }

My code to call the index looks like so:

var theJoinIndexes = ravenSession.Query<TestJoinIndexOutput, TestJoinIndex>().ToList();

This returns almost immediately and fails unless I do the following:

var theParentDocuments = ravenSession.Query<TestParentDocument>().ToList();
var theJoinIndexes = ravenSession.Query<TestJoinIndexOutput, TestJoinIndex>().ToList();

My Ravendb embedded definition looks like so:

docStore = new EmbeddableDocumentStore
                 {
                    UseEmbeddedHttpServer = false,
                    RunInMemory = true

                };
                docStore.Configuration.Port = 7777;
                docStore.Initialize();

                IndexCreation.CreateIndexes(typeof(TestJoinIndex).Assembly, docstore);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的鱼塘能养鲲 2024-12-13 21:41:45

您无需等待索引完成,请调用 WaitForNonStaleResultsAsOfNow

You aren't waiting for indexing to complete, call WaitForNonStaleResultsAsOfNow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文