Range Aggregation Usage

edit

Fluent DSL example

edit
s => s
.Aggregations(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 SearchRequest<Project>
{
    Aggregations = new RangeAggregation("commit_ranges")
    {
        Field = Field<Project>(p => p.NumberOfCommits),
        Ranges = new List<Nest.Range>
        {
            { new Nest.Range { To = 100 } },
            { new Nest.Range { From = 100, To = 500 } },
            { new Nest.Range { From = 500 } }
        }
    }
}

Example json output.

{
  "aggs": {
    "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.Aggs.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();