# Composite Aggregation with After functionality example

**URL:** <https://discuss.elastic.co/t/composite-aggregation-with-after-functionality-example/174013>\
**Category:** Elasticsearch\
**Created:** [March 26, 2019, 9:06pm UTC](https://discuss.elastic.co/t/composite-aggregation-with-after-functionality-example/174013 "2019-03-26T21:06:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrei-galkin](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@andrei-galkin](https://discuss.elastic.co/u/andrei-galkin)\
**Post date:** [March 26, 2019, 9:06pm UTC](https://discuss.elastic.co/t/composite-aggregation-with-after-functionality-example/174013/1 "2019-03-26T21:06:42Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_after).

Thanks in advance for the code snippet

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 27, 2019, 4:37am UTC](https://discuss.elastic.co/t/composite-aggregation-with-after-functionality-example/174013/2 "2019-03-27T04:37:00Z")

</div>

I answered [your same question on Stack Overflow](https://stackoverflow.com/a/55368712/1831). 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

```auto
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](https://github.com/elastic/elasticsearch-net/issues/3575).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 24, 2019, 4:37am UTC](https://discuss.elastic.co/t/composite-aggregation-with-after-functionality-example/174013/3 "2019-04-24T04:37:03Z")

</div>

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