RavenDB Index Querying on Nested Properties
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:\B开发者_如何学JAVAuilds\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?
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.
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)
}
精彩评论