Skip to content

Commit 69819fe

Browse files
committed
Fix parsing of the top hits aggregation response
The top hits aggregation response wasn't being read to completion causing other aggregation results on the same level to be skipped. Closes #1169
1 parent 856a4d1 commit 69819fe

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/Nest/Resolvers/Converters/Aggregations/AggregationConverter.cs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private IAggregation GetHitsAggregation(JsonReader reader, JsonSerializer serial
7878
var total = o["total"].ToObject<long>();
7979
var maxScore = o["max_score"].ToObject<double?>();
8080
var hits = o["hits"].Children().OfType<JObject>().Select(s=>s);
81+
reader.Read();
8182
return new TopHitsMetric(hits) { Total = total, MaxScore = maxScore };
8283
}
8384

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<Compile Include="Mapping\MappingVisitorTests.cs" />
184184
<Compile Include="Mapping\GetMultipleMappingTests.cs" />
185185
<Compile Include="Reproduce\Reproduce1079Tests.cs" />
186+
<Compile Include="Reproduce\Reproduce1169Tests.cs" />
186187
<Compile Include="Reproduce\Reproduce769Tests.cs" />
187188
<Compile Include="Reproduce\Reproduce945Tests.cs" />
188189
<Compile Include="Reproduce\Reproduce953Tests.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using FluentAssertions;
2+
using Nest.Tests.MockData.Domain;
3+
using NUnit.Framework;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Nest.Tests.Integration.Reproduce
11+
{
12+
[TestFixture]
13+
public class Reproduce1169Tests : IntegrationTests
14+
{
15+
[Test]
16+
public void TopHitsReturnedFirst()
17+
{
18+
var response = this.Client.Search<ElasticsearchProject>(s => s
19+
.Size(0)
20+
.Aggregations(a => a
21+
.TopHits("my-top-hits", th => th
22+
.Size(1)
23+
)
24+
.Terms("a", t => t
25+
.Field(p => p.Name)
26+
)
27+
)
28+
);
29+
30+
response.IsValid.Should().BeTrue();
31+
32+
var topHits = response.Aggs.TopHitsMetric("my-top-hits");
33+
topHits.Should().NotBeNull();
34+
topHits.Hits<ElasticsearchProject>().Count().Should().BeGreaterThan(0);
35+
36+
var terms = response.Aggs.Terms("a");
37+
terms.Should().NotBeNull();
38+
terms.Items.Count().Should().BeGreaterThan(0);
39+
}
40+
41+
[Test]
42+
public void TermsReturnedFirst()
43+
{
44+
var response = this.Client.Search<ElasticsearchProject>(s => s
45+
.Size(0)
46+
.Aggregations(a => a
47+
.TopHits("my-top-hits", th => th
48+
.Size(1)
49+
)
50+
.Terms("b", t => t
51+
.Field(p => p.Name)
52+
)
53+
)
54+
);
55+
56+
response.IsValid.Should().BeTrue();
57+
58+
var topHits = response.Aggs.TopHitsMetric("my-top-hits");
59+
topHits.Should().NotBeNull();
60+
topHits.Hits<ElasticsearchProject>().Count().Should().BeGreaterThan(0);
61+
62+
var terms = response.Aggs.Terms("b");
63+
terms.Should().NotBeNull();
64+
terms.Items.Count().Should().BeGreaterThan(0);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)