WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Date Range Aggregation Usage
editDate Range Aggregation Usage
editA range aggregation that is dedicated for date values. The main difference between this aggregation and the normal range aggregation is that the from
and to
values can be expressed in DateMath
expressions, and it is also possible to specify a date format by which the from and
to response fields will be returned.
this aggregation includes the from
value and excludes the to
value for each range.
Be sure to read the Elasticsearch documentation on Date Range Aggregation
Fluent DSL example
edits => s .Aggregations(aggs => aggs .DateRange("projects_date_ranges", date => date .Field(p => p.StartedOn) .Ranges( r => r.From(DateMath.Anchored(FixedDate).Add("2d")).To(DateMath.Now), r => r.To(DateMath.Now.Add(TimeSpan.FromDays(1)).Subtract("30m").RoundTo(TimeUnit.Hour)), r => r.From(DateMath.Anchored("2012-05-05").Add(TimeSpan.FromDays(1)).Subtract("1m")) ) .Aggregations(childAggs => childAggs .Terms("project_tags", avg => avg.Field(p => p.Tags)) ) ) )
Object Initializer syntax example
editnew SearchRequest<Project> { Aggregations = new DateRangeAggregation("projects_date_ranges") { Field = Field<Project>(p => p.StartedOn), Ranges = new List<DateRangeExpression> { new DateRangeExpression { From = DateMath.Anchored(FixedDate).Add("2d"), To = DateMath.Now}, new DateRangeExpression { To = DateMath.Now.Add(TimeSpan.FromDays(1)).Subtract("30m").RoundTo(TimeUnit.Hour) }, new DateRangeExpression { From = DateMath.Anchored("2012-05-05").Add(TimeSpan.FromDays(1)).Subtract("1m") } }, Aggregations = new TermsAggregation("project_tags") { Field = Field<Project>(p => p.Tags) } } }
Example json output.
{ "aggs": { "projects_date_ranges": { "date_range": { "field": "startedOn", "ranges": [ { "to": "now", "from": "2015-06-06T12:01:02.123||+2d" }, { "to": "now+1d-30m/h" }, { "from": "2012-05-05||+1d-1m" } ] }, "aggs": { "project_tags": { "terms": { "field": "tags" } } } } } }