RavenDB 嵌套属性索引查询

发布于 2024-12-06 17:29:24 字数 3378 浏览 1 评论 0原文

sss我目前有一个名为 SchoolMetrics 的索引,它聚合了 School 字段上的多个字段作为键并生成如下文档:

{ 
    School: {
      SchoolId: 1234
      Name: "asdf"
    }
    StudentCount: 1234,
    CourseCount: 1234
 }

我的索引映射定义为:

from s in docs.Metrics
where s.School != null
select new
{
    s.School,
    s.StudentCount,
    s.CourseCount
}

减少为:

from s in results
group s by s.School
into g
select new
{
    School= g.Key,
    StudentCount = g.Sum(x => x.StudentCount),
    CourseCount = g.Sum(x => x.CourseCount)
}

当我尝试执行如下查询时: http://localhost:8080/databases/Database/indexes/SchoolMetrics ?query=School.SchoolId:1234

它给了我这个错误:

 "System.ArgumentException: The field 'School.SchoolId' is not indexed, cannot query on fields that are not indexed
   at Raven.Database.Indexing.Index.IndexQueryOperation.AssertQueryDoesNotContainFieldsThatAreNotIndexes() in c:\Builds\raven\Raven.Database\Indexing\Index.cs:line 639
   at Raven.Database.Indexing.Index.IndexQueryOperation.<Query>d__24.MoveNext() in c:\Builds\raven\Raven.Database\Indexing\Index.cs:line 558
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at Raven.Database.DocumentDatabase.<>c__DisplayClass70.<Query>b__68(IStorageActionsAccessor actions) in c:\Builds\raven\Raven.Database\DocumentDatabase.cs:line 705
   at Raven.Storage.Esent.TransactionalStorage.ExecuteBatch(Action`1 action) in c:\Builds\raven\Raven.Storage.Esent\TransactionalStorage.cs:line 378
   at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action) in c:\Builds\raven\Raven.Storage.Esent\TransactionalStorage.cs:line 341
   at Raven.Database.DocumentDatabase.Query(String index, IndexQuery query) in c:\Builds\raven\Raven.Database\DocumentDatabase.cs:line 652
   at Raven.Database.Server.Responders.Index.PerformQueryAgainstExistingIndex(IHttpContext context, String index, IndexQuery indexQuery, Guid& indexEtag) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 150
   at Raven.Database.Server.Responders.Index.ExecuteQuery(IHttpContext context, String index, Guid& indexEtag) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 136
   at Raven.Database.Server.Responders.Index.GetIndexQueryRessult(IHttpContext context, String index) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 92
   at Raven.Database.Server.Responders.Index.OnGet(IHttpContext context, String index) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 84
   at Raven.Database.Server.Responders.Index.Respond(IHttpContext context) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 46
   at Raven.Http.HttpServer.DispatchRequest(IHttpContext ctx) in c:\Builds\raven\Raven.Http\HttpServer.cs:line 399
   at Raven.Http.HttpServer.HandleActualRequest(IHttpContext ctx) in c:\Builds\raven\Raven.Http\HttpServer.cs:line 222"

更奇怪的是,当我尝试查询 StudentCount 或 CourseCount 时它有效的字段...我尝试在 School.SchoolId 字段上添加分析器,但这似乎没有帮助...我也尝试压平生成的文档,但出现了相同的错误。我错过了什么吗?

sssI currently have an index called SchoolMetrics that aggregates several fields on the School field as the key and produces documents like this:

{ 
    School: {
      SchoolId: 1234
      Name: "asdf"
    }
    StudentCount: 1234,
    CourseCount: 1234
 }

My index map is defined as:

from s in docs.Metrics
where s.School != null
select new
{
    s.School,
    s.StudentCount,
    s.CourseCount
}

And the reduce is:

from s in results
group s by s.School
into g
select new
{
    School= g.Key,
    StudentCount = g.Sum(x => x.StudentCount),
    CourseCount = g.Sum(x => x.CourseCount)
}

When I try to do a query such a:
http://localhost:8080/databases/Database/indexes/SchoolMetrics?query=School.SchoolId:1234

It gives me this error:

 "System.ArgumentException: The field 'School.SchoolId' is not indexed, cannot query on fields that are not indexed
   at Raven.Database.Indexing.Index.IndexQueryOperation.AssertQueryDoesNotContainFieldsThatAreNotIndexes() in c:\Builds\raven\Raven.Database\Indexing\Index.cs:line 639
   at Raven.Database.Indexing.Index.IndexQueryOperation.<Query>d__24.MoveNext() in c:\Builds\raven\Raven.Database\Indexing\Index.cs:line 558
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at Raven.Database.DocumentDatabase.<>c__DisplayClass70.<Query>b__68(IStorageActionsAccessor actions) in c:\Builds\raven\Raven.Database\DocumentDatabase.cs:line 705
   at Raven.Storage.Esent.TransactionalStorage.ExecuteBatch(Action`1 action) in c:\Builds\raven\Raven.Storage.Esent\TransactionalStorage.cs:line 378
   at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action) in c:\Builds\raven\Raven.Storage.Esent\TransactionalStorage.cs:line 341
   at Raven.Database.DocumentDatabase.Query(String index, IndexQuery query) in c:\Builds\raven\Raven.Database\DocumentDatabase.cs:line 652
   at Raven.Database.Server.Responders.Index.PerformQueryAgainstExistingIndex(IHttpContext context, String index, IndexQuery indexQuery, Guid& indexEtag) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 150
   at Raven.Database.Server.Responders.Index.ExecuteQuery(IHttpContext context, String index, Guid& indexEtag) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 136
   at Raven.Database.Server.Responders.Index.GetIndexQueryRessult(IHttpContext context, String index) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 92
   at Raven.Database.Server.Responders.Index.OnGet(IHttpContext context, String index) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 84
   at Raven.Database.Server.Responders.Index.Respond(IHttpContext context) in c:\Builds\raven\Raven.Database\Server\Responders\Index.cs:line 46
   at Raven.Http.HttpServer.DispatchRequest(IHttpContext ctx) in c:\Builds\raven\Raven.Http\HttpServer.cs:line 399
   at Raven.Http.HttpServer.HandleActualRequest(IHttpContext ctx) in c:\Builds\raven\Raven.Http\HttpServer.cs:line 222"

