RavenDB 4 - Querying Id doesn't work for map-reduce indexes with ProjectInto

32 views
Skip to first unread message

Andrej Krivulčík

unread,
Feb 21, 2018, 12:32:10 PM2/21/18
to RavenDB - 2nd generation document database
In 4.0.2-nightly-20180221-1351, querying on Id doesn't work with map-reduce indexes.

The query constructed by the code below is the following: from index 'DocsIndex' where Id = $p0 select id() as Id, StrVal

The exception thrown:

Raven.Client.Exceptions.RavenException : System.ArgumentException: The field 'id()' is not indexed, cannot query/sort on fields that are not indexed
   at Raven.Server.Documents.Indexes.Index.ThrowInvalidField(String f) in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 2285
   at Raven.Server.Documents.Indexes.Index.AssertQueryDoesNotContainFieldsThatAreNotIndexed(QueryMetadata metadata) in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 2246
   at Raven.Server.Documents.Indexes.Index.<QueryInternal>d__150`1.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 1846
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Indexes.Index.<Query>d__149.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 1830
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Queries.QueryRunner.<ExecuteQuery>d__5.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Queries\QueryRunner.cs:line 54
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Handlers.QueriesHandler.<Query>d__3.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Handlers\QueriesHandler.cs:line 112
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Handlers.QueriesHandler.<Post>d__0.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Handlers\QueriesHandler.cs:line 42
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Routing.RequestRouter.<HandlePath>d__6.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Routing\RequestRouter.cs:line 97
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Raven.Server.RavenServerStartup.<RequestHandler>d__11.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\RavenServerStartup.cs:line 159
---- System.ArgumentException : System.ArgumentException: The field 'id()' is not indexed, cannot query/sort on fields that are not indexed
   at Raven.Server.Documents.Indexes.Index.ThrowInvalidField(String f) in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 2285
   at Raven.Server.Documents.Indexes.Index.AssertQueryDoesNotContainFieldsThatAreNotIndexed(QueryMetadata metadata) in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 2246
   at Raven.Server.Documents.Indexes.Index.<QueryInternal>d__150`1.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 1846
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Indexes.Index.<Query>d__149.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Indexes\Index.cs:line 1830
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Queries.QueryRunner.<ExecuteQuery>d__5.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Queries\QueryRunner.cs:line 54
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Handlers.QueriesHandler.<Query>d__3.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Handlers\QueriesHandler.cs:line 112
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Documents.Handlers.QueriesHandler.<Post>d__0.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Documents\Handlers\QueriesHandler.cs:line 42
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Raven.Server.Routing.RequestRouter.<HandlePath>d__6.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\Routing\RequestRouter.cs:line 97
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Raven.Server.RavenServerStartup.<RequestHandler>d__11.MoveNext() in C:\Builds\RavenDB-4.0-Nightly\src\Raven.Server\RavenServerStartup.cs:line 159


When the index is map-only, the query is slightly different (Id vs. id()) and works correctly: from index 'DocsIndex' where id() = $p0 select id() as Id, StrVal

When not projecting into the class, the query doesn't contain the select part (from index 'DocsIndex' where Id = $p0) and works, but our code kinda needs the projection part.

Reproduction code:

using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Linq;
using Raven.TestDriver;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace RavenDB4Tests.Tests
{
   
public class TestIdQuery : RavenTestDriver<ServerLocator>
   
{
       
[Fact]
       
public async Task TestIdQueryTest()
       
{
           
using (var store = GetDocumentStore())
           
{
               
new DocsIndex().Execute(store);
               
using (var session = store.OpenAsyncSession())
               
{
                   
for (int i = 0; i < 10; i++)
                   
{
                        await session
.StoreAsync(new Doc { Id = "doc-" + i, StrVal = i.ToString() });
                   
}
                    await session
.SaveChangesAsync();

                   
WaitForIndexing(store);

                   
var query = session.Query<Doc, DocsIndex>()
                       
.Where(x => x.Id == "doc-1")
                       
.ProjectInto<Doc>(); // without ProjectInto, the query works: from index 'DocsIndex' where Id = $p0
                   
var results = await query.ToListAsync();

                   
Assert.Single(results);
               
}
           
}
       
}

       
public class Doc
       
{
           
public string Id { get; set; }
           
public string StrVal { get; set; }
       
}

       
public class DocsIndex : AbstractIndexCreationTask<Doc>
       
{
           
public DocsIndex()
           
{
               
Map = docs =>
                   
from doc in docs
                   
select new
                   
{
                        doc
.Id,
                        doc
.StrVal,
                   
};
               
// Without reduce part, the query looks like this: from index 'DocsIndex' where id() = $p0 select id() as Id, StrVal
               
// and works correctly.
               
Reduce = results =>
                   
from result in results
                   
group result by result.Id
                   
into g
                    let doc
= g.First()
                   
select new
                   
{
                        doc
.Id,
                        doc
.StrVal,
                   
};
           
}
       
}
   
}
}


Oren Eini (Ayende Rahien)

unread,
Feb 22, 2018, 3:21:31 AM2/22/18
to ravendb
Thanks, fixed and will be in the next nightly

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages