Rate Aggregation Usageedit

A rate metrics aggregation can be used only inside a date_histogram and calculates a rate of documents or a field in each date_histogram bucket. The field values can be generated by a provided script or extracted from specific numeric or histogram fields in the documents.

Be sure to read the Elasticsearch documentation on Rate Aggregation.

Fluent DSL exampleedit

a => a
.DateHistogram("by_date", d => d
    .Field(f => f.StartedOn)
    .CalendarInterval(DateInterval.Month)
    .Aggregations(a => a
        .Rate("my_rate", m => m
            .Field(p => p.NumberOfCommits)
            .Unit(DateInterval.Month)
            .Mode(RateMode.Sum)
        )))

Object Initializer syntax exampleedit

new DateHistogramAggregation("by_date")
{
    Field = Field<Project>(p => p.StartedOn),
    CalendarInterval = DateInterval.Month,
    Aggregations = new RateAggregation("my_rate", Field<Project>(p => p.NumberOfCommits))
    {
        Unit = DateInterval.Month,
        Mode = RateMode.Sum
    }
}

Example json output.

{
  "by_date": {
    "date_histogram": {
      "field": "startedOn",
      "calendar_interval": "month"
    },
    "aggs": {
      "my_rate": {
        "rate": {
          "field": "numberOfCommits",
          "unit": "month",
          "mode": "sum"
        }
      }
    }
  }
}

Handling Responsesedit

response.ShouldBeValid();

var dateHistogram = response.Aggregations.DateHistogram("by_date");
dateHistogram.Should().NotBeNull();
dateHistogram.Buckets.Should().NotBeNull();
dateHistogram.Buckets.Count.Should().BeGreaterThan(10);
foreach (var item in dateHistogram.Buckets)
{
    var rate = item.Rate("my_rate");
    rate.Should().NotBeNull();
    rate.Value.Should().BeGreaterOrEqualTo(1);
}