|
| 1 | +[[aggregations]] |
| 2 | +== Aggregation examples |
| 3 | + |
| 4 | +This page demonstrates how to use aggregations. |
| 5 | + |
| 6 | +[discrete] |
| 7 | +=== Top-level aggreggation |
| 8 | + |
| 9 | +[discrete] |
| 10 | +==== Fluent API |
| 11 | + |
| 12 | +[source,csharp] |
| 13 | +---- |
| 14 | +var response = await client |
| 15 | + .SearchAsync<Person>(search => search |
| 16 | + .Index("persons") |
| 17 | + .Query(query => query |
| 18 | + .MatchAll(_ => {}) |
| 19 | + ) |
| 20 | + .Aggregations(aggregations => aggregations |
| 21 | + .Add("agg_name", aggregation => aggregation |
| 22 | + .Max(max => max |
| 23 | + .Field(x => x.Age) |
| 24 | + ) |
| 25 | + ) |
| 26 | + ) |
| 27 | + .Size(10) |
| 28 | + ); |
| 29 | +---- |
| 30 | + |
| 31 | +[discrete] |
| 32 | +==== Object initializer API |
| 33 | + |
| 34 | +[source,csharp] |
| 35 | +---- |
| 36 | +var response = await client.SearchAsync<Person>(new SearchRequest("persons") |
| 37 | +{ |
| 38 | + Query = Query.MatchAll(new MatchAllQuery()), |
| 39 | + Aggregations = new Dictionary<string, Aggregation> |
| 40 | + { |
| 41 | + { "agg_name", Aggregation.Max(new MaxAggregation |
| 42 | + { |
| 43 | + Field = Infer.Field<Person>(x => x.Age) |
| 44 | + })} |
| 45 | + }, |
| 46 | + Size = 10 |
| 47 | +}); |
| 48 | +---- |
| 49 | + |
| 50 | +[discrete] |
| 51 | +==== Consume the response |
| 52 | + |
| 53 | +[source,csharp] |
| 54 | +---- |
| 55 | +var max = response.Aggregations!.GetMax("agg_name")!; |
| 56 | +Console.WriteLine(max.Value); |
| 57 | +---- |
| 58 | + |
| 59 | +[discrete] |
| 60 | +=== Sub-aggregation |
| 61 | + |
| 62 | +[discrete] |
| 63 | +==== Fluent API |
| 64 | + |
| 65 | +[source,csharp] |
| 66 | +---- |
| 67 | +var response = await client |
| 68 | + .SearchAsync<Person>(search => search |
| 69 | + .Index("persons") |
| 70 | + .Query(query => query |
| 71 | + .MatchAll(_ => {}) |
| 72 | + ) |
| 73 | + .Aggregations(aggregations => aggregations |
| 74 | + .Add("firstnames", aggregation => aggregation |
| 75 | + .Terms(terms => terms |
| 76 | + .Field(x => x.FirstName) |
| 77 | + ) |
| 78 | + .Aggregations(aggregations => aggregations |
| 79 | + .Add("avg_age", aggregation => aggregation |
| 80 | + .Max(avg => avg |
| 81 | + .Field(x => x.Age) |
| 82 | + ) |
| 83 | + ) |
| 84 | + ) |
| 85 | + ) |
| 86 | + ) |
| 87 | + .Size(10) |
| 88 | + ); |
| 89 | +---- |
| 90 | + |
| 91 | +[discrete] |
| 92 | +==== Object initializer API |
| 93 | + |
| 94 | +[source,csharp] |
| 95 | +---- |
| 96 | +var topLevelAggregation = Aggregation.Terms(new TermsAggregation |
| 97 | +{ |
| 98 | + Field = Infer.Field<Person>(x => x.FirstName) |
| 99 | +}); |
| 100 | +
|
| 101 | +topLevelAggregation.Aggregations = new Dictionary<string, Aggregation> |
| 102 | +{ |
| 103 | + { "avg_age", new MaxAggregation |
| 104 | + { |
| 105 | + Field = Infer.Field<Person>(x => x.Age) |
| 106 | + }} |
| 107 | +}; |
| 108 | +
|
| 109 | +var response = await client.SearchAsync<Person>(new SearchRequest("persons") |
| 110 | +{ |
| 111 | + Query = Query.MatchAll(new MatchAllQuery()), |
| 112 | + Aggregations = new Dictionary<string, Aggregation> |
| 113 | + { |
| 114 | + { "firstnames", topLevelAggregation} |
| 115 | + }, |
| 116 | + Size = 10 |
| 117 | +}); |
| 118 | +---- |
| 119 | + |
| 120 | +[discrete] |
| 121 | +==== Consume the response |
| 122 | + |
| 123 | +[source,csharp] |
| 124 | +---- |
| 125 | +var firstnames = response.Aggregations!.GetStringTerms("firstnames")!; |
| 126 | +foreach (var bucket in firstnames.Buckets) |
| 127 | +{ |
| 128 | + var avg = bucket.Aggregations.GetAverage("avg_age")!; |
| 129 | + Console.WriteLine($"The average age for persons named '{bucket.Key}' is {avg}"); |
| 130 | +} |
| 131 | +---- |
0 commit comments