Skip to content

Add numeric_type sorting option. #3998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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..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 },
}
};
}
}