Skip to content

Commit bbdccfa

Browse files
committed
Merge branch 'feature/aggregations-top-hits' of github.com:elasticsearch/elasticsearch-net into feature/aggregations-top-hits
Conflicts: src/Nest/DSL/Aggregations/AggregationDescriptor.cs src/Nest/Nest.csproj src/Tests/Nest.Tests.Integration/Aggregations/MetricAggregationTests.cs
2 parents aab5ad6 + 9fb1620 commit bbdccfa

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

src/Nest/DSL/Aggregations/AggregationDescriptor.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public interface IAggregationContainer
8484
[JsonProperty("percentile_ranks")]
8585
IPercentileRanksAggregaor PercentileRanks { get; set; }
8686

87+
[JsonProperty("top_hits")]
88+
ITopHitsAggregator TopHits { get; set; }
89+
8790
[JsonProperty("aggs")]
8891
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
8992
IDictionary<string, IAggregationContainer> Aggregations { get; set; }
@@ -111,14 +114,14 @@ public class AggregationContainer : IAggregationContainer
111114
private ISignificantTermsAggregator _significantTerms;
112115
private IPercentileRanksAggregaor _percentileRanks;
113116

117+
private ITopHitsAggregator _topHits;
114118
public IAverageAggregator Average { get; set; }
115119
public IValueCountAggregator ValueCount { get; set; }
116120
public IMaxAggregator Max { get; set; }
117121
public IMinAggregator Min { get; set; }
118122
public IStatsAggregator Stats { get; set; }
119123
public ISumAggregator Sum { get; set; }
120124
public IExtendedStatsAggregator ExtendedStats { get; set; }
121-
122125
public IDateHistogramAggregator DateHistogram
123126
{
124127
get { return _dateHistogram; }
@@ -227,6 +230,12 @@ public IPercentileRanksAggregaor PercentileRanks
227230
set { _percentileRanks = value; }
228231
}
229232

