Skip to content

Commit 97e81b5

Browse files
authored
Add numeric_type sorting option. (#3998)
For numeric fields it is also possible to cast the values from one type to another using this option. This can be useful for cross-index search if the sort field is mapped differently on some indices.
1 parent dbfc407 commit 97e81b5

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Runtime.Serialization;
2+
using Elasticsearch.Net;
3+
4+
namespace Nest
5+
{
6+
/// <summary>
7+
/// For numeric fields it is also possible to cast the values from one type to another using this option. This can be useful for cross-index
8+
/// search if the sort field is mapped differently on some indices.
9+
/// </summary>
10+
[StringEnum]
11+
public enum NumericType
12+
{
13+
[EnumMember(Value = "long")]
14+
Long,
15+
16+
[EnumMember(Value = "double")]
17+
Double,
18+
19+
[EnumMember(Value = "date")]
20+
Date,
21+
22+
[EnumMember(Value = "date_nanos")]
23+
DateNanos
24+
}
25+
}

src/Nest/Search/Search/Sort/SortBase.cs

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public interface ISort
2222
[DataMember(Name ="mode")]
2323
SortMode? Mode { get; set; }
2424

25+
/// <summary>
26+
/// Set a single resolution for the sort
27+
/// </summary>
28+
[DataMember(Name ="numeric_type")]
29+
NumericType? NumericType { get; set; }
30+
2531
/// <summary>
2632
/// Specifies the path and filter to apply when sorting on a nested field
2733
/// </summary>
@@ -49,6 +55,9 @@ public abstract class SortBase : ISort
4955
/// <inheritdoc />
5056
public SortMode? Mode { get; set; }
5157

58+
/// <inheritdoc />
59+
public NumericType? NumericType { get; set; }
60+
5261
/// <inheritdoc />
5362
public INestedSort Nested { get; set; }
5463

@@ -76,6 +85,7 @@ public abstract class SortDescriptorBase<TDescriptor, TInterface, T> : Descripto
7685

7786
object ISort.Missing { get; set; }
7887
SortMode? ISort.Mode { get; set; }
88+
NumericType? ISort.NumericType { get; set; }
7989
INestedSort ISort.Nested { get; set; }
8090
SortOrder? ISort.Order { get; set; }
8191
Field ISort.SortKey => SortKey;
@@ -93,6 +103,9 @@ public abstract class SortDescriptorBase<TDescriptor, TInterface, T> : Descripto
93103
/// <inheritdoc cref="ISort.Order" />
94104
public virtual TDescriptor Order(SortOrder? order) => Assign(order, (a, v) => a.Order = v);
95105

106+
/// <inheritdoc cref="ISort.NumericType" />
107+
public virtual TDescriptor NumericType(NumericType? numericType) => Assign(numericType, (a, v) => a.NumericType = v);
108+
96109
/// <inheritdoc cref="ISort.Mode" />
97110
public virtual TDescriptor Mode(SortMode? mode) => Assign(mode, (a, v) => a.Mode = v);
98111

src/Tests/Tests/Search/Request/SortUsageTests.cs

+34
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,38 @@ public GeoDistanceIgnoreUnmappedUsageTests(ReadOnlyCluster cluster, EndpointUsag
331331
}
332332
};
333333
}
334+
335+
//hide
336+
[SkipVersion("<7.2.0", "numeric_type added in 7.2.0")]
337+
public class NumericTypeUsageTests : SearchUsageTestBase
338+
{
339+
public NumericTypeUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
340+
341+
protected override object ExpectJson =>
342+
new
343+
{
344+
sort = new object[]
345+
{
346+
new { startedOn = new { numeric_type = "date", order = "asc" } }
347+
}
348+
};
349+
350+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
351+
.Sort(ss => ss
352+
.Field(g => g
353+
.Field(p => p.StartedOn)
354+
.NumericType(NumericType.Date)
355+
.Ascending()
356+
)
357+
);
358+
359+
protected override SearchRequest<Project> Initializer =>
360+
new SearchRequest<Project>
361+
{
362+
Sort = new List<ISort>
363+
{
364+
new FieldSort { Field = "startedOn", NumericType = NumericType.Date, Order = SortOrder.Ascending },
365+
}
366+
};
367+
}
334368
}

0 commit comments

Comments
 (0)