Skip to content

Commit 241291b

Browse files
authored
Include missing in CompositeAggregationSource (#3602)
Closes #3540
1 parent 7b7c4eb commit 241291b

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

src/Nest/Aggregations/Bucket/Composite/CompositeAggregationSource.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public interface ICompositeAggregationSource
2626
[JsonProperty("missing_bucket")]
2727
bool? MissingBucket { get; set; }
2828

29+
30+
/// <summary>
31+
/// Allows an explicit value to be set when one is missing for the field.
32+
/// </summary>
33+
[Obsolete("Will be removed in Elasticsearch 7.x. Use MissingBucket")]
34+
[JsonProperty("missing")]
35+
object Missing { get; set; }
36+
2937
/// <summary>
3038
/// The name of the source
3139
/// </summary>
@@ -60,6 +68,10 @@ protected CompositeAggregationSourceBase(string name) =>
6068
/// <inheritdoc />
6169
public bool? MissingBucket { get; set; }
6270

71+
/// <inheritdoc />
72+
[Obsolete("Will be removed in Elasticsearch 7.x. Use MissingBucket")]
73+
public object Missing { get; set; }
74+
6375
/// <inheritdoc />
6476
public SortOrder? Order { get; set; }
6577

@@ -114,7 +126,8 @@ protected CompositeAggregationSourceDescriptorBase(string name, string sourceTyp
114126

115127
Field ICompositeAggregationSource.Field { get; set; }
116128
bool? ICompositeAggregationSource.MissingBucket { get; set; }
117-
129+
[Obsolete("Will be removed in Elasticsearch 7.x. Use MissingBucket")]
130+
object ICompositeAggregationSource.Missing { get; set; }
118131
string ICompositeAggregationSource.Name { get; set; }
119132
SortOrder? ICompositeAggregationSource.Order { get; set; }
120133
string ICompositeAggregationSource.SourceType => _sourceType;
@@ -130,6 +143,10 @@ protected CompositeAggregationSourceDescriptorBase(string name, string sourceTyp
130143

131144
/// <inheritdoc cref="ICompositeAggregationSource.MissingBucket" />
132145
public TDescriptor MissingBucket(bool? includeMissing = true) => Assign(a => a.MissingBucket = includeMissing);
146+
147+
/// <inheritdoc cref="ICompositeAggregationSource.Missing" />
148+
[Obsolete("Will be removed in Elasticsearch 7.x. Use MissingBucket")]
149+
public TDescriptor Missing(object missingObject) => Assign(a => a.Missing = missingObject);
133150
}
134151

135152
internal class CompositeAggregationSourceConverter : ReserializeJsonConverter<CompositeAggregationSourceBase, ICompositeAggregationSource>

src/Tests/Tests/Aggregations/Bucket/Composite/CompositeAggregationUsageTests.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,130 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
319319
}
320320
}
321321

322+
// hide
323+
#pragma warning disable 618
324+
[SkipVersion("<6.4.0", "Missing added to Composite Aggregation Elasticsearch 6.4.0+")]
325+
public class CompositeAggregationMissingUsageTests : ProjectsOnlyAggregationUsageTestBase
326+
{
327+
public CompositeAggregationMissingUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
328+
329+
protected override object AggregationJson => new
330+
{
331+
my_buckets = new
332+
{
333+
composite = new
334+
{
335+
sources = new object[]
336+
{
337+
new
338+
{
339+
branches = new
340+
{
341+
terms = new
342+
{
343+
field = "branches.keyword",
344+
order = "asc",
345+
missing = "missing_branch"
346+
}
347+
}
348+
},
349+
}
350+
},
351+
aggs = new
352+
{
353+
project_tags = new
354+
{
355+
nested = new { path = "tags" },
356+
aggs = new
357+
{
358+
tags = new { terms = new { field = "tags.name" } }
359+
}
360+
}
361+
}
362+
}
363+
};
364+
365+
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
366+
.Composite("my_buckets", date => date
367+
.Sources(s => s
368+
.Terms("branches", t => t
369+
.Field(f => f.Branches.Suffix("keyword"))
370+
371+
.Missing("missing_branch")
372+
.Order(SortOrder.Ascending)
373+
)
374+
)
375+
.Aggregations(childAggs => childAggs
376+
.Nested("project_tags", n => n
377+
.Path(p => p.Tags)
378+
.Aggregations(nestedAggs => nestedAggs
379+
.Terms("tags", avg => avg.Field(p => p.Tags.First().Name))
380+
)
381+
)
382+
)
383+
);
384+
385+
protected override AggregationDictionary InitializerAggs =>
386+
new CompositeAggregation("my_buckets")
387+
{
388+
Sources = new List<ICompositeAggregationSource>
389+
{
390+
new TermsCompositeAggregationSource("branches")
391+
{
392+
Field = Field<Project>(f => f.Branches.Suffix("keyword")),
393+
Missing = "missing_branch",
394+
Order = SortOrder.Ascending
395+
}
396+
},
397+
Aggregations = new NestedAggregation("project_tags")
398+
{
399+
Path = Field<Project>(p => p.Tags),
400+
Aggregations = new TermsAggregation("tags")
401+
{
402+
Field = Field<Project>(p => p.Tags.First().Name)
403+
}
404+
}
405+
};
406+
407+
protected override void ExpectResponse(ISearchResponse<Project> response)
408+
{
409+
response.ShouldBeValid();
410+
411+
var composite = response.Aggregations.Composite("my_buckets");
412+
composite.Should().NotBeNull();
413+
composite.Buckets.Should().NotBeNullOrEmpty();
414+
composite.AfterKey.Should().NotBeNull();
415+
416+
if (TestConfiguration.Instance.InRange(">=6.3.0"))
417+
composite.AfterKey.Should().HaveCount(1).And.ContainKeys("branches");
418+
419+
var i = 0;
420+
var seenMissingBranches = false;
421+
foreach (var item in composite.Buckets)
422+
{
423+
var key = item.Key;
424+
key.Should().NotBeNull();
425+
426+
key.TryGetValue("branches", out string branches).Should().BeTrue("expected to find 'branches' in composite bucket");
427+
branches.Should().NotBeNullOrEmpty();
428+
if (branches == "missing_branch")
429+
{
430+
seenMissingBranches = true;
431+
}
432+
433+
var nested = item.Nested("project_tags");
434+
nested.Should().NotBeNull();
435+
436+
var nestedTerms = nested.Terms("tags");
437+
nestedTerms.Buckets.Count.Should().BeGreaterThan(0);
438+
i++;
439+
}
440+
441+
seenMissingBranches.Should().BeTrue();
442+
}
443+
}
444+
#pragma warning restore 618
445+
322446
//hide
323447
[SkipVersion("<6.3.0", "Date histogram source only supports format starting from Elasticsearch 6.3.0+")]
324448
public class DateFormatCompositeAggregationUsageTests : ProjectsOnlyAggregationUsageTestBase

0 commit comments

Comments
 (0)