Skip to content

Commit

Permalink
Add numeric_type sorting option. (#3998)
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 98a9427)
  • Loading branch information
codebrain authored and russcam committed Sep 3, 2019
1 parent 0af7b8d commit 8b0deb1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Nest/Search/Search/Sort/NumericType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Runtime.Serialization;
using Elasticsearch.Net;

namespace Nest
{
/// <summary>
/// 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.
/// </summary>
[StringEnum]
public enum NumericType
{
[EnumMember(Value = "long")]
Long,

[EnumMember(Value = "double")]
Double,

[EnumMember(Value = "date")]
Date,

[EnumMember(Value = "date_nanos")]
DateNanos
}
}
13 changes: 13 additions & 0 deletions src/Nest/Search/Search/Sort/SortBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public interface ISort
[DataMember(Name ="mode")]
SortMode? Mode { get; set; }

/// <summary>
/// Set a single resolution for the sort
/// </summary>
[DataMember(Name ="numeric_type")]
NumericType? NumericType { get; set; }

/// <summary>
/// Specifies the path and filter to apply when sorting on a nested field
/// </summary>
Expand Down Expand Up @@ -49,6 +55,9 @@ public abstract class SortBase : ISort
/// <inheritdoc />
public SortMode? Mode { get; set; }

/// <inheritdoc />
public NumericType? NumericType { get; set; }

/// <inheritdoc />
public INestedSort Nested { get; set; }

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

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

/// <inheritdoc cref="ISort.NumericType" />
public virtual TDescriptor NumericType(NumericType? numericType) => Assign(numericType, (a, v) => a.NumericType = v);

/// <inheritdoc cref="ISort.Mode" />
public virtual TDescriptor Mode(SortMode? mode) => Assign(mode, (a, v) => a.Mode = v);

Expand Down
34 changes: 34 additions & 0 deletions src/Tests/Tests/Search/Request/SortUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,38 @@ public GeoDistanceIgnoreUnmappedUsageTests(ReadOnlyCluster cluster, EndpointUsag
}
};
}

//hide
[SkipVersion("<7.2.0", "numeric_type added in 7.2.0")]
public class NumericTypeUsageTests : SearchUsageTestBase
{
public NumericTypeUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override object ExpectJson =>
new
{
sort = new object[]
{
new { startedOn = new { numeric_type = "date", order = "asc" } }
}
};

protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
.Sort(ss => ss
.Field(g => g
.Field(p => p.StartedOn)
.NumericType(NumericType.Date)
.Ascending()
)
);

protected override SearchRequest<Project> Initializer =>
new SearchRequest<Project>
{
Sort = new List<ISort>
{
new FieldSort { Field = "startedOn", NumericType = NumericType.Date, Order = SortOrder.Ascending },
}
};
}
}

0 comments on commit 8b0deb1

Please sign in to comment.