Auto Date Histogram Aggregation Usageedit
A multi-bucket aggregation similar to the Date Histogram Aggregation except instead of providing an interval to use as the width of each bucket, a target number of buckets is provided indicating the number of buckets needed and the interval of the buckets is automatically chosen to best achieve that target. The number of buckets returned will always be less than or equal to this target number.
When specifying a format
and extended_bounds
or missing
, in order for Elasticsearch to be able to parse
the serialized DateTime
of extended_bounds
or missing
correctly, the date_optional_time
format is included
as part of the format
value.
Be sure to read the Elasticsearch documentation on Auto Date Histogram Aggregation.
Fluent DSL exampleedit
a => a .AutoDateHistogram("projects_started_per_month", date => date .Field(p => p.StartedOn) .Buckets(10) .Format("yyyy-MM-dd'T'HH:mm:ss") .Missing(FixedDate) .Aggregations(childAggs => childAggs .Nested("project_tags", n => n .Path(p => p.Tags) .Aggregations(nestedAggs => nestedAggs .Terms("tags", avg => avg.Field(p => p.Tags.First().Name)) ) ) ) )
Object Initializer syntax exampleedit
new AutoDateHistogramAggregation("projects_started_per_month") { Field = Field<Project>(p => p.StartedOn), Buckets = 10, Format = "yyyy-MM-dd'T'HH:mm:ss", Missing = FixedDate, Aggregations = new NestedAggregation("project_tags") { Path = Field<Project>(p => p.Tags), Aggregations = new TermsAggregation("tags") { Field = Field<Project>(p => p.Tags.First().Name) } } }
Example json output.
{ "projects_started_per_month": { "auto_date_histogram": { "field": "startedOn", "buckets": 10, "format": "yyyy-MM-dd'T'HH:mm:ss||date_optional_time", "missing": "2015-06-06T12:01:02.123" }, "aggs": { "project_tags": { "nested": { "path": "tags" }, "aggs": { "tags": { "terms": { "field": "tags.name" } } } } } } }
Handling responsesedit
The AggregateDictionary found on `.Aggregations
on SearchResponse<T>
has several helper methods
so we can fetch our aggregation results easily in the correct type.
Be sure to read more about these helper methods
response.ShouldBeValid(); var dateHistogram = response.Aggregations.AutoDateHistogram("projects_started_per_month"); dateHistogram.Should().NotBeNull(); dateHistogram.AutoInterval.Should().NotBeNull(); dateHistogram.Buckets.Should().NotBeNull(); dateHistogram.Buckets.Count.Should().BeGreaterThan(1); foreach (var item in dateHistogram.Buckets) { item.Date.Should().NotBe(default); item.DocCount.Should().BeGreaterThan(0); var nested = item.Nested("project_tags"); nested.Should().NotBeNull(); var nestedTerms = nested.Terms("tags"); nestedTerms.Buckets.Count.Should().BeGreaterThan(0); }