What's weirder is that when I try querying on the StudentCount or CourseCount fields it works... I've tried adding an analyzer on the School.SchoolId field but that doesn't seem to help... I've also tried flattening out the resulting document and I get the same error. Am I missing something?

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

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

发布评论

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

评论(2

隐诗 2024-12-13 17:29:24

除了托马斯的注释之外,您还必须了解我们正在将索引的最终输出视为索引项。
如果您正在为一个复杂的对象建立索引,它将被索引为 Json 值,而不是您可以进一步查询的内容。

In addition to Thomas' note, you have to understand that we are looking at the final output from the index as the indexed item.
And if you are indexing a complex object, it is going to be indexed as a Json value, not as something that you can query on further.

一梦浮鱼 2024-12-13 17:29:24

您正在按 School 对象进行分组。指数扁平化怎么样?

from s in docs.Metrics
where s.School != null
select new
{
    SchoolId = s.School.SchoolId,
    s.StudentCount,
    s.CourseCount
}

from s in results
group s by s.SchoolId
into g
select new
{
    SchoolId = g.Key,
    StudentCount = g.Sum(x => x.StudentCount),
    CourseCount = g.Sum(x => x.CourseCount)
}

You are grouping by the School object. How about flattening the index?

from s in docs.Metrics
where s.School != null
select new
{
    SchoolId = s.School.SchoolId,
    s.StudentCount,
    s.CourseCount
}

from s in results
group s by s.SchoolId
into g
select new
{
    SchoolId = g.Key,
    StudentCount = g.Sum(x => x.StudentCount),
    CourseCount = g.Sum(x => x.CourseCount)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文