Composite Aggregation with After functionality example

I am looking for a code snippet of After functionality usage with NEST lib.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_after.

Thanks in advance for the code snippet

I answered your same question on Stack Overflow. Adding here for completeness.

You can pass the CompositeKey from a previous composite aggregation as the .After() parameter for a new composite aggregation. For example

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool);
var client = new ElasticClient(settings);

var searchResponse = client.Search<object>(s => s
	.From(0)
	.AllIndices()
	.AllTypes()
	.Aggregations(a => a
		.Composite("composite_agg", c => c
			.Sources(so => so
				.DateHistogram("date", dh => dh
					.Field("timestamp")
					.Interval("1d")
				)
				.Terms("product", t => t
					.Field("product")
				)
			)
		)
	)
);

var compositeAgg = searchResponse.Aggregations.Composite("composite_agg");

searchResponse = client.Search<object>(s => s
	.From(0)
	.AllIndices()
	.AllTypes()
	.Aggregations(a => a
		.Composite("composite_agg", c => c
			.Sources(so => so
				.DateHistogram("date", dh => dh
					.Field("timestamp")
					.Interval("1d")
				)
				.Terms("product", t => t
					.Field("product")
				)
			)
			.After(compositeAgg.AfterKey) // <-- pass the after key from previous agg response
		)
	)
);

Assuming you're using Elasticsearch 6.x (which you must be to be using Composite Aggregation), please update NEST client to latest (6.6.0 at this time), as it contains a bug fix for a CompositeKey with null values.

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.