Skip to content

Commit 5a1bc45

Browse files
committed
Add additional sort options to Top Hits Aggregation
Top Hits Aggregation fluent lambda syntax constrains sort options to field sorting, even though ITopHitsAggregation can accept any implementation of ISort. This commit deprecates the current fluent Sort implementation in favour of an implementation that allow multiple sort options besides field sorting, and assigns the resulting collection of sort options to IList<ISort>. Update TopHitsAggregation usage test to call the new Sort method. Fixes #2913
1 parent 717281a commit 5a1bc45

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

docs/aggregations/metric/top-hits/top-hits-aggregation-usage.asciidoc

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ s => s
2828
.Aggregations(aa => aa
2929
.TopHits("top_state_hits", th => th
3030
.Sort(srt => srt
31-
.Field(p => p.StartedOn)
32-
.Order(SortOrder.Descending)
31+
.Field(sf => sf
32+
.Field(p => p.StartedOn)
33+
.Order(SortOrder.Descending)
34+
)
35+
.Script(ss => ss
36+
.Type("number")
37+
.Script(sss => sss
38+
.Inline("Math.sin(34*(double)doc['numberOfCommits'].value)")
39+
.Lang("painless")
40+
)
41+
.Order(SortOrder.Descending)
42+
)
3343
)
3444
.Source(src => src
3545
.Includes(fs => fs
@@ -79,7 +89,13 @@ new SearchRequest<Project>
7989
{
8090
Sort = new List<ISort>
8191
{
82-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
92+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
93+
new ScriptSort
94+
{
95+
Type = "number",
96+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
97+
Order = SortOrder.Descending
98+
},
8399
},
84100
Source = new SourceFilter
85101
{
@@ -127,6 +143,16 @@ new SearchRequest<Project>
127143
"startedOn": {
128144
"order": "desc"
129145
}
146+
},
147+
{
148+
"_script": {
149+
"type": "number",
150+
"script": {
151+
"lang": "painless",
152+
"inline": "Math.sin(34*(double)doc['numberOfCommits'].value)"
153+
},
154+
"order": "desc"
155+
}
130156
}
131157
],
132158
"_source": {

src/Nest/Aggregations/Metric/TopHits/TopHitsAggregation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,17 @@ public class TopHitsAggregationDescriptor<T>
9595

9696
public TopHitsAggregationDescriptor<T> Size(int size) => Assign(a => a.Size = size);
9797

98+
[Obsolete("Use Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>>) that accepts multiple sort options")]
9899
public TopHitsAggregationDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector) => Assign(a =>
99100
{
100101
a.Sort = a.Sort ?? new List<ISort>();
101102
var sort = sortSelector?.Invoke(new SortFieldDescriptor<T>());
102103
if (sort != null) a.Sort.Add(sort);
103104
});
104105

106+
public TopHitsAggregationDescriptor<T> Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>> sortSelector) =>
107+
Assign(a => a.Sort = sortSelector?.Invoke(new SortDescriptor<T>())?.Value);
108+
105109
public TopHitsAggregationDescriptor<T> Source(bool enabled = true) =>
106110
Assign(a => a.Source = enabled);
107111

src/Tests/Aggregations/Metric/TopHits/TopHitsAggregationUsageTests.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Security;
45
using FluentAssertions;
56
using Nest;
67
using Tests.Framework;
@@ -39,6 +40,19 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
3940
{
4041
order = "desc"
4142
}
43+
},
44+
new
45+
{
46+
_script = new
47+
{
48+
type = "number",
49+
script = new
50+
{
51+
lang = "painless",
52+
inline = "Math.sin(34*(double)doc['numberOfCommits'].value)"
53+
},
54+
order = "desc"
55+
}
4256
}
4357
},
4458
_source = new
@@ -84,8 +98,18 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
8498
.Aggregations(aa => aa
8599
.TopHits("top_state_hits", th => th
86100
.Sort(srt => srt
87-
.Field(p => p.StartedOn)
88-
.Order(SortOrder.Descending)
101+
.Field(sf => sf
102+
.Field(p => p.StartedOn)
103+
.Order(SortOrder.Descending)
104+
)
105+
.Script(ss => ss
106+
.Type("number")
107+
.Script(sss => sss
108+
.Inline("Math.sin(34*(double)doc['numberOfCommits'].value)")
109+
.Lang("painless")
110+
)
111+
.Order(SortOrder.Descending)
112+
)
89113
)
90114
.Source(src => src
91115
.Includes(fs => fs
@@ -131,7 +155,13 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
131155
{
132156
Sort = new List<ISort>
133157
{
134-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
158+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
159+
new ScriptSort
160+
{
161+
Type = "number",
162+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
163+
Order = SortOrder.Descending
164+
},
135165
},
136166
Source = new SourceFilter
137167
{

0 commit comments

Comments
 (0)