233+
public ITopHitsAggregator TopHits
234+
{
235+
get { return _topHits; }
236+
set { _topHits = value; }
237+
}
238+
230239
private void LiftAggregations(IBucketAggregator bucket)
231240
{
232241
if (bucket == null) return;
@@ -290,7 +299,9 @@ public class AggregationDescriptor<T> : IAggregationContainer
290299
IPercentileRanksAggregaor IAggregationContainer.PercentileRanks { get;set; }
291300

292301
ITermsAggregator IAggregationContainer.Terms { get; set; }
293-
302+
303+
ITopHitsAggregator IAggregationContainer.TopHits { get; set; }
304+
294305
public AggregationDescriptor<T> Average(string name, Func<AverageAggregationDescriptor<T>, AverageAggregationDescriptor<T>> selector)
295306
{
296307
return _SetInnerAggregation(name, selector, (a, d) => a.Average = d);
@@ -429,6 +440,12 @@ public AggregationDescriptor<T> ValueCount(string name,
429440
return _SetInnerAggregation(name, selector, (a, d) => a.ValueCount = d);
430441
}
431442

443+
public AggregationDescriptor<T> TopHits(string name,
444+
Func<TopHitsAggregationDescriptor<T>, TopHitsAggregationDescriptor<T>> selector)
445+
{
446+
return _SetInnerAggregation(name, selector, (a, d) => a.TopHits = d);
447+
}
448+
432449
private AggregationDescriptor<T> _SetInnerAggregation<TAggregation>(
433450
string key,
434451
Func<TAggregation, TAggregation> selector
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
11+
[JsonConverter(typeof(ReadAsTypeConverter<TopHitsAggregator>))]
12+
public interface ITopHitsAggregator : IMetricAggregator
13+
{
14+
[JsonProperty("from")]
15+
int? From { get; set; }
16+
17+
[JsonProperty("size")]
18+
int? Size { get; set; }
19+
20+
[JsonProperty("sort")]
21+
[JsonConverter(typeof(SortCollectionConverter))]
22+
IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
23+
24+
[JsonProperty("_source")]
25+
ISourceFilter Source { get; set; }
26+
}
27+
28+
public class TopHitsAggregator : MetricAggregator, ITopHitsAggregator
29+
{
30+
public int? From { get; set; }
31+
public int? Size { get; set; }
32+
public IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
33+
public ISourceFilter Source { get; set; }
34+
}
35+
36+
public class TopHitsAggregationDescriptor<T>
37+
: MetricAggregationBaseDescriptor<TopHitsAggregationDescriptor<T>, T>, ITopHitsAggregator
38+
where T : class
39+
{
40+
ITopHitsAggregator Self { get { return this; } }
41+
42+
int? ITopHitsAggregator.From { get; set; }
43+
44+
int? ITopHitsAggregator.Size { get; set; }
45+
46+
IList<KeyValuePair<PropertyPathMarker, ISort>> ITopHitsAggregator.Sort { get; set; }
47+
48+
ISourceFilter ITopHitsAggregator.Source { get; set; }
49+
50+
public TopHitsAggregationDescriptor<T> From(int from)
51+
{
52+
this.Self.From = from;
53+
return this;
54+
}
55+
56+
public TopHitsAggregationDescriptor<T> Size(int size)
57+
{
58+
this.Self.Size = size;
59+
return this;
60+
}
61+
62+
public TopHitsAggregationDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector)
63+
{
64+
sortSelector.ThrowIfNull("sortSelector");
65+
66+
if (Self.Sort == null)
67+
Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
68+
69+
var descriptor = sortSelector(new SortFieldDescriptor<T>());
70+
this.Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(descriptor.Field, descriptor));
71+
72+
return this;
73+
}
74+
75+
public TopHitsAggregationDescriptor<T> Source(bool include = true)
76+
{
77+
if (!include)
78+
this.Self.Source = new SourceFilter { Exclude = new PropertyPathMarker[] { "*" } };
79+
else
80+
this.Self.Source = null;
81+
82+
return this;
83+
}
84+
85+
public TopHitsAggregationDescriptor<T> Source(Func<SearchSourceDescriptor<T>, SearchSourceDescriptor<T>> sourceSelector)
86+
{
87+
this.Self.Source = sourceSelector(new SearchSourceDescriptor<T>());
88+
return this;
89+
}
90+
}
91+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
<Compile Include="Domain\Stats\PluginStats.cs" />
212212
<Compile Include="Domain\Stats\SegmentsStats.cs" />
213213
<Compile Include="DSL\Aggregations\PercentileRanksAggregationDescriptor.cs" />
214+
<Compile Include="DSL\Aggregations\TopHitsAggregationDescriptor.cs" />
214215
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
215216
<Compile Include="DSL\Aggregations\ReverseNestedAggregationDescriptor.cs" />
216217
<Compile Include="DSL\CatIndicesDescriptor.cs" />

src/Tests/Nest.Tests.Integration/Aggregations/MetricAggregationTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,31 @@ public void GeoBounds()
162162
geoBoundsMetric.Bounds.BottomRight.Lat.Should().NotBe(0);
163163
geoBoundsMetric.Bounds.BottomRight.Lon.Should().NotBe(0);
164164
}
165+
166+
[Test]
167+
public void TopHits()
168+
{
169+
var results = this.Client.Search<ElasticsearchProject>(s => s
170+
.Size(0)
171+
.Aggregations(a => a
172+
.Terms("top-countries", t => t
173+
.Field(p => p.Country)
174+
.Size(3)
175+
.Aggregations(aa => aa
176+
.TopHits("top-country-hits", th => th
177+
.Sort(sort => sort
178+
.OnField(p => p.StartedOn)
179+
.Order(SortOrder.Descending)
180+
)
181+
.Source(src => src
182+
.Include(p => p.Name)
183+
)
184+
.Size(1)
185+
)
186+
)
187+
)
188+
)
189+
);
190+
}
165191
}
166192
}

0 commit comments

Comments
 (0)