|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Elastic.Xunit.XunitPlumbing; |
| 4 | +using Elasticsearch.Net; |
| 5 | +using FluentAssertions; |
| 6 | +using Nest; |
| 7 | +using Tests.Core.Client; |
| 8 | +using Tests.Core.Extensions; |
| 9 | +using Tests.Core.ManagedElasticsearch.Clusters; |
| 10 | +using Tests.Core.ManagedElasticsearch.NodeSeeders; |
| 11 | +using Tests.Domain; |
| 12 | +using Tests.Framework; |
| 13 | +using Tests.Framework.Integration; |
| 14 | +using static Nest.Infer; |
| 15 | + |
| 16 | +namespace Tests.Aggregations.Bucket.Parent |
| 17 | +{ |
| 18 | + /** |
| 19 | + * A special single bucket aggregation that selects parent documents that have the specified type, as defined in a `join` field. |
| 20 | + * |
| 21 | + * Be sure to read the Elasticsearch documentation on {ref_current}/search-aggregations-bucket-parent-aggregation.html[Parent Aggregation]. |
| 22 | + */ |
| 23 | + public class ParentAggregationUsageTests : ApiIntegrationTestBase<ReadOnlyCluster, ISearchResponse<CommitActivity>, ISearchRequest, SearchDescriptor<CommitActivity>, SearchRequest<CommitActivity>> |
| 24 | + { |
| 25 | + public ParentAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } |
| 26 | + |
| 27 | + protected override bool ExpectIsValid => true; |
| 28 | + |
| 29 | + protected sealed override object ExpectJson => new |
| 30 | + { |
| 31 | + size = 0, |
| 32 | + aggs = new |
| 33 | + { |
| 34 | + name_of_parent_agg = new |
| 35 | + { |
| 36 | + parent = new { type = "commits" }, |
| 37 | + aggs = new |
| 38 | + { |
| 39 | + average_commits = new |
| 40 | + { |
| 41 | + avg = new { field = "numberOfCommits" } |
| 42 | + }, |
| 43 | + max_commits = new |
| 44 | + { |
| 45 | + max = new { field = "numberOfCommits" } |
| 46 | + }, |
| 47 | + min_commits = new |
| 48 | + { |
| 49 | + min = new { field = "numberOfCommits" } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + protected override int ExpectStatusCode => 200; |
| 57 | + |
| 58 | + // hide |
| 59 | + protected override Func<SearchDescriptor<CommitActivity>, ISearchRequest> Fluent => s => s |
| 60 | + .Size(0) |
| 61 | + .Index(DefaultSeeder.CommitsAliasFilter) |
| 62 | + .Type<CommitActivity>() |
| 63 | + .TypedKeys(TestClient.Configuration.Random.TypedKeys) |
| 64 | + .Aggregations(FluentAggs); |
| 65 | + |
| 66 | + protected override HttpMethod HttpMethod => HttpMethod.POST; |
| 67 | + |
| 68 | + // hide |
| 69 | + protected override SearchRequest<CommitActivity> Initializer => |
| 70 | + new SearchRequest<CommitActivity>(DefaultSeeder.CommitsAliasFilter, Type<CommitActivity>()) |
| 71 | + { |
| 72 | + Size = 0, |
| 73 | + TypedKeys = TestClient.Configuration.Random.TypedKeys, |
| 74 | + Aggregations = InitializerAggs |
| 75 | + }; |
| 76 | + |
| 77 | + protected override string UrlPath => $"/commits-only/doc/_search"; |
| 78 | + |
| 79 | + // https://youtrack.jetbrains.com/issue/RIDER-19912 |
| 80 | + [U] protected override Task HitsTheCorrectUrl() => base.HitsTheCorrectUrl(); |
| 81 | + |
| 82 | + [U] protected override Task UsesCorrectHttpMethod() => base.UsesCorrectHttpMethod(); |
| 83 | + |
| 84 | + [U] protected override void SerializesInitializer() => base.SerializesInitializer(); |
| 85 | + |
| 86 | + [U] protected override void SerializesFluent() => base.SerializesFluent(); |
| 87 | + |
| 88 | + [I] public override Task ReturnsExpectedStatusCode() => base.ReturnsExpectedResponse(); |
| 89 | + |
| 90 | + [I] public override Task ReturnsExpectedIsValid() => base.ReturnsExpectedIsValid(); |
| 91 | + |
| 92 | + [I] public override Task ReturnsExpectedResponse() => base.ReturnsExpectedResponse(); |
| 93 | + |
| 94 | + protected override LazyResponses ClientUsage() => Calls( |
| 95 | + (client, f) => client.Search<CommitActivity>(f), |
| 96 | + (client, f) => client.SearchAsync<CommitActivity>(f), |
| 97 | + (client, r) => client.Search<CommitActivity>(r), |
| 98 | + (client, r) => client.SearchAsync<CommitActivity>(r) |
| 99 | + ); |
| 100 | + |
| 101 | + protected Func<AggregationContainerDescriptor<CommitActivity>, IAggregationContainer> FluentAggs => a => a |
| 102 | + .Parent<Project>("name_of_parent_agg", parent => parent // <1> sub-aggregations are on the type determined from the generic type parameter. In this example, the search is against `CommitActivity` type and `Project` is a parent of `CommitActivity` |
| 103 | + .Aggregations(parentAggs => parentAggs |
| 104 | + .Average("average_commits", avg => avg.Field(p => p.NumberOfCommits)) |
| 105 | + .Max("max_commits", avg => avg.Field(p => p.NumberOfCommits)) |
| 106 | + .Min("min_commits", avg => avg.Field(p => p.NumberOfCommits)) |
| 107 | + ) |
| 108 | + ); |
| 109 | + |
| 110 | + protected AggregationDictionary InitializerAggs => |
| 111 | + new ParentAggregation("name_of_parent_agg", typeof(CommitActivity)) // <1> `join` field is determined from the _child_ type. In this example, it is `CommitActivity` |
| 112 | + { |
| 113 | + Aggregations = |
| 114 | + new AverageAggregation("average_commits", Field<Project>(f => f.NumberOfCommits)) // <2> sub-aggregations are on the type determined from the `join` field. In this example, a `Project` is a parent of `CommitActivity` |
| 115 | + && new MaxAggregation("max_commits", Field<Project>(f => f.NumberOfCommits)) |
| 116 | + && new MinAggregation("min_commits", Field<Project>(f => f.NumberOfCommits)) |
| 117 | + }; |
| 118 | + |
| 119 | + protected override void ExpectResponse(ISearchResponse<CommitActivity> response) |
| 120 | + { |
| 121 | + response.ShouldBeValid(); |
| 122 | + |
| 123 | + var parentAgg = response.Aggregations.Parent("name_of_parent_agg"); |
| 124 | + parentAgg.Should().NotBeNull(); |
| 125 | + parentAgg.DocCount.Should().BeGreaterThan(0); |
| 126 | + parentAgg.Min("average_commits").Should().NotBeNull(); |
| 127 | + parentAgg.Min("min_commits").Should().NotBeNull(); |
| 128 | + parentAgg.Max("max_commits").Should().NotBeNull(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments