Skip to content

Commit 780f52f

Browse files
authored
Deserialize anonymous bucket with only doc_count (#4214)
This commit deserializes an anonymous bucket that contains only a doc_count. Existing tests cover the case where the bucket has sub-aggregations, but did not handle the case where there are none. Fixes #4197
1 parent 33476bd commit 780f52f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,12 @@ private IBucket GetSignificantTermsBucket(ref JsonReader reader, IJsonFormatterR
877877
private IBucket GetFiltersBucket(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
878878
{
879879
var docCount = reader.ReadNullableLong().GetValueOrDefault(0);
880+
if (reader.GetCurrentJsonToken() == JsonToken.EndObject)
881+
return new FiltersBucketItem(EmptyReadOnly<string, IAggregate>.Dictionary)
882+
{
883+
DocCount = docCount
884+
};
885+
880886
reader.ReadNext(); // ,
881887
var propertyName = reader.ReadPropertyName();
882888
var subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Elastic.Xunit.XunitPlumbing;
3+
using Elasticsearch.Net;
4+
using FluentAssertions;
5+
using Nest;
6+
using Tests.Core.ManagedElasticsearch.Clusters;
7+
8+
namespace Tests.Reproduce
9+
{
10+
public class GithubIssue4197 : IClusterFixture<WritableCluster>
11+
{
12+
private readonly IElasticClient _client;
13+
14+
public GithubIssue4197(WritableCluster cluster) => _client = cluster.Client;
15+
16+
[I]
17+
public void CanDeserializeAnonymousFiltersAggregation()
18+
{
19+
const string index = "github-issue-4197";
20+
21+
_client.Indices.Create(index);
22+
23+
_client.Index(new Doc { ModificationDate = DateTime.Parse("2019-10-09T10:43:07.8633456+02:00") },
24+
i => i.Index(index).Refresh(Refresh.WaitFor));
25+
26+
var searchResponse = _client.Search<Doc>(s => s
27+
.Index(index)
28+
.Aggregations(a => a
29+
.Filters("Modification date", f => f
30+
.AnonymousFilters(q => q
31+
.DateRange(dr => dr
32+
.Field(d => d.ModificationDate)
33+
.GreaterThan(DateMath.Now.Subtract(TimeSpan.FromDays(120)))
34+
)
35+
)
36+
)
37+
)
38+
);
39+
40+
var filtersAggregate = searchResponse.Aggregations.Filters("Modification date");
41+
filtersAggregate.AnonymousBuckets().Count.Should().Be(1);
42+
filtersAggregate.AnonymousBuckets()[0].DocCount.Should().Be(1);
43+
}
44+
45+
private class Doc
46+
{
47+
public DateTime ModificationDate { get; set; }
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)