diff --git a/src/Raven.Server/Documents/Indexes/Persistence/Corax/CoraxQueryBuilder.cs b/src/Raven.Server/Documents/Indexes/Persistence/Corax/CoraxQueryBuilder.cs index adeb022984cf..e928c6f614c7 100644 --- a/src/Raven.Server/Documents/Indexes/Persistence/Corax/CoraxQueryBuilder.cs +++ b/src/Raven.Server/Documents/Indexes/Persistence/Corax/CoraxQueryBuilder.cs @@ -253,7 +253,7 @@ internal static IQueryMatch BuildQuery(Parameters builderParameters, out OrderMe coraxQuery = MaterializeWhenNeeded(builderParameters, coraxQuery, ref streamingOptimization); } // We sort on known field types, we'll optimize based on the first one to get the rest - else if (sortMetadata is [{ FieldType: MatchCompareFieldType.Floating or MatchCompareFieldType.Integer or MatchCompareFieldType.Sequence } sortBy, ..]) + else if (sortMetadata is [{ FieldType: MatchCompareFieldType.Floating or MatchCompareFieldType.Integer or MatchCompareFieldType.Sequence, Field.FieldId: not CoraxConstants.IndexWriter.DynamicField } sortBy, ..]) { var maxTermToScan = builderParameters.Take switch { diff --git a/test/SlowTests/Issues/RavenDB-22703.cs b/test/SlowTests/Issues/RavenDB-22703.cs index e43a6dbd2407..94014d91281c 100644 --- a/test/SlowTests/Issues/RavenDB-22703.cs +++ b/test/SlowTests/Issues/RavenDB-22703.cs @@ -1,6 +1,8 @@ +using System.Collections.Generic; using System.Linq; using FastTests; using Raven.Client.Documents.Commands; +using Raven.Client.Documents.Indexes; using Raven.Client.Documents.Operations; using Sparrow.Json.Parsing; using Tests.Infrastructure; @@ -127,4 +129,56 @@ private class Foo public short BarShort { get; set; } public bool? BarBool { get; set; } } + + [RavenTheory(RavenTestCategory.Corax | RavenTestCategory.Indexes)] + [RavenData(SearchEngineMode = RavenSearchEngineMode.Corax, DatabaseMode = RavenDatabaseMode.All)] + public void TestOrderingOfDynamicFields(Options options) + { + using (var store = GetDocumentStore(options)) + { + using (var session = store.OpenSession()) + { + var p1 = new Product() { Attributes = new Dictionary() { { "Color", "Red" }, {"Size", 42 } } }; + var p2 = new Product() { Attributes = new Dictionary() { { "Color", "Blue" } } }; + var p3 = new Product() { Attributes = new Dictionary() { { "Size", 37 } } }; + + session.Store(p1); + session.Store(p2); + session.Store(p3); + + session.SaveChanges(); + + var index = new Products_ByAttributeKey(); + + index.Execute(store); + + Indexes.WaitForIndexing(store); + + var res = session + .Advanced + .DocumentQuery() + .OrderBy("Size") + .ToList(); + + Assert.Equal(3, res.Count); + } + } + } + + private class Product + { + public Dictionary Attributes { get; set; } + } + + private class Products_ByAttributeKey : AbstractIndexCreationTask + { + public Products_ByAttributeKey() + { + Map = products => from p in products + select new + { + _ = p.Attributes.Select(item => CreateField(item.Key, item.Value)) + }; + } + } }