Skip to content

Commit

Permalink
added support for decimal column sorting with SearchAfter & SearchBef…
Browse files Browse the repository at this point in the history
…ore (#104)
  • Loading branch information
randylsu authored May 20, 2024
1 parent c119f42 commit 7091542
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,24 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
{
return reader.TokenType switch
{
JsonTokenType.Number => reader.GetInt64(),
JsonTokenType.Number => GetNumber(reader),
JsonTokenType.String => reader.GetString(),
JsonTokenType.True => reader.GetBoolean(),
JsonTokenType.False => reader.GetBoolean(),
_ => null
};
}

private object GetNumber(Utf8JsonReader reader)
{
if (reader.TryGetInt64(out var l))
return l;
else if (reader.TryGetDecimal(out var d))
return d;
else
throw new InvalidOperationException("Value is not a number");
}

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,10 @@ public async Task ShouldNotIncludeWhenDeletedAsync()
Assert.Single(employees.Documents);
}

[Fact]
public async Task CanSearchAfterAndBeforeWithMultipleSorts()
[Theory]
[InlineData("age")]
[InlineData("decimalAge")]
public async Task CanSearchAfterAndBeforeWithMultipleSorts(string secondarySort)
{
await _employeeRepository.AddAsync(EmployeeGenerator.GenerateEmployees(count: 100), o => o.ImmediateConsistency());
int pageSize = 10;
Expand All @@ -1214,7 +1216,7 @@ public async Task CanSearchAfterAndBeforeWithMultipleSorts()
do
{
page++;
var employees = await _employeeRepository.FindAsync(q => q.Sort(e => e.Name).Sort(e => e.CompanyName).SortDescending(e => e.Age), o => o.SearchAfterToken(searchAfterToken).PageLimit(pageSize).QueryLogLevel(LogLevel.Information));
var employees = await _employeeRepository.FindAsync(q => q.Sort(e => e.Name).Sort(e => e.CompanyName).SortDescending(secondarySort), o => o.SearchAfterToken(searchAfterToken).PageLimit(pageSize).QueryLogLevel(LogLevel.Information));
searchBeforeToken = employees.GetSearchBeforeToken();
searchAfterToken = employees.GetSearchAfterToken();
if (page == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public override TypeMappingDescriptor<Employee> ConfigureIndexMapping(TypeMappin
.Text(f => f.Name(e => e.Name).AddKeywordAndSortFields().CopyTo(c => c.Field("_all")))
.Scalar(f => f.Age, f => f.Name(e => e.Age))
.FieldAlias(a => a.Name("aliasedage").Path(f => f.Age))
.Scalar(f => f.DecimalAge, f => f.Name(e => e.DecimalAge))
.Scalar(f => f.NextReview, f => f.Name(e => e.NextReview))
.FieldAlias(a => a.Name("next").Path(f => f.NextReview))
.GeoPoint(f => f.Name(e => e.Location))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Employee : IIdentity, IHaveDates, IVersioned, ISupportSoftDeletes
public string UnmappedEmailAddress => EmailAddress;
public int Age { get; set; }
public int UnmappedAge => Age;
public double DecimalAge => Age + .5;
public string Location { get; set; }
public int YearsEmployed { get; set; }
public EmploymentType EmploymentType { get; set; } = EmploymentType.FullTime;
Expand Down

0 comments on commit 7091542

Please sign in to comment.