Skip to content

Commit 826ca5e

Browse files
authored
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 * Remove superfluous using directive
1 parent 5632144 commit 826ca5e

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
.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: 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+
inline = "Math.sin(34*(double)doc['numberOfCommits'].value)"
52+
},
53+
order = "desc"
54+
}
4255
}
4356
},
4457
_source = new
@@ -84,8 +97,18 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
8497
.Aggregations(aa => aa
8598
.TopHits("top_state_hits", th => th
8699
.Sort(srt => srt
87-
.Field(p => p.StartedOn)
88-
.Order(SortOrder.Descending)
100+
.Field(sf => sf
101+
.Field(p => p.StartedOn)
102+
.Order(SortOrder.Descending)
103+
)
104+
.Script(ss => ss
105+
.Type("number")
106+
.Script(sss => sss
107+
.Inline("Math.sin(34*(double)doc['numberOfCommits'].value)")
108+
.Lang("painless")
109+
)
110+
.Order(SortOrder.Descending)
111+
)
89112
)
90113
.Source(src => src
91114
.Includes(fs => fs
@@ -131,7 +154,13 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
131154
{
132155
Sort = new List<ISort>
133156
{
134-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
157+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
158+
new ScriptSort
159+
{
160+
Type = "number",
161+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
162+
Order = SortOrder.Descending
163+
},
135164
},
136165
Source = new SourceFilter
137166
{

0 commit comments

Comments
 (0)