Skip to content

Commit

Permalink
RavenDB-22703 Fix ordering of dynamic fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Lwiel committed Aug 23, 2024
1 parent 31f724b commit aa255b4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
54 changes: 54 additions & 0 deletions test/SlowTests/Issues/RavenDB-22703.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, object>() { { "Color", "Red" }, {"Size", 42 } } };
var p2 = new Product() { Attributes = new Dictionary<string, object>() { { "Color", "Blue" } } };
var p3 = new Product() { Attributes = new Dictionary<string, object>() { { "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<Product, Products_ByAttributeKey>()
.OrderBy("Size")
.ToList();

Assert.Equal(3, res.Count);
}
}
}

private class Product
{
public Dictionary<string, object> Attributes { get; set; }
}

private class Products_ByAttributeKey : AbstractIndexCreationTask<Product>
{
public Products_ByAttributeKey()
{
Map = products => from p in products
select new
{
_ = p.Attributes.Select(item => CreateField(item.Key, item.Value))
};
}
}
}

0 comments on commit aa255b4

Please sign in to comment.