sssI目前有一个名为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/数据库/数据库/索引/SchoolMetrics ? = 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:BuildsravenRaven.DatabaseIndexingIndex.cs:line 639
at Raven.Database.Indexing.Index.IndexQueryOperation.<Query>d__24.MoveNext() in c:BuildsravenRaven.DatabaseIndexingIndex.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:BuildsravenRaven.DatabaseDocumentDatabase.cs:line 705
at Raven.Storage.Esent.TransactionalStorage.ExecuteBatch(Action`1 action) in c:BuildsravenRaven.Storage.EsentTransactionalStorage.cs:line 378
at Raven.Storage.Esent.TransactionalStorage.Batch(Action`1 action) in c:BuildsravenRaven.Storage.EsentTransactionalStorage.cs:line 341
at Raven.Database.DocumentDatabase.Query(String index, IndexQuery query) in c:BuildsravenRaven.DatabaseDocumentDatabase.cs:line 652
at Raven.Database.Server.Responders.Index.PerformQueryAgainstExistingIndex(IHttpContext context, String index, IndexQuery indexQuery, Guid& indexEtag) in c:BuildsravenRaven.DatabaseServerRespondersIndex.cs:line 150
at Raven.Database.Server.Responders.Index.ExecuteQuery(IHttpContext context, String index, Guid& indexEtag) in c:BuildsravenRaven.DatabaseServerRespondersIndex.cs:line 136
at Raven.Database.Server.Responders.Index.GetIndexQueryRessult(IHttpContext context, String index) in c:BuildsravenRaven.DatabaseServerRespondersIndex.cs:line 92
at Raven.Database.Server.Responders.Index.OnGet(IHttpContext context, String index) in c:BuildsravenRaven.DatabaseServerRespondersIndex.cs:line 84
at Raven.Database.Server.Responders.Index.Respond(IHttpContext context) in c:BuildsravenRaven.DatabaseServerRespondersIndex.cs:line 46
at Raven.Http.HttpServer.DispatchRequest(IHttpContext ctx) in c:BuildsravenRaven.HttpHttpServer.cs:line 399
at Raven.Http.HttpServer.HandleActualRequest(IHttpContext ctx) in c:BuildsravenRaven.HttpHttpServer.cs:line 222"
更奇怪的是,当我尝试查询StudentCount或CourseCount字段它的工作原理…我试过在学校里装个分析仪。学校id字段,但这似乎没有帮助…我也试过把结果文档弄平,也得到了同样的错误。我错过什么了吗?
除了Thomas的注释之外,您必须理解我们将索引的最终输出视为索引项。如果你要索引一个复杂的对象,它将被索引为Json值,而不是你可以进一步查询的东西
按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)
}