Skip to content

Commit 65bc7d7

Browse files
committed
Add additional sort options to Top Hits Aggregation (#2968)
* 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 0ed63dc commit 65bc7d7

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
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
@@ -26,8 +26,18 @@ s => s
2626
.Aggregations(aa => aa
2727
.TopHits("top_state_hits", th => th
2828
.Sort(srt => srt
29-
.Field(p => p.StartedOn)
30-
.Order(SortOrder.Descending)
29+
.Field(sf => sf
30+
.Field(p => p.StartedOn)
31+
.Order(SortOrder.Descending)
32+
)
33+
.Script(ss => ss
34+
.Type("number")
35+
.Script(sss => sss
36+
.Source("Math.sin(34*(double)doc['numberOfCommits'].value)")
37+
.Lang("painless")
38+
)
39+
.Order(SortOrder.Descending)
40+
)
3141
)
3242
.Source(src => src
3343
.Includes(fs => fs
@@ -73,7 +83,13 @@ new SearchRequest<Project>
7383
{
7484
Sort = new List<ISort>
7585
{
76-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
86+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
87+
new ScriptSort
88+
{
89+
Type = "number",
90+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
91+
Order = SortOrder.Descending
92+
},
7793
},
7894
Source = new SourceFilter
7995
{
@@ -120,6 +136,16 @@ new SearchRequest<Project>
120136
"startedOn": {
121137
"order": "desc"
122138
}
139+
},
140+
{
141+
"_script": {
142+
"type": "number",
143+
"script": {
144+
"lang": "painless",
145+
"source": "Math.sin(34*(double)doc['numberOfCommits'].value)"
146+
},
147+
"order": "desc"
148+
}
123149
}
124150
],
125151
"_source": {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ public class TopHitsAggregationDescriptor<T>
8989

9090
public TopHitsAggregationDescriptor<T> Size(int size) => Assign(a => a.Size = size);
9191

92-
public TopHitsAggregationDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector) => Assign(a =>
93-
{
94-
a.Sort = a.Sort ?? new List<ISort>();
95-
var sort = sortSelector?.Invoke(new SortFieldDescriptor<T>());
96-
if (sort != null) a.Sort.Add(sort);
97-
});
92+
public TopHitsAggregationDescriptor<T> Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>> sortSelector) =>
93+
Assign(a => a.Sort = sortSelector?.Invoke(new SortDescriptor<T>())?.Value);
9894

9995
public TopHitsAggregationDescriptor<T> Source(bool enabled = true) =>
10096
Assign(a => a.Source = enabled);

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
3939
{
4040
order = "desc"
4141
}
42+
},
43+
new
44+
{
45+
_script = new
46+
{
47+
type = "number",
48+
script = new
49+
{
50+
lang = "painless",
51+
source = "Math.sin(34*(double)doc['numberOfCommits'].value)"
52+
},
53+
order = "desc"
54+
}
4255
}
4356
},
4457
_source = new
@@ -82,8 +95,18 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
8295
.Aggregations(aa => aa
8396
.TopHits("top_state_hits", th => th
8497
.Sort(srt => srt
85-
.Field(p => p.StartedOn)
86-
.Order(SortOrder.Descending)
98+
.Field(sf => sf
99+
.Field(p => p.StartedOn)
100+
.Order(SortOrder.Descending)
101+
)
102+
.Script(ss => ss
103+
.Type("number")
104+
.Script(sss => sss
105+
.Source("Math.sin(34*(double)doc['numberOfCommits'].value)")
106+
.Lang("painless")
107+
)
108+
.Order(SortOrder.Descending)
109+
)
87110
)
88111
.Source(src => src
89112
.Includes(fs => fs
@@ -125,7 +148,13 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
125148
{
126149
Sort = new List<ISort>
127150
{
128-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
151+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
152+
new ScriptSort
153+
{
154+
Type = "number",
155+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
156+
Order = SortOrder.Descending
157+
},
129158
},
130159
Source = new SourceFilter
131160
{

0 commit comments

Comments
 (0)