Skip to content

Commit eba9fd9

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 efbb717 commit eba9fd9

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-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
.Include(fs => fs
@@ -77,7 +87,13 @@ new SearchRequest<Project>
7787
{
7888
Sort = new List<ISort>
7989
{
80-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
90+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
91+
new ScriptSort
92+
{
93+
Type = "number",
94+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
95+
Order = SortOrder.Descending
96+
},
8197
},
8298
Source = new SourceFilter
8399
{
@@ -122,6 +138,16 @@ new SearchRequest<Project>
122138
"startedOn": {
123139
"order": "desc"
124140
}
141+
},
142+
{
143+
"_script": {
144+
"type": "number",
145+
"script": {
146+
"lang": "painless",
147+
"inline": "Math.sin(34*(double)doc['numberOfCommits'].value)"
148+
},
149+
"order": "desc"
150+
}
125151
}
126152
],
127153
"_source": {

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

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

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

92+
[Obsolete("Use Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>>) that accepts multiple sort options")]
9293
public TopHitsAggregationDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector) => Assign(a =>
9394
{
9495
a.Sort = a.Sort ?? new List<ISort>();
9596
var sort = sortSelector?.Invoke(new SortFieldDescriptor<T>());
9697
if (sort != null) a.Sort.Add(sort);
9798
});
9899

100+
public TopHitsAggregationDescriptor<T> Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>> sortSelector) =>
101+
Assign(a => a.Sort = sortSelector?.Invoke(new SortDescriptor<T>())?.Value);
102+
99103
public TopHitsAggregationDescriptor<T> Source(bool include = true) =>
100104
Assign(a => a.Source = new SourceFilter { Disable = !include });
101105

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
4040
{
4141
order = "desc"
4242
}
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+
}
4356
}
4457
},
4558
_source = new
@@ -83,8 +96,18 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
8396
.Aggregations(aa => aa
8497
.TopHits("top_state_hits", th => th
8598
.Sort(srt => srt
86-
.Field(p => p.StartedOn)
87-
.Order(SortOrder.Descending)
99+
.Field(sf => sf
100+
.Field(p => p.StartedOn)
101+
.Order(SortOrder.Descending)
102+
)
103+
.Script(ss => ss
104+
.Type("number")
105+
.Script(sss => sss
106+
.Inline("Math.sin(34*(double)doc['numberOfCommits'].value)")
107+
.Lang("painless")
108+
)
109+
.Order(SortOrder.Descending)
110+
)
88111
)
89112
.Source(src => src
90113
.Include(fs => fs
@@ -128,7 +151,13 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
128151
{
129152
Sort = new List<ISort>
130153
{
131-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
154+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
155+
new ScriptSort
156+
{
157+
Type = "number",
158+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
159+
Order = SortOrder.Descending
160+
},
132161
},
133162
Source = new SourceFilter
134163
{

0 commit comments

Comments
 (0)