【问题标题】:RavenDB Index Querying on Nested PropertiesRavenDB 对嵌套属性的索引查询
【发布时间】:2011-09-26 17:23:22
【问题描述】:

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
}

reduce 是:

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 字段上添加分析器,但这似乎没有帮助...我也尝试展平生成的文档,我得到了同样的错误。我错过了什么吗?

【问题讨论】:

    标签: indexing mapreduce ravendb


    【解决方案1】:

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

    【讨论】:

    • 那么,RavenDB 中是否有任何选项可以同时过滤存储在嵌套集合中的复杂对象的多个属性,而无需扇出索引?我描述了我的情况here,如果可以的话,请提供帮助。
    【解决方案2】:

    您正在按 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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多