Aggregation examples

edit

This page demonstrates how to use aggregations.

Top-level aggreggation

edit

Fluent API

edit
var response = await client
	.SearchAsync<Person>(search => search
		.Index("persons")
		.Query(query => query
			.MatchAll(_ => {})
		)
		.Aggregations(aggregations => aggregations
			.Add("agg_name", aggregation => aggregation
				.Max(max => max
					.Field(x => x.Age)
				)
			)
		)
		.Size(10)
	);

Object initializer API

edit
var response = await client.SearchAsync<Person>(new SearchRequest("persons")
{
	Query = Query.MatchAll(new MatchAllQuery()),
	Aggregations = new Dictionary<string, Aggregation>
	{
		{ "agg_name", Aggregation.Max(new MaxAggregation
		{
			Field = Infer.Field<Person>(x => x.Age)
		})}
	},
	Size = 10
});

Consume the response

edit
var max = response.Aggregations!.GetMax("agg_name")!;
Console.WriteLine(max.Value);

Sub-aggregation

edit

Fluent API

edit
var response = await client
	.SearchAsync<Person>(search => search
		.Index("persons")
		.Query(query => query
			.MatchAll(_ => {})
		)
		.Aggregations(aggregations => aggregations
			.Add("firstnames", aggregation => aggregation
				.Terms(terms => terms
					.Field(x => x.FirstName)
				)
				.Aggregations(aggregations => aggregations
					.Add("avg_age", aggregation => aggregation
						.Max(avg => avg
							.Field(x => x.Age)
						)
					)
				)
			)
		)
		.Size(10)
	);

Object initializer API

edit
var topLevelAggregation = Aggregation.Terms(new TermsAggregation
{
	Field = Infer.Field<Person>(x => x.FirstName)
});

topLevelAggregation.Aggregations = new Dictionary<string, Aggregation>
{
	{ "avg_age", new MaxAggregation
	{
		Field = Infer.Field<Person>(x => x.Age)
	}}
};

var response = await client.SearchAsync<Person>(new SearchRequest("persons")
{
	Query = Query.MatchAll(new MatchAllQuery()),
	Aggregations = new Dictionary<string, Aggregation>
	{
		{ "firstnames", topLevelAggregation}
	},
	Size = 10
});

Consume the response

edit
var firstnames = response.Aggregations!.GetStringTerms("firstnames")!;
foreach (var bucket in firstnames.Buckets)
{
	var avg = bucket.Aggregations.GetAverage("avg_age")!;
	Console.WriteLine($"The average age for persons named '{bucket.Key}' is {avg}");
}