Range Aggregation Usage

edit

Fluent DSL example

edit
a => a
.Range("commit_ranges", ra => ra
    .Field(p => p.NumberOfCommits)
    .Ranges(
        r => r.To(100),
        r => r.From(100).To(500),
        r => r.From(500)
    )
)

Object Initializer syntax example

edit
new RangeAggregation("commit_ranges")
{
    Field = Field<Project>(p => p.NumberOfCommits),
    Ranges = new List<AggregationRange>
    {
        { new AggregationRange { To = 100 } },
        { new AggregationRange { From = 100, To = 500 } },
        { new AggregationRange { From = 500 } }
    }
}

Example json output.

{
  "commit_ranges": {
    "range": {
      "field": "numberOfCommits",
      "ranges": [
        {
          "to": 100.0
        },
        {
          "from": 100.0,
          "to": 500.0
        },
        {
          "from": 500.0
        }
      ]
    }
  }
}

Handling Responses

edit
response.ShouldBeValid();
var commitRanges = response.Aggregations.Range("commit_ranges");
commitRanges.Should().NotBeNull();
commitRanges.Buckets.Count.Should().Be(3);
commitRanges.Buckets.FirstOrDefault(r => r.Key == "*-100.0").Should().NotBeNull();
commitRanges.Buckets.FirstOrDefault(r => r.Key == "100.0-500.0").Should().NotBeNull();
commitRanges.Buckets.FirstOrDefault(r => r.Key == "500.0-*").Should().NotBeNull();