|
1 | 1 | using System; |
2 | 2 | using System.Linq; |
| 3 | +using Elastic.Xunit.XunitPlumbing; |
3 | 4 | using FluentAssertions; |
4 | 5 | using Nest; |
5 | 6 | using Tests.Core.Extensions; |
@@ -27,6 +28,104 @@ public class AutoDateHistogramAggregationUsageTests : ProjectsOnlyAggregationUsa |
27 | 28 | { |
28 | 29 | public AutoDateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } |
29 | 30 |
|
| 31 | + protected override object AggregationJson => new |
| 32 | + { |
| 33 | + projects_started_per_month = new |
| 34 | + { |
| 35 | + auto_date_histogram = new |
| 36 | + { |
| 37 | + field = "startedOn", |
| 38 | + buckets = 10, |
| 39 | + format = "yyyy-MM-dd'T'HH:mm:ss||date_optional_time", //<1> Note the inclusion of `date_optional_time` to `format` |
| 40 | + missing = FixedDate |
| 41 | + }, |
| 42 | + aggs = new |
| 43 | + { |
| 44 | + project_tags = new |
| 45 | + { |
| 46 | + nested = new |
| 47 | + { |
| 48 | + path = "tags" |
| 49 | + }, |
| 50 | + aggs = new |
| 51 | + { |
| 52 | + tags = new |
| 53 | + { |
| 54 | + terms = new { field = "tags.name" } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a |
| 63 | + .AutoDateHistogram("projects_started_per_month", date => date |
| 64 | + .Field(p => p.StartedOn) |
| 65 | + .Buckets(10) |
| 66 | + .Format("yyyy-MM-dd'T'HH:mm:ss") |
| 67 | + .Missing(FixedDate) |
| 68 | + .Aggregations(childAggs => childAggs |
| 69 | + .Nested("project_tags", n => n |
| 70 | + .Path(p => p.Tags) |
| 71 | + .Aggregations(nestedAggs => nestedAggs |
| 72 | + .Terms("tags", avg => avg.Field(p => p.Tags.First().Name)) |
| 73 | + ) |
| 74 | + ) |
| 75 | + ) |
| 76 | + ); |
| 77 | + |
| 78 | + protected override AggregationDictionary InitializerAggs => |
| 79 | + new AutoDateHistogramAggregation("projects_started_per_month") |
| 80 | + { |
| 81 | + Field = Field<Project>(p => p.StartedOn), |
| 82 | + Buckets = 10, |
| 83 | + Format = "yyyy-MM-dd'T'HH:mm:ss", |
| 84 | + Missing = FixedDate, |
| 85 | + Aggregations = new NestedAggregation("project_tags") |
| 86 | + { |
| 87 | + Path = Field<Project>(p => p.Tags), |
| 88 | + Aggregations = new TermsAggregation("tags") |
| 89 | + { |
| 90 | + Field = Field<Project>(p => p.Tags.First().Name) |
| 91 | + } |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + protected override void ExpectResponse(ISearchResponse<Project> response) |
| 96 | + { |
| 97 | + /** ==== Handling responses |
| 98 | + * The `AggregateDictionary found on `.Aggregations` on `SearchResponse<T>` has several helper methods |
| 99 | + * so we can fetch our aggregation results easily in the correct type. |
| 100 | + * <<handling-aggregate-response, Be sure to read more about these helper methods>> |
| 101 | + */ |
| 102 | + response.ShouldBeValid(); |
| 103 | + |
| 104 | + var dateHistogram = response.Aggregations.AutoDateHistogram("projects_started_per_month"); |
| 105 | + dateHistogram.Should().NotBeNull(); |
| 106 | + dateHistogram.Interval.Should().NotBeNull(); |
| 107 | + dateHistogram.Buckets.Should().NotBeNull(); |
| 108 | + dateHistogram.Buckets.Count.Should().BeGreaterThan(1); |
| 109 | + foreach (var item in dateHistogram.Buckets) |
| 110 | + { |
| 111 | + item.Date.Should().NotBe(default); |
| 112 | + item.DocCount.Should().BeGreaterThan(0); |
| 113 | + |
| 114 | + var nested = item.Nested("project_tags"); |
| 115 | + nested.Should().NotBeNull(); |
| 116 | + |
| 117 | + var nestedTerms = nested.Terms("tags"); |
| 118 | + nestedTerms.Buckets.Count.Should().BeGreaterThan(0); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // hide |
| 124 | + [SkipVersion("<7.3.0", "minimum_interval introduced in 7.3.0")] |
| 125 | + public class AutoDateHistogramAggregationWithMinimumIntervalUsageTests : ProjectsOnlyAggregationUsageTestBase |
| 126 | + { |
| 127 | + public AutoDateHistogramAggregationWithMinimumIntervalUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } |
| 128 | + |
30 | 129 | protected override object AggregationJson => new |
31 | 130 | { |
32 | 131 | projects_started_per_month = new |
|
0 commit